Commit | Line | Data |
---|---|---|
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 | */ | |
29 | package jexer; | |
30 | ||
d1115203 KL |
31 | import java.awt.Font; |
32 | import java.awt.FontMetrics; | |
33 | import java.awt.Graphics2D; | |
5fc7bf09 | 34 | import java.awt.image.BufferedImage; |
d1115203 KL |
35 | |
36 | import java.io.InputStream; | |
34a42e78 | 37 | import java.io.IOException; |
55d2b2c2 | 38 | import java.lang.reflect.Field; |
339652cc | 39 | import java.text.MessageFormat; |
a69ed767 | 40 | import java.util.ArrayList; |
d1115203 | 41 | import java.util.HashMap; |
34a42e78 | 42 | import java.util.List; |
bd8d51fa | 43 | import java.util.Map; |
339652cc | 44 | import java.util.ResourceBundle; |
34a42e78 | 45 | |
d1115203 | 46 | import jexer.backend.ECMA48Terminal; |
0d86ab84 | 47 | import jexer.backend.GlyphMaker; |
d1115203 KL |
48 | import jexer.backend.MultiScreen; |
49 | import jexer.backend.SwingTerminal; | |
34a42e78 KL |
50 | import jexer.bits.Cell; |
51 | import jexer.bits.CellAttributes; | |
52 | import jexer.event.TKeypressEvent; | |
b2d49e0f | 53 | import jexer.event.TMenuEvent; |
34a42e78 KL |
54 | import jexer.event.TMouseEvent; |
55 | import jexer.event.TResizeEvent; | |
b2d49e0f | 56 | import jexer.menu.TMenu; |
34a42e78 | 57 | import jexer.tterminal.DisplayLine; |
be72cb5c | 58 | import jexer.tterminal.DisplayListener; |
34a42e78 KL |
59 | import jexer.tterminal.ECMA48; |
60 | import static jexer.TKeypress.*; | |
61 | ||
62 | /** | |
63 | * TTerminalWindow exposes a ECMA-48 / ANSI X3.64 style terminal in a window. | |
64 | */ | |
4d2c61b4 | 65 | public 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 | 165 | // Spin it up |
2bc32111 KL |
166 | terminal = new TTerminalWidget(this, 0, 0, new TAction() { |
167 | public void DO() { | |
168 | onShellExit(); | |
169 | } | |
170 | }); | |
6f8ff91a KL |
171 | } |
172 | ||
173 | /** | |
174 | * Public constructor spawns a shell. | |
175 | * | |
176 | * @param application TApplication that manages this window | |
177 | * @param x column relative to parent | |
178 | * @param y row relative to parent | |
179 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
180 | */ | |
181 | public TTerminalWindow(final TApplication application, final int x, | |
182 | final int y, final int flags) { | |
183 | ||
a69ed767 KL |
184 | this(application, x, y, flags, |
185 | System.getProperty("jexer.TTerminal.closeOnExit", | |
186 | "false").equals("true")); | |
187 | ||
188 | } | |
189 | ||
190 | /** | |
191 | * Public constructor spawns a shell. | |
192 | * | |
193 | * @param application TApplication that manages this window | |
194 | * @param x column relative to parent | |
195 | * @param y row relative to parent | |
196 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
197 | * @param closeOnExit if true, close the window when the shell exits | |
198 | */ | |
199 | public TTerminalWindow(final TApplication application, final int x, | |
200 | final int y, final int flags, final boolean closeOnExit) { | |
201 | ||
6f8ff91a KL |
202 | super(application, i18n.getString("windowTitle"), x, y, |
203 | 80 + 2, 24 + 2, flags); | |
204 | ||
a69ed767 | 205 | this.closeOnExit = closeOnExit; |
4d2c61b4 | 206 | vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); |
a69ed767 | 207 | |
4d2c61b4 KL |
208 | // Claim the keystrokes the emulator will need. |
209 | addShortcutKeys(); | |
6f8ff91a | 210 | |
4d2c61b4 KL |
211 | // Add shortcut text |
212 | newStatusBar(i18n.getString("statusBarRunning")); | |
6f8ff91a | 213 | |
4d2c61b4 | 214 | // Spin it up |
2bc32111 KL |
215 | terminal = new TTerminalWidget(this, 0, 0, new TAction() { |
216 | public void DO() { | |
217 | onShellExit(); | |
218 | } | |
219 | }); | |
6f8ff91a KL |
220 | } |
221 | ||
615a0d99 KL |
222 | // ------------------------------------------------------------------------ |
223 | // TScrollableWindow ------------------------------------------------------ | |
224 | // ------------------------------------------------------------------------ | |
55d2b2c2 | 225 | |
34a42e78 KL |
226 | /** |
227 | * Draw the display buffer. | |
228 | */ | |
229 | @Override | |
230 | public void draw() { | |
4d2c61b4 KL |
231 | setTitle(terminal.getTitle()); |
232 | reflowData(); | |
ab215e38 | 233 | super.draw(); |
107bba16 KL |
234 | } |
235 | ||
be72cb5c | 236 | /** |
615a0d99 | 237 | * Handle window/screen resize events. |
aa77d682 | 238 | * |
615a0d99 | 239 | * @param resize resize event |
aa77d682 | 240 | */ |
615a0d99 KL |
241 | @Override |
242 | public void onResize(final TResizeEvent resize) { | |
4d2c61b4 KL |
243 | if (resize.getType() == TResizeEvent.Type.WIDGET) { |
244 | terminal.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, | |
245 | getWidth() - 2, getHeight() - 2)); | |
615a0d99 | 246 | |
4d2c61b4 KL |
247 | // Resize the scroll bars |
248 | reflowData(); | |
249 | placeScrollbars(); | |
250 | } | |
251 | return; | |
aa77d682 KL |
252 | } |
253 | ||
254 | /** | |
615a0d99 | 255 | * Resize scrollbars for a new width/height. |
aa77d682 | 256 | */ |
615a0d99 KL |
257 | @Override |
258 | public void reflowData() { | |
4d2c61b4 KL |
259 | // Vertical scrollbar |
260 | terminal.reflowData(); | |
261 | setTopValue(terminal.getTopValue()); | |
262 | setBottomValue(terminal.getBottomValue()); | |
263 | setVerticalBigChange(terminal.getVerticalBigChange()); | |
264 | setVerticalValue(terminal.getVerticalValue()); | |
aa77d682 KL |
265 | } |
266 | ||
b2d49e0f | 267 | /** |
615a0d99 KL |
268 | * Handle keystrokes. |
269 | * | |
270 | * @param keypress keystroke event | |
34a42e78 | 271 | */ |
615a0d99 KL |
272 | @Override |
273 | public void onKeypress(final TKeypressEvent keypress) { | |
4d2c61b4 KL |
274 | if (terminal.isReading()) { |
275 | terminal.onKeypress(keypress); | |
276 | } else { | |
277 | super.onKeypress(keypress); | |
34a42e78 | 278 | } |
34a42e78 KL |
279 | } |
280 | ||
281 | /** | |
282 | * Handle mouse press events. | |
283 | * | |
284 | * @param mouse mouse button press event | |
285 | */ | |
286 | @Override | |
287 | public void onMouseDown(final TMouseEvent mouse) { | |
bd8d51fa KL |
288 | if (inWindowMove || inWindowResize) { |
289 | // TWindow needs to deal with this. | |
290 | super.onMouseDown(mouse); | |
291 | return; | |
292 | } | |
34a42e78 | 293 | |
34a42e78 KL |
294 | super.onMouseDown(mouse); |
295 | } | |
296 | ||
bd8d51fa KL |
297 | /** |
298 | * Handle mouse release events. | |
299 | * | |
300 | * @param mouse mouse button release event | |
301 | */ | |
302 | @Override | |
303 | public void onMouseUp(final TMouseEvent mouse) { | |
304 | if (inWindowMove || inWindowResize) { | |
305 | // TWindow needs to deal with this. | |
306 | super.onMouseUp(mouse); | |
307 | return; | |
308 | } | |
309 | ||
bd8d51fa KL |
310 | super.onMouseUp(mouse); |
311 | } | |
312 | ||
313 | /** | |
314 | * Handle mouse motion events. | |
315 | * | |
316 | * @param mouse mouse motion event | |
317 | */ | |
318 | @Override | |
319 | public void onMouseMotion(final TMouseEvent mouse) { | |
320 | if (inWindowMove || inWindowResize) { | |
321 | // TWindow needs to deal with this. | |
322 | super.onMouseMotion(mouse); | |
323 | return; | |
324 | } | |
325 | ||
bd8d51fa KL |
326 | super.onMouseMotion(mouse); |
327 | } | |
328 | ||
615a0d99 KL |
329 | // ------------------------------------------------------------------------ |
330 | // TTerminalWindow -------------------------------------------------------- | |
331 | // ------------------------------------------------------------------------ | |
332 | ||
80b1b7b5 KL |
333 | /** |
334 | * Returns true if this window does not want the application-wide mouse | |
335 | * cursor drawn over it. | |
336 | * | |
337 | * @return true if this window does not want the application-wide mouse | |
338 | * cursor drawn over it | |
339 | */ | |
340 | @Override | |
341 | public boolean hasHiddenMouse() { | |
4d2c61b4 | 342 | return terminal.hasHiddenMouse(); |
80b1b7b5 KL |
343 | } |
344 | ||
615a0d99 KL |
345 | /** |
346 | * Claim the keystrokes the emulator will need. | |
347 | */ | |
348 | private void addShortcutKeys() { | |
349 | addShortcutKeypress(kbCtrlA); | |
350 | addShortcutKeypress(kbCtrlB); | |
351 | addShortcutKeypress(kbCtrlC); | |
352 | addShortcutKeypress(kbCtrlD); | |
353 | addShortcutKeypress(kbCtrlE); | |
354 | addShortcutKeypress(kbCtrlF); | |
355 | addShortcutKeypress(kbCtrlG); | |
356 | addShortcutKeypress(kbCtrlH); | |
357 | addShortcutKeypress(kbCtrlU); | |
358 | addShortcutKeypress(kbCtrlJ); | |
359 | addShortcutKeypress(kbCtrlK); | |
360 | addShortcutKeypress(kbCtrlL); | |
361 | addShortcutKeypress(kbCtrlM); | |
362 | addShortcutKeypress(kbCtrlN); | |
363 | addShortcutKeypress(kbCtrlO); | |
364 | addShortcutKeypress(kbCtrlP); | |
365 | addShortcutKeypress(kbCtrlQ); | |
366 | addShortcutKeypress(kbCtrlR); | |
367 | addShortcutKeypress(kbCtrlS); | |
368 | addShortcutKeypress(kbCtrlT); | |
369 | addShortcutKeypress(kbCtrlU); | |
370 | addShortcutKeypress(kbCtrlV); | |
371 | addShortcutKeypress(kbCtrlW); | |
372 | addShortcutKeypress(kbCtrlX); | |
373 | addShortcutKeypress(kbCtrlY); | |
374 | addShortcutKeypress(kbCtrlZ); | |
375 | addShortcutKeypress(kbF1); | |
376 | addShortcutKeypress(kbF2); | |
377 | addShortcutKeypress(kbF3); | |
378 | addShortcutKeypress(kbF4); | |
379 | addShortcutKeypress(kbF5); | |
380 | addShortcutKeypress(kbF6); | |
381 | addShortcutKeypress(kbF7); | |
382 | addShortcutKeypress(kbF8); | |
383 | addShortcutKeypress(kbF9); | |
384 | addShortcutKeypress(kbF10); | |
385 | addShortcutKeypress(kbF11); | |
386 | addShortcutKeypress(kbF12); | |
387 | addShortcutKeypress(kbAltA); | |
388 | addShortcutKeypress(kbAltB); | |
389 | addShortcutKeypress(kbAltC); | |
390 | addShortcutKeypress(kbAltD); | |
391 | addShortcutKeypress(kbAltE); | |
392 | addShortcutKeypress(kbAltF); | |
393 | addShortcutKeypress(kbAltG); | |
394 | addShortcutKeypress(kbAltH); | |
395 | addShortcutKeypress(kbAltU); | |
396 | addShortcutKeypress(kbAltJ); | |
397 | addShortcutKeypress(kbAltK); | |
398 | addShortcutKeypress(kbAltL); | |
399 | addShortcutKeypress(kbAltM); | |
400 | addShortcutKeypress(kbAltN); | |
401 | addShortcutKeypress(kbAltO); | |
402 | addShortcutKeypress(kbAltP); | |
403 | addShortcutKeypress(kbAltQ); | |
404 | addShortcutKeypress(kbAltR); | |
405 | addShortcutKeypress(kbAltS); | |
406 | addShortcutKeypress(kbAltT); | |
407 | addShortcutKeypress(kbAltU); | |
408 | addShortcutKeypress(kbAltV); | |
409 | addShortcutKeypress(kbAltW); | |
410 | addShortcutKeypress(kbAltX); | |
411 | addShortcutKeypress(kbAltY); | |
412 | addShortcutKeypress(kbAltZ); | |
413 | } | |
414 | ||
615a0d99 KL |
415 | /** |
416 | * Hook for subclasses to be notified of the shell termination. | |
417 | */ | |
418 | public void onShellExit() { | |
a69ed767 KL |
419 | if (closeOnExit) { |
420 | close(); | |
421 | } | |
4d2c61b4 | 422 | clearShortcutKeypresses(); |
615a0d99 KL |
423 | getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT)); |
424 | } | |
425 | ||
34a42e78 | 426 | } |