Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
7b5261bc | 2 | * Jexer - Java Text User Interface |
7d4115a5 | 3 | * |
e16dda65 | 4 | * The MIT License (MIT) |
7d4115a5 | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
7d4115a5 | 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: | |
7d4115a5 | 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. | |
7d4115a5 | 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. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
7d4115a5 KL |
28 | */ |
29 | package jexer; | |
30 | ||
e23ea538 | 31 | import java.io.File; |
4328bb42 | 32 | import java.io.InputStream; |
0d47c546 | 33 | import java.io.IOException; |
4328bb42 | 34 | import java.io.OutputStream; |
6985c572 KL |
35 | import java.io.PrintWriter; |
36 | import java.io.Reader; | |
4328bb42 | 37 | import java.io.UnsupportedEncodingException; |
e23ea538 | 38 | import java.text.MessageFormat; |
a69ed767 | 39 | import java.util.ArrayList; |
a06459bd | 40 | import java.util.Collections; |
d502a0e9 | 41 | import java.util.Date; |
e826b451 | 42 | import java.util.HashMap; |
4328bb42 KL |
43 | import java.util.LinkedList; |
44 | import java.util.List; | |
e826b451 | 45 | import java.util.Map; |
339652cc | 46 | import java.util.ResourceBundle; |
4328bb42 | 47 | |
a69ed767 | 48 | import jexer.bits.Cell; |
4328bb42 KL |
49 | import jexer.bits.CellAttributes; |
50 | import jexer.bits.ColorTheme; | |
4328bb42 KL |
51 | import jexer.event.TCommandEvent; |
52 | import jexer.event.TInputEvent; | |
53 | import jexer.event.TKeypressEvent; | |
fca67db0 | 54 | import jexer.event.TMenuEvent; |
4328bb42 KL |
55 | import jexer.event.TMouseEvent; |
56 | import jexer.event.TResizeEvent; | |
57 | import jexer.backend.Backend; | |
be72cb5c | 58 | import jexer.backend.MultiBackend; |
a69ed767 | 59 | import jexer.backend.Screen; |
a4406f4e | 60 | import jexer.backend.SwingBackend; |
4328bb42 | 61 | import jexer.backend.ECMA48Backend; |
3e074355 | 62 | import jexer.backend.TWindowBackend; |
928811d8 KL |
63 | import jexer.menu.TMenu; |
64 | import jexer.menu.TMenuItem; | |
1dac6b8d | 65 | import jexer.menu.TSubMenu; |
4328bb42 | 66 | import static jexer.TCommand.*; |
2ce6dab2 | 67 | import static jexer.TKeypress.*; |
4328bb42 | 68 | |
7d4115a5 | 69 | /** |
42873e30 KL |
70 | * TApplication is the main driver class for a full Text User Interface |
71 | * application. It manages windows, provides a menu bar and status bar, and | |
72 | * processes events received from the user. | |
7d4115a5 | 73 | */ |
a4406f4e | 74 | public class TApplication implements Runnable { |
7d4115a5 | 75 | |
339652cc KL |
76 | /** |
77 | * Translated strings. | |
78 | */ | |
79 | private static final ResourceBundle i18n = ResourceBundle.getBundle(TApplication.class.getName()); | |
80 | ||
2ce6dab2 | 81 | // ------------------------------------------------------------------------ |
d36057df | 82 | // Constants -------------------------------------------------------------- |
2ce6dab2 KL |
83 | // ------------------------------------------------------------------------ |
84 | ||
99144c71 KL |
85 | /** |
86 | * If true, emit thread stuff to System.err. | |
87 | */ | |
88 | private static final boolean debugThreads = false; | |
89 | ||
a83fea2b KL |
90 | /** |
91 | * If true, emit events being processed to System.err. | |
92 | */ | |
93 | private static final boolean debugEvents = false; | |
94 | ||
a7986f7b KL |
95 | /** |
96 | * If true, do "smart placement" on new windows that are not specified to | |
97 | * be centered. | |
98 | */ | |
99 | private static final boolean smartWindowPlacement = true; | |
100 | ||
a4406f4e KL |
101 | /** |
102 | * Two backend types are available. | |
103 | */ | |
104 | public static enum BackendType { | |
105 | /** | |
106 | * A Swing JFrame. | |
107 | */ | |
108 | SWING, | |
109 | ||
110 | /** | |
111 | * An ECMA48 / ANSI X3.64 / XTERM style terminal. | |
112 | */ | |
113 | ECMA48, | |
114 | ||
115 | /** | |
329fd62e | 116 | * Synonym for ECMA48. |
a4406f4e KL |
117 | */ |
118 | XTERM | |
119 | } | |
120 | ||
2ce6dab2 | 121 | // ------------------------------------------------------------------------ |
d36057df | 122 | // Variables -------------------------------------------------------------- |
2ce6dab2 KL |
123 | // ------------------------------------------------------------------------ |
124 | ||
d36057df KL |
125 | /** |
126 | * The primary event handler thread. | |
127 | */ | |
128 | private volatile WidgetEventHandler primaryEventHandler; | |
129 | ||
130 | /** | |
131 | * The secondary event handler thread. | |
132 | */ | |
133 | private volatile WidgetEventHandler secondaryEventHandler; | |
134 | ||
135 | /** | |
136 | * The widget receiving events from the secondary event handler thread. | |
137 | */ | |
138 | private volatile TWidget secondaryEventReceiver; | |
139 | ||
140 | /** | |
141 | * Access to the physical screen, keyboard, and mouse. | |
142 | */ | |
143 | private Backend backend; | |
144 | ||
145 | /** | |
146 | * Actual mouse coordinate X. | |
147 | */ | |
148 | private int mouseX; | |
149 | ||
150 | /** | |
151 | * Actual mouse coordinate Y. | |
152 | */ | |
153 | private int mouseY; | |
154 | ||
155 | /** | |
156 | * Old version of mouse coordinate X. | |
157 | */ | |
158 | private int oldMouseX; | |
159 | ||
160 | /** | |
161 | * Old version mouse coordinate Y. | |
162 | */ | |
163 | private int oldMouseY; | |
164 | ||
a69ed767 KL |
165 | /** |
166 | * Old drawn version of mouse coordinate X. | |
167 | */ | |
168 | private int oldDrawnMouseX; | |
169 | ||
170 | /** | |
171 | * Old drawn version mouse coordinate Y. | |
172 | */ | |
173 | private int oldDrawnMouseY; | |
174 | ||
175 | /** | |
176 | * Old drawn version mouse cell. | |
177 | */ | |
178 | private Cell oldDrawnMouseCell = new Cell(); | |
179 | ||
d36057df KL |
180 | /** |
181 | * The last mouse up click time, used to determine if this is a mouse | |
182 | * double-click. | |
183 | */ | |
184 | private long lastMouseUpTime; | |
185 | ||
186 | /** | |
187 | * The amount of millis between mouse up events to assume a double-click. | |
188 | */ | |
189 | private long doubleClickTime = 250; | |
190 | ||
191 | /** | |
192 | * Event queue that is filled by run(). | |
193 | */ | |
194 | private List<TInputEvent> fillEventQueue; | |
195 | ||
196 | /** | |
197 | * Event queue that will be drained by either primary or secondary | |
198 | * Thread. | |
199 | */ | |
200 | private List<TInputEvent> drainEventQueue; | |
201 | ||
202 | /** | |
203 | * Top-level menus in this application. | |
204 | */ | |
205 | private List<TMenu> menus; | |
206 | ||
207 | /** | |
208 | * Stack of activated sub-menus in this application. | |
209 | */ | |
210 | private List<TMenu> subMenus; | |
211 | ||
212 | /** | |
213 | * The currently active menu. | |
214 | */ | |
215 | private TMenu activeMenu = null; | |
216 | ||
217 | /** | |
218 | * Active keyboard accelerators. | |
219 | */ | |
220 | private Map<TKeypress, TMenuItem> accelerators; | |
221 | ||
222 | /** | |
223 | * All menu items. | |
224 | */ | |
225 | private List<TMenuItem> menuItems; | |
226 | ||
227 | /** | |
228 | * Windows and widgets pull colors from this ColorTheme. | |
229 | */ | |
230 | private ColorTheme theme; | |
231 | ||
232 | /** | |
233 | * The top-level windows (but not menus). | |
234 | */ | |
235 | private List<TWindow> windows; | |
236 | ||
237 | /** | |
238 | * The currently acive window. | |
239 | */ | |
240 | private TWindow activeWindow = null; | |
241 | ||
242 | /** | |
243 | * Timers that are being ticked. | |
244 | */ | |
245 | private List<TTimer> timers; | |
246 | ||
247 | /** | |
248 | * When true, the application has been started. | |
249 | */ | |
250 | private volatile boolean started = false; | |
251 | ||
252 | /** | |
253 | * When true, exit the application. | |
254 | */ | |
255 | private volatile boolean quit = false; | |
256 | ||
257 | /** | |
258 | * When true, repaint the entire screen. | |
259 | */ | |
260 | private volatile boolean repaint = true; | |
261 | ||
262 | /** | |
263 | * Y coordinate of the top edge of the desktop. For now this is a | |
264 | * constant. Someday it would be nice to have a multi-line menu or | |
265 | * toolbars. | |
266 | */ | |
267 | private static final int desktopTop = 1; | |
268 | ||
269 | /** | |
270 | * Y coordinate of the bottom edge of the desktop. | |
271 | */ | |
272 | private int desktopBottom; | |
273 | ||
274 | /** | |
275 | * An optional TDesktop background window that is drawn underneath | |
276 | * everything else. | |
277 | */ | |
278 | private TDesktop desktop; | |
279 | ||
280 | /** | |
281 | * If true, focus follows mouse: windows automatically raised if the | |
282 | * mouse passes over them. | |
283 | */ | |
284 | private boolean focusFollowsMouse = false; | |
285 | ||
a69ed767 KL |
286 | /** |
287 | * The list of commands to run before the next I/O check. | |
288 | */ | |
289 | private List<Runnable> invokeLaters = new LinkedList<Runnable>(); | |
290 | ||
c6940ed9 KL |
291 | /** |
292 | * WidgetEventHandler is the main event consumer loop. There are at most | |
293 | * two such threads in existence: the primary for normal case and a | |
294 | * secondary that is used for TMessageBox, TInputBox, and similar. | |
295 | */ | |
296 | private class WidgetEventHandler implements Runnable { | |
297 | /** | |
298 | * The main application. | |
299 | */ | |
300 | private TApplication application; | |
301 | ||
302 | /** | |
303 | * Whether or not this WidgetEventHandler is the primary or secondary | |
304 | * thread. | |
305 | */ | |
306 | private boolean primary = true; | |
307 | ||
308 | /** | |
309 | * Public constructor. | |
310 | * | |
311 | * @param application the main application | |
312 | * @param primary if true, this is the primary event handler thread | |
313 | */ | |
314 | public WidgetEventHandler(final TApplication application, | |
315 | final boolean primary) { | |
316 | ||
317 | this.application = application; | |
318 | this.primary = primary; | |
319 | } | |
320 | ||
321 | /** | |
322 | * The consumer loop. | |
323 | */ | |
324 | public void run() { | |
a69ed767 KL |
325 | // Wrap everything in a try, so that if we go belly up we can let |
326 | // the user have their terminal back. | |
327 | try { | |
328 | runImpl(); | |
329 | } catch (Throwable t) { | |
330 | this.application.restoreConsole(); | |
331 | t.printStackTrace(); | |
332 | this.application.exit(); | |
333 | } | |
334 | } | |
335 | ||
336 | /** | |
337 | * The consumer loop. | |
338 | */ | |
339 | private void runImpl() { | |
be72cb5c | 340 | boolean first = true; |
c6940ed9 KL |
341 | |
342 | // Loop forever | |
343 | while (!application.quit) { | |
344 | ||
345 | // Wait until application notifies me | |
346 | while (!application.quit) { | |
347 | try { | |
348 | synchronized (application.drainEventQueue) { | |
349 | if (application.drainEventQueue.size() > 0) { | |
350 | break; | |
351 | } | |
352 | } | |
92554d64 | 353 | |
be72cb5c KL |
354 | long timeout = 0; |
355 | if (first) { | |
356 | first = false; | |
357 | } else { | |
358 | timeout = application.getSleepTime(1000); | |
359 | } | |
360 | ||
361 | if (timeout == 0) { | |
362 | // A timer needs to fire, break out. | |
363 | break; | |
364 | } | |
92554d64 | 365 | |
be72cb5c | 366 | if (debugThreads) { |
a69ed767 | 367 | System.err.printf("%d %s %s %s sleep %d millis\n", |
be72cb5c | 368 | System.currentTimeMillis(), this, |
a69ed767 KL |
369 | primary ? "primary" : "secondary", |
370 | Thread.currentThread(), timeout); | |
be72cb5c | 371 | } |
92554d64 | 372 | |
be72cb5c KL |
373 | synchronized (this) { |
374 | this.wait(timeout); | |
375 | } | |
92554d64 | 376 | |
be72cb5c | 377 | if (debugThreads) { |
a69ed767 | 378 | System.err.printf("%d %s %s %s AWAKE\n", |
be72cb5c | 379 | System.currentTimeMillis(), this, |
a69ed767 KL |
380 | primary ? "primary" : "secondary", |
381 | Thread.currentThread()); | |
be72cb5c KL |
382 | } |
383 | ||
384 | if ((!primary) | |
385 | && (application.secondaryEventReceiver == null) | |
386 | ) { | |
387 | // Secondary thread, emergency exit. If we got | |
388 | // here then something went wrong with the | |
389 | // handoff between yield() and closeWindow(). | |
390 | synchronized (application.primaryEventHandler) { | |
391 | application.primaryEventHandler.notify(); | |
c6940ed9 | 392 | } |
be72cb5c KL |
393 | application.secondaryEventHandler = null; |
394 | throw new RuntimeException("secondary exited " + | |
395 | "at wrong time"); | |
c6940ed9 | 396 | } |
be72cb5c | 397 | break; |
c6940ed9 KL |
398 | } catch (InterruptedException e) { |
399 | // SQUASH | |
400 | } | |
be72cb5c | 401 | } // while (!application.quit) |
ef368bd0 | 402 | |
c6940ed9 KL |
403 | // Pull all events off the queue |
404 | for (;;) { | |
405 | TInputEvent event = null; | |
406 | synchronized (application.drainEventQueue) { | |
407 | if (application.drainEventQueue.size() == 0) { | |
408 | break; | |
409 | } | |
410 | event = application.drainEventQueue.remove(0); | |
411 | } | |
be72cb5c KL |
412 | |
413 | // We will have an event to process, so repaint the | |
414 | // screen at the end. | |
bd8d51fa | 415 | application.repaint = true; |
be72cb5c | 416 | |
c6940ed9 KL |
417 | if (primary) { |
418 | primaryHandleEvent(event); | |
419 | } else { | |
420 | secondaryHandleEvent(event); | |
421 | } | |
422 | if ((!primary) | |
423 | && (application.secondaryEventReceiver == null) | |
424 | ) { | |
99144c71 KL |
425 | // Secondary thread, time to exit. |
426 | ||
a69ed767 KL |
427 | // Eliminate my reference so that wakeEventHandler() |
428 | // resumes working on the primary. | |
429 | application.secondaryEventHandler = null; | |
430 | ||
b9724916 KL |
431 | // We are ready to exit, wake up the primary thread. |
432 | // Remember that it is currently sleeping inside its | |
433 | // primaryHandleEvent(). | |
92554d64 KL |
434 | synchronized (application.primaryEventHandler) { |
435 | application.primaryEventHandler.notify(); | |
436 | } | |
92554d64 KL |
437 | |
438 | // All done! | |
c6940ed9 KL |
439 | return; |
440 | } | |
92554d64 | 441 | |
be72cb5c | 442 | } // for (;;) |
ef368bd0 | 443 | |
be72cb5c KL |
444 | // Fire timers, update screen. |
445 | if (!quit) { | |
446 | application.finishEventProcessing(); | |
c6940ed9 | 447 | } |
92554d64 | 448 | |
c6940ed9 KL |
449 | } // while (true) (main runnable loop) |
450 | } | |
451 | } | |
452 | ||
d36057df KL |
453 | // ------------------------------------------------------------------------ |
454 | // Constructors ----------------------------------------------------------- | |
455 | // ------------------------------------------------------------------------ | |
c6940ed9 KL |
456 | |
457 | /** | |
d36057df KL |
458 | * Public constructor. |
459 | * | |
460 | * @param backendType BackendType.XTERM, BackendType.ECMA48 or | |
461 | * BackendType.SWING | |
462 | * @param windowWidth the number of text columns to start with | |
463 | * @param windowHeight the number of text rows to start with | |
464 | * @param fontSize the size in points | |
465 | * @throws UnsupportedEncodingException if an exception is thrown when | |
466 | * creating the InputStreamReader | |
c6940ed9 | 467 | */ |
d36057df KL |
468 | public TApplication(final BackendType backendType, final int windowWidth, |
469 | final int windowHeight, final int fontSize) | |
470 | throws UnsupportedEncodingException { | |
c6940ed9 | 471 | |
d36057df KL |
472 | switch (backendType) { |
473 | case SWING: | |
474 | backend = new SwingBackend(this, windowWidth, windowHeight, | |
475 | fontSize); | |
476 | break; | |
477 | case XTERM: | |
478 | // Fall through... | |
479 | case ECMA48: | |
480 | backend = new ECMA48Backend(this, null, null, windowWidth, | |
481 | windowHeight, fontSize); | |
482 | break; | |
483 | default: | |
484 | throw new IllegalArgumentException("Invalid backend type: " | |
485 | + backendType); | |
486 | } | |
487 | TApplicationImpl(); | |
488 | } | |
c6940ed9 | 489 | |
92554d64 | 490 | /** |
d36057df KL |
491 | * Public constructor. |
492 | * | |
493 | * @param backendType BackendType.XTERM, BackendType.ECMA48 or | |
494 | * BackendType.SWING | |
495 | * @throws UnsupportedEncodingException if an exception is thrown when | |
496 | * creating the InputStreamReader | |
92554d64 | 497 | */ |
d36057df KL |
498 | public TApplication(final BackendType backendType) |
499 | throws UnsupportedEncodingException { | |
b2d49e0f | 500 | |
d36057df KL |
501 | switch (backendType) { |
502 | case SWING: | |
503 | // The default SwingBackend is 80x25, 20 pt font. If you want to | |
504 | // change that, you can pass the extra arguments to the | |
505 | // SwingBackend constructor here. For example, if you wanted | |
506 | // 90x30, 16 pt font: | |
507 | // | |
508 | // backend = new SwingBackend(this, 90, 30, 16); | |
509 | backend = new SwingBackend(this); | |
510 | break; | |
511 | case XTERM: | |
512 | // Fall through... | |
513 | case ECMA48: | |
514 | backend = new ECMA48Backend(this, null, null); | |
515 | break; | |
516 | default: | |
517 | throw new IllegalArgumentException("Invalid backend type: " | |
518 | + backendType); | |
92554d64 | 519 | } |
d36057df | 520 | TApplicationImpl(); |
92554d64 KL |
521 | } |
522 | ||
7d4115a5 | 523 | /** |
d36057df | 524 | * Public constructor. The backend type will be BackendType.ECMA48. |
55d2b2c2 | 525 | * |
d36057df KL |
526 | * @param input an InputStream connected to the remote user, or null for |
527 | * System.in. If System.in is used, then on non-Windows systems it will | |
528 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked | |
529 | * mode. input is always converted to a Reader with UTF-8 encoding. | |
530 | * @param output an OutputStream connected to the remote user, or null | |
531 | * for System.out. output is always converted to a Writer with UTF-8 | |
532 | * encoding. | |
533 | * @throws UnsupportedEncodingException if an exception is thrown when | |
534 | * creating the InputStreamReader | |
55d2b2c2 | 535 | */ |
d36057df KL |
536 | public TApplication(final InputStream input, |
537 | final OutputStream output) throws UnsupportedEncodingException { | |
538 | ||
539 | backend = new ECMA48Backend(this, input, output); | |
540 | TApplicationImpl(); | |
55d2b2c2 KL |
541 | } |
542 | ||
48e27807 | 543 | /** |
d36057df | 544 | * Public constructor. The backend type will be BackendType.ECMA48. |
48e27807 | 545 | * |
d36057df KL |
546 | * @param input the InputStream underlying 'reader'. Its available() |
547 | * method is used to determine if reader.read() will block or not. | |
548 | * @param reader a Reader connected to the remote user. | |
549 | * @param writer a PrintWriter connected to the remote user. | |
550 | * @param setRawMode if true, set System.in into raw mode with stty. | |
551 | * This should in general not be used. It is here solely for Demo3, | |
552 | * which uses System.in. | |
553 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
48e27807 | 554 | */ |
d36057df KL |
555 | public TApplication(final InputStream input, final Reader reader, |
556 | final PrintWriter writer, final boolean setRawMode) { | |
557 | ||
558 | backend = new ECMA48Backend(this, input, reader, writer, setRawMode); | |
559 | TApplicationImpl(); | |
48e27807 KL |
560 | } |
561 | ||
4328bb42 | 562 | /** |
d36057df KL |
563 | * Public constructor. The backend type will be BackendType.ECMA48. |
564 | * | |
565 | * @param input the InputStream underlying 'reader'. Its available() | |
566 | * method is used to determine if reader.read() will block or not. | |
567 | * @param reader a Reader connected to the remote user. | |
568 | * @param writer a PrintWriter connected to the remote user. | |
569 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
4328bb42 | 570 | */ |
d36057df KL |
571 | public TApplication(final InputStream input, final Reader reader, |
572 | final PrintWriter writer) { | |
4328bb42 | 573 | |
d36057df KL |
574 | this(input, reader, writer, false); |
575 | } | |
4328bb42 | 576 | |
bd8d51fa | 577 | /** |
d36057df KL |
578 | * Public constructor. This hook enables use with new non-Jexer |
579 | * backends. | |
580 | * | |
581 | * @param backend a Backend that is already ready to go. | |
bd8d51fa | 582 | */ |
d36057df KL |
583 | public TApplication(final Backend backend) { |
584 | this.backend = backend; | |
585 | backend.setListener(this); | |
586 | TApplicationImpl(); | |
587 | } | |
bd8d51fa KL |
588 | |
589 | /** | |
d36057df | 590 | * Finish construction once the backend is set. |
bd8d51fa | 591 | */ |
d36057df KL |
592 | private void TApplicationImpl() { |
593 | theme = new ColorTheme(); | |
594 | desktopBottom = getScreen().getHeight() - 1; | |
a69ed767 KL |
595 | fillEventQueue = new LinkedList<TInputEvent>(); |
596 | drainEventQueue = new LinkedList<TInputEvent>(); | |
d36057df | 597 | windows = new LinkedList<TWindow>(); |
a69ed767 KL |
598 | menus = new ArrayList<TMenu>(); |
599 | subMenus = new ArrayList<TMenu>(); | |
d36057df KL |
600 | timers = new LinkedList<TTimer>(); |
601 | accelerators = new HashMap<TKeypress, TMenuItem>(); | |
a69ed767 | 602 | menuItems = new LinkedList<TMenuItem>(); |
d36057df | 603 | desktop = new TDesktop(this); |
bd8d51fa | 604 | |
d36057df KL |
605 | // Special case: the Swing backend needs to have a timer to drive its |
606 | // blink state. | |
607 | if ((backend instanceof SwingBackend) | |
608 | || (backend instanceof MultiBackend) | |
609 | ) { | |
610 | // Default to 500 millis, unless a SwingBackend has its own | |
611 | // value. | |
612 | long millis = 500; | |
613 | if (backend instanceof SwingBackend) { | |
614 | millis = ((SwingBackend) backend).getBlinkMillis(); | |
615 | } | |
616 | if (millis > 0) { | |
617 | addTimer(millis, true, | |
618 | new TAction() { | |
619 | public void DO() { | |
620 | TApplication.this.doRepaint(); | |
621 | } | |
622 | } | |
623 | ); | |
624 | } | |
625 | } | |
626 | } | |
b6faeac0 | 627 | |
d36057df KL |
628 | // ------------------------------------------------------------------------ |
629 | // Runnable --------------------------------------------------------------- | |
630 | // ------------------------------------------------------------------------ | |
b6faeac0 | 631 | |
4328bb42 | 632 | /** |
d36057df | 633 | * Run this application until it exits. |
4328bb42 | 634 | */ |
d36057df | 635 | public void run() { |
abb84744 KL |
636 | // System.err.println("*** TApplication.run() begins ***"); |
637 | ||
d36057df KL |
638 | // Start the main consumer thread |
639 | primaryEventHandler = new WidgetEventHandler(this, true); | |
640 | (new Thread(primaryEventHandler)).start(); | |
8e688b92 | 641 | |
d36057df | 642 | started = true; |
4328bb42 | 643 | |
d36057df KL |
644 | while (!quit) { |
645 | synchronized (this) { | |
646 | boolean doWait = false; | |
fca67db0 | 647 | |
d36057df KL |
648 | if (!backend.hasEvents()) { |
649 | synchronized (fillEventQueue) { | |
650 | if (fillEventQueue.size() == 0) { | |
651 | doWait = true; | |
652 | } | |
653 | } | |
654 | } | |
fca67db0 | 655 | |
d36057df KL |
656 | if (doWait) { |
657 | // No I/O to dispatch, so wait until the backend | |
658 | // provides new I/O. | |
659 | try { | |
660 | if (debugThreads) { | |
661 | System.err.println(System.currentTimeMillis() + | |
a69ed767 | 662 | " " + Thread.currentThread() + " MAIN sleep"); |
d36057df | 663 | } |
fca67db0 | 664 | |
d36057df | 665 | this.wait(); |
e826b451 | 666 | |
d36057df KL |
667 | if (debugThreads) { |
668 | System.err.println(System.currentTimeMillis() + | |
a69ed767 | 669 | " " + Thread.currentThread() + " MAIN AWAKE"); |
d36057df KL |
670 | } |
671 | } catch (InterruptedException e) { | |
672 | // I'm awake and don't care why, let's see what's | |
673 | // going on out there. | |
674 | } | |
675 | } | |
efb7af1f | 676 | |
d36057df | 677 | } // synchronized (this) |
7b5261bc | 678 | |
d36057df KL |
679 | synchronized (fillEventQueue) { |
680 | // Pull any pending I/O events | |
681 | backend.getEvents(fillEventQueue); | |
4328bb42 | 682 | |
d36057df KL |
683 | // Dispatch each event to the appropriate handler, one at a |
684 | // time. | |
685 | for (;;) { | |
686 | TInputEvent event = null; | |
687 | if (fillEventQueue.size() == 0) { | |
688 | break; | |
689 | } | |
690 | event = fillEventQueue.remove(0); | |
691 | metaHandleEvent(event); | |
692 | } | |
693 | } | |
a06459bd | 694 | |
d36057df KL |
695 | // Wake a consumer thread if we have any pending events. |
696 | if (drainEventQueue.size() > 0) { | |
697 | wakeEventHandler(); | |
698 | } | |
92453213 | 699 | |
d36057df | 700 | } // while (!quit) |
d502a0e9 | 701 | |
d36057df KL |
702 | // Shutdown the event consumer threads |
703 | if (secondaryEventHandler != null) { | |
704 | synchronized (secondaryEventHandler) { | |
705 | secondaryEventHandler.notify(); | |
706 | } | |
707 | } | |
708 | if (primaryEventHandler != null) { | |
709 | synchronized (primaryEventHandler) { | |
710 | primaryEventHandler.notify(); | |
711 | } | |
712 | } | |
b2d49e0f | 713 | |
d36057df KL |
714 | // Shutdown the user I/O thread(s) |
715 | backend.shutdown(); | |
4328bb42 | 716 | |
d36057df KL |
717 | // Close all the windows. This gives them an opportunity to release |
718 | // resources. | |
719 | closeAllWindows(); | |
4328bb42 | 720 | |
abb84744 KL |
721 | // Give the overarching application an opportunity to release |
722 | // resources. | |
723 | onExit(); | |
724 | ||
725 | // System.err.println("*** TApplication.run() exits ***"); | |
be72cb5c KL |
726 | } |
727 | ||
d36057df KL |
728 | // ------------------------------------------------------------------------ |
729 | // Event handlers --------------------------------------------------------- | |
730 | // ------------------------------------------------------------------------ | |
48e27807 KL |
731 | |
732 | /** | |
d36057df KL |
733 | * Method that TApplication subclasses can override to handle menu or |
734 | * posted command events. | |
48e27807 | 735 | * |
d36057df KL |
736 | * @param command command event |
737 | * @return if true, this event was consumed | |
48e27807 | 738 | */ |
d36057df KL |
739 | protected boolean onCommand(final TCommandEvent command) { |
740 | // Default: handle cmExit | |
741 | if (command.equals(cmExit)) { | |
742 | if (messageBox(i18n.getString("exitDialogTitle"), | |
743 | i18n.getString("exitDialogText"), | |
a69ed767 KL |
744 | TMessageBox.Type.YESNO).isYes()) { |
745 | ||
d36057df KL |
746 | exit(); |
747 | } | |
748 | return true; | |
749 | } | |
48e27807 | 750 | |
d36057df KL |
751 | if (command.equals(cmShell)) { |
752 | openTerminal(0, 0, TWindow.RESIZABLE); | |
753 | return true; | |
754 | } | |
4328bb42 | 755 | |
d36057df KL |
756 | if (command.equals(cmTile)) { |
757 | tileWindows(); | |
758 | return true; | |
759 | } | |
760 | if (command.equals(cmCascade)) { | |
761 | cascadeWindows(); | |
762 | return true; | |
763 | } | |
764 | if (command.equals(cmCloseAll)) { | |
765 | closeAllWindows(); | |
766 | return true; | |
767 | } | |
0ee88b6d | 768 | |
d36057df KL |
769 | if (command.equals(cmMenu)) { |
770 | if (!modalWindowActive() && (activeMenu == null)) { | |
771 | if (menus.size() > 0) { | |
772 | menus.get(0).setActive(true); | |
773 | activeMenu = menus.get(0); | |
774 | return true; | |
775 | } | |
776 | } | |
0ee88b6d | 777 | } |
0ee88b6d | 778 | |
d36057df | 779 | return false; |
0ee88b6d KL |
780 | } |
781 | ||
92453213 | 782 | /** |
d36057df KL |
783 | * Method that TApplication subclasses can override to handle menu |
784 | * events. | |
92453213 | 785 | * |
d36057df KL |
786 | * @param menu menu event |
787 | * @return if true, this event was consumed | |
92453213 | 788 | */ |
d36057df | 789 | protected boolean onMenu(final TMenuEvent menu) { |
92453213 | 790 | |
d36057df KL |
791 | // Default: handle MID_EXIT |
792 | if (menu.getId() == TMenu.MID_EXIT) { | |
793 | if (messageBox(i18n.getString("exitDialogTitle"), | |
794 | i18n.getString("exitDialogText"), | |
a69ed767 KL |
795 | TMessageBox.Type.YESNO).isYes()) { |
796 | ||
d36057df KL |
797 | exit(); |
798 | } | |
799 | return true; | |
800 | } | |
92453213 | 801 | |
d36057df KL |
802 | if (menu.getId() == TMenu.MID_SHELL) { |
803 | openTerminal(0, 0, TWindow.RESIZABLE); | |
804 | return true; | |
805 | } | |
72fca17b | 806 | |
d36057df KL |
807 | if (menu.getId() == TMenu.MID_TILE) { |
808 | tileWindows(); | |
809 | return true; | |
810 | } | |
811 | if (menu.getId() == TMenu.MID_CASCADE) { | |
812 | cascadeWindows(); | |
813 | return true; | |
814 | } | |
815 | if (menu.getId() == TMenu.MID_CLOSE_ALL) { | |
816 | closeAllWindows(); | |
817 | return true; | |
818 | } | |
e23ea538 KL |
819 | if (menu.getId() == TMenu.MID_ABOUT) { |
820 | showAboutDialog(); | |
821 | return true; | |
822 | } | |
d36057df | 823 | if (menu.getId() == TMenu.MID_REPAINT) { |
a69ed767 | 824 | getScreen().clearPhysical(); |
d36057df KL |
825 | doRepaint(); |
826 | return true; | |
827 | } | |
e23ea538 KL |
828 | if (menu.getId() == TMenu.MID_VIEW_IMAGE) { |
829 | openImage(); | |
830 | return true; | |
831 | } | |
832 | if (menu.getId() == TMenu.MID_CHANGE_FONT) { | |
833 | new TFontChooserWindow(this); | |
834 | return true; | |
835 | } | |
d36057df | 836 | return false; |
72fca17b KL |
837 | } |
838 | ||
839 | /** | |
d36057df | 840 | * Method that TApplication subclasses can override to handle keystrokes. |
72fca17b | 841 | * |
d36057df KL |
842 | * @param keypress keystroke event |
843 | * @return if true, this event was consumed | |
72fca17b | 844 | */ |
d36057df KL |
845 | protected boolean onKeypress(final TKeypressEvent keypress) { |
846 | // Default: only menu shortcuts | |
72fca17b | 847 | |
d36057df KL |
848 | // Process Alt-F, Alt-E, etc. menu shortcut keys |
849 | if (!keypress.getKey().isFnKey() | |
850 | && keypress.getKey().isAlt() | |
851 | && !keypress.getKey().isCtrl() | |
852 | && (activeMenu == null) | |
853 | && !modalWindowActive() | |
854 | ) { | |
2ce6dab2 | 855 | |
d36057df | 856 | assert (subMenus.size() == 0); |
2ce6dab2 | 857 | |
d36057df KL |
858 | for (TMenu menu: menus) { |
859 | if (Character.toLowerCase(menu.getMnemonic().getShortcut()) | |
860 | == Character.toLowerCase(keypress.getKey().getChar()) | |
861 | ) { | |
862 | activeMenu = menu; | |
863 | menu.setActive(true); | |
864 | return true; | |
865 | } | |
866 | } | |
867 | } | |
868 | ||
869 | return false; | |
870 | } | |
2ce6dab2 | 871 | |
eb29bbb5 | 872 | /** |
d36057df | 873 | * Process background events, and update the screen. |
eb29bbb5 | 874 | */ |
d36057df KL |
875 | private void finishEventProcessing() { |
876 | if (debugThreads) { | |
877 | System.err.printf(System.currentTimeMillis() + " " + | |
878 | Thread.currentThread() + " finishEventProcessing()\n"); | |
879 | } | |
eb29bbb5 | 880 | |
d36057df KL |
881 | // Process timers and call doIdle()'s |
882 | doIdle(); | |
883 | ||
884 | // Update the screen | |
885 | synchronized (getScreen()) { | |
886 | drawAll(); | |
887 | } | |
888 | ||
889 | if (debugThreads) { | |
890 | System.err.printf(System.currentTimeMillis() + " " + | |
891 | Thread.currentThread() + " finishEventProcessing() END\n"); | |
eb29bbb5 | 892 | } |
eb29bbb5 KL |
893 | } |
894 | ||
4328bb42 | 895 | /** |
d36057df KL |
896 | * Peek at certain application-level events, add to eventQueue, and wake |
897 | * up the consuming Thread. | |
4328bb42 | 898 | * |
d36057df | 899 | * @param event the input event to consume |
a4406f4e | 900 | */ |
d36057df | 901 | private void metaHandleEvent(final TInputEvent event) { |
a4406f4e | 902 | |
d36057df KL |
903 | if (debugEvents) { |
904 | System.err.printf(String.format("metaHandleEvents event: %s\n", | |
905 | event)); System.err.flush(); | |
a4406f4e | 906 | } |
6985c572 | 907 | |
d36057df KL |
908 | if (quit) { |
909 | // Do no more processing if the application is already trying | |
910 | // to exit. | |
911 | return; | |
912 | } | |
30bd4abd | 913 | |
d36057df | 914 | // Special application-wide events ------------------------------- |
c6940ed9 | 915 | |
d36057df KL |
916 | // Abort everything |
917 | if (event instanceof TCommandEvent) { | |
918 | TCommandEvent command = (TCommandEvent) event; | |
abb84744 | 919 | if (command.equals(cmAbort)) { |
d36057df KL |
920 | exit(); |
921 | return; | |
be72cb5c KL |
922 | } |
923 | } | |
4328bb42 | 924 | |
d36057df KL |
925 | synchronized (drainEventQueue) { |
926 | // Screen resize | |
927 | if (event instanceof TResizeEvent) { | |
928 | TResizeEvent resize = (TResizeEvent) event; | |
929 | synchronized (getScreen()) { | |
930 | getScreen().setDimensions(resize.getWidth(), | |
931 | resize.getHeight()); | |
932 | desktopBottom = getScreen().getHeight() - 1; | |
933 | mouseX = 0; | |
934 | mouseY = 0; | |
935 | oldMouseX = 0; | |
936 | oldMouseY = 0; | |
937 | } | |
938 | if (desktop != null) { | |
939 | desktop.setDimensions(0, 0, resize.getWidth(), | |
940 | resize.getHeight() - 1); | |
5434cb2b | 941 | desktop.onResize(resize); |
d36057df | 942 | } |
2ce6dab2 | 943 | |
d36057df KL |
944 | // Change menu edges if needed. |
945 | recomputeMenuX(); | |
be72cb5c | 946 | |
d36057df KL |
947 | // We are dirty, redraw the screen. |
948 | doRepaint(); | |
be72cb5c | 949 | |
d36057df KL |
950 | /* |
951 | System.err.println("New screen: " + resize.getWidth() + | |
952 | " x " + resize.getHeight()); | |
953 | */ | |
954 | return; | |
955 | } | |
be72cb5c | 956 | |
d36057df KL |
957 | // Put into the main queue |
958 | drainEventQueue.add(event); | |
be72cb5c KL |
959 | } |
960 | } | |
961 | ||
4328bb42 | 962 | /** |
d36057df KL |
963 | * Dispatch one event to the appropriate widget or application-level |
964 | * event handler. This is the primary event handler, it has the normal | |
965 | * application-wide event handling. | |
bd8d51fa | 966 | * |
d36057df KL |
967 | * @param event the input event to consume |
968 | * @see #secondaryHandleEvent(TInputEvent event) | |
4328bb42 | 969 | */ |
d36057df KL |
970 | private void primaryHandleEvent(final TInputEvent event) { |
971 | ||
972 | if (debugEvents) { | |
a69ed767 KL |
973 | System.err.printf("%s primaryHandleEvent: %s\n", |
974 | Thread.currentThread(), event); | |
7b5261bc | 975 | } |
d36057df | 976 | TMouseEvent doubleClick = null; |
4328bb42 | 977 | |
d36057df | 978 | // Special application-wide events ----------------------------------- |
339652cc | 979 | |
d36057df KL |
980 | // Peek at the mouse position |
981 | if (event instanceof TMouseEvent) { | |
982 | TMouseEvent mouse = (TMouseEvent) event; | |
983 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
984 | oldMouseX = mouseX; | |
985 | oldMouseY = mouseY; | |
986 | mouseX = mouse.getX(); | |
987 | mouseY = mouse.getY(); | |
988 | } else { | |
e23ea538 KL |
989 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) |
990 | && (!mouse.isMouseWheelUp()) | |
991 | && (!mouse.isMouseWheelDown()) | |
992 | ) { | |
d36057df KL |
993 | if ((mouse.getTime().getTime() - lastMouseUpTime) < |
994 | doubleClickTime) { | |
99144c71 | 995 | |
d36057df KL |
996 | // This is a double-click. |
997 | doubleClick = new TMouseEvent(TMouseEvent.Type. | |
998 | MOUSE_DOUBLE_CLICK, | |
999 | mouse.getX(), mouse.getY(), | |
1000 | mouse.getAbsoluteX(), mouse.getAbsoluteY(), | |
1001 | mouse.isMouse1(), mouse.isMouse2(), | |
1002 | mouse.isMouse3(), | |
1003 | mouse.isMouseWheelUp(), mouse.isMouseWheelDown()); | |
1004 | ||
1005 | } else { | |
1006 | // The first click of a potential double-click. | |
1007 | lastMouseUpTime = mouse.getTime().getTime(); | |
1008 | } | |
1d14ffab | 1009 | } |
bd8d51fa | 1010 | } |
7b5261bc | 1011 | |
d36057df KL |
1012 | // See if we need to switch focus to another window or the menu |
1013 | checkSwitchFocus((TMouseEvent) event); | |
99144c71 KL |
1014 | } |
1015 | ||
d36057df KL |
1016 | // Handle menu events |
1017 | if ((activeMenu != null) && !(event instanceof TCommandEvent)) { | |
1018 | TMenu menu = activeMenu; | |
7b5261bc | 1019 | |
d36057df KL |
1020 | if (event instanceof TMouseEvent) { |
1021 | TMouseEvent mouse = (TMouseEvent) event; | |
7b5261bc | 1022 | |
d36057df KL |
1023 | while (subMenus.size() > 0) { |
1024 | TMenu subMenu = subMenus.get(subMenus.size() - 1); | |
1025 | if (subMenu.mouseWouldHit(mouse)) { | |
1026 | break; | |
1027 | } | |
1028 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
1029 | && (!mouse.isMouse1()) | |
1030 | && (!mouse.isMouse2()) | |
1031 | && (!mouse.isMouse3()) | |
1032 | && (!mouse.isMouseWheelUp()) | |
1033 | && (!mouse.isMouseWheelDown()) | |
1034 | ) { | |
1035 | break; | |
1036 | } | |
1037 | // We navigated away from a sub-menu, so close it | |
1038 | closeSubMenu(); | |
1039 | } | |
7b5261bc | 1040 | |
d36057df KL |
1041 | // Convert the mouse relative x/y to menu coordinates |
1042 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1043 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1044 | if (subMenus.size() > 0) { | |
1045 | menu = subMenus.get(subMenus.size() - 1); | |
1046 | } | |
1047 | mouse.setX(mouse.getX() - menu.getX()); | |
1048 | mouse.setY(mouse.getY() - menu.getY()); | |
7b5261bc | 1049 | } |
d36057df KL |
1050 | menu.handleEvent(event); |
1051 | return; | |
7b5261bc | 1052 | } |
7b5261bc | 1053 | |
d36057df KL |
1054 | if (event instanceof TKeypressEvent) { |
1055 | TKeypressEvent keypress = (TKeypressEvent) event; | |
2ce6dab2 | 1056 | |
d36057df KL |
1057 | // See if this key matches an accelerator, and is not being |
1058 | // shortcutted by the active window, and if so dispatch the menu | |
1059 | // event. | |
1060 | boolean windowWillShortcut = false; | |
1061 | if (activeWindow != null) { | |
1062 | assert (activeWindow.isShown()); | |
1063 | if (activeWindow.isShortcutKeypress(keypress.getKey())) { | |
1064 | // We do not process this key, it will be passed to the | |
1065 | // window instead. | |
1066 | windowWillShortcut = true; | |
1067 | } | |
1068 | } | |
7b5261bc | 1069 | |
d36057df KL |
1070 | if (!windowWillShortcut && !modalWindowActive()) { |
1071 | TKeypress keypressLowercase = keypress.getKey().toLowerCase(); | |
1072 | TMenuItem item = null; | |
1073 | synchronized (accelerators) { | |
1074 | item = accelerators.get(keypressLowercase); | |
1075 | } | |
1076 | if (item != null) { | |
1077 | if (item.isEnabled()) { | |
1078 | // Let the menu item dispatch | |
1079 | item.dispatch(); | |
1080 | return; | |
339652cc | 1081 | } |
be72cb5c | 1082 | } |
d36057df KL |
1083 | |
1084 | // Handle the keypress | |
1085 | if (onKeypress(keypress)) { | |
1086 | return; | |
1087 | } | |
7b5261bc KL |
1088 | } |
1089 | } | |
1090 | ||
d36057df KL |
1091 | if (event instanceof TCommandEvent) { |
1092 | if (onCommand((TCommandEvent) event)) { | |
1093 | return; | |
1094 | } | |
7b5261bc | 1095 | } |
7b5261bc | 1096 | |
d36057df KL |
1097 | if (event instanceof TMenuEvent) { |
1098 | if (onMenu((TMenuEvent) event)) { | |
1099 | return; | |
1100 | } | |
1d14ffab | 1101 | } |
7b5261bc | 1102 | |
d36057df KL |
1103 | // Dispatch events to the active window ------------------------------- |
1104 | boolean dispatchToDesktop = true; | |
1105 | TWindow window = activeWindow; | |
1106 | if (window != null) { | |
1107 | assert (window.isActive()); | |
1108 | assert (window.isShown()); | |
1109 | if (event instanceof TMouseEvent) { | |
1110 | TMouseEvent mouse = (TMouseEvent) event; | |
1111 | // Convert the mouse relative x/y to window coordinates | |
1112 | assert (mouse.getX() == mouse.getAbsoluteX()); | |
1113 | assert (mouse.getY() == mouse.getAbsoluteY()); | |
1114 | mouse.setX(mouse.getX() - window.getX()); | |
1115 | mouse.setY(mouse.getY() - window.getY()); | |
4328bb42 | 1116 | |
d36057df KL |
1117 | if (doubleClick != null) { |
1118 | doubleClick.setX(doubleClick.getX() - window.getX()); | |
1119 | doubleClick.setY(doubleClick.getY() - window.getY()); | |
1120 | } | |
2ce6dab2 | 1121 | |
d36057df KL |
1122 | if (window.mouseWouldHit(mouse)) { |
1123 | dispatchToDesktop = false; | |
1124 | } | |
1125 | } else if (event instanceof TKeypressEvent) { | |
1126 | dispatchToDesktop = false; | |
1127 | } | |
1128 | ||
1129 | if (debugEvents) { | |
1130 | System.err.printf("TApplication dispatch event: %s\n", | |
1131 | event); | |
1132 | } | |
1133 | window.handleEvent(event); | |
1134 | if (doubleClick != null) { | |
1135 | window.handleEvent(doubleClick); | |
1136 | } | |
1137 | } | |
1138 | if (dispatchToDesktop) { | |
1139 | // This event is fair game for the desktop to process. | |
1140 | if (desktop != null) { | |
1141 | desktop.handleEvent(event); | |
1142 | if (doubleClick != null) { | |
1143 | desktop.handleEvent(doubleClick); | |
1144 | } | |
1145 | } | |
be72cb5c | 1146 | } |
42873e30 KL |
1147 | } |
1148 | ||
4328bb42 | 1149 | /** |
d36057df KL |
1150 | * Dispatch one event to the appropriate widget or application-level |
1151 | * event handler. This is the secondary event handler used by certain | |
1152 | * special dialogs (currently TMessageBox and TFileOpenBox). | |
1153 | * | |
1154 | * @param event the input event to consume | |
1155 | * @see #primaryHandleEvent(TInputEvent event) | |
4328bb42 | 1156 | */ |
d36057df KL |
1157 | private void secondaryHandleEvent(final TInputEvent event) { |
1158 | TMouseEvent doubleClick = null; | |
2027327c | 1159 | |
a69ed767 KL |
1160 | if (debugEvents) { |
1161 | System.err.printf("%s secondaryHandleEvent: %s\n", | |
1162 | Thread.currentThread(), event); | |
1163 | } | |
1164 | ||
d36057df KL |
1165 | // Peek at the mouse position |
1166 | if (event instanceof TMouseEvent) { | |
1167 | TMouseEvent mouse = (TMouseEvent) event; | |
1168 | if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { | |
1169 | oldMouseX = mouseX; | |
1170 | oldMouseY = mouseY; | |
1171 | mouseX = mouse.getX(); | |
1172 | mouseY = mouse.getY(); | |
1173 | } else { | |
e23ea538 KL |
1174 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) |
1175 | && (!mouse.isMouseWheelUp()) | |
1176 | && (!mouse.isMouseWheelDown()) | |
1177 | ) { | |
d36057df KL |
1178 | if ((mouse.getTime().getTime() - lastMouseUpTime) < |
1179 | doubleClickTime) { | |
b2d49e0f | 1180 | |
d36057df KL |
1181 | // This is a double-click. |
1182 | doubleClick = new TMouseEvent(TMouseEvent.Type. | |
1183 | MOUSE_DOUBLE_CLICK, | |
1184 | mouse.getX(), mouse.getY(), | |
1185 | mouse.getAbsoluteX(), mouse.getAbsoluteY(), | |
1186 | mouse.isMouse1(), mouse.isMouse2(), | |
1187 | mouse.isMouse3(), | |
1188 | mouse.isMouseWheelUp(), mouse.isMouseWheelDown()); | |
be72cb5c | 1189 | |
d36057df KL |
1190 | } else { |
1191 | // The first click of a potential double-click. | |
1192 | lastMouseUpTime = mouse.getTime().getTime(); | |
6358f6e5 | 1193 | } |
be72cb5c | 1194 | } |
d36057df KL |
1195 | } |
1196 | } | |
be72cb5c | 1197 | |
d36057df | 1198 | secondaryEventReceiver.handleEvent(event); |
5255f69c KL |
1199 | // Note that it is possible for secondaryEventReceiver to be null |
1200 | // now, because its handleEvent() might have finished out on the | |
1201 | // secondary thread. So put any extra processing inside a null | |
1202 | // check. | |
1203 | if (secondaryEventReceiver != null) { | |
1204 | if (doubleClick != null) { | |
1205 | secondaryEventReceiver.handleEvent(doubleClick); | |
1206 | } | |
d36057df KL |
1207 | } |
1208 | } | |
be72cb5c | 1209 | |
d36057df KL |
1210 | /** |
1211 | * Enable a widget to override the primary event thread. | |
1212 | * | |
1213 | * @param widget widget that will receive events | |
1214 | */ | |
1215 | public final void enableSecondaryEventReceiver(final TWidget widget) { | |
1216 | if (debugThreads) { | |
1217 | System.err.println(System.currentTimeMillis() + | |
1218 | " enableSecondaryEventReceiver()"); | |
1219 | } | |
be72cb5c | 1220 | |
d36057df KL |
1221 | assert (secondaryEventReceiver == null); |
1222 | assert (secondaryEventHandler == null); | |
1223 | assert ((widget instanceof TMessageBox) | |
1224 | || (widget instanceof TFileOpenBox)); | |
1225 | secondaryEventReceiver = widget; | |
1226 | secondaryEventHandler = new WidgetEventHandler(this, false); | |
1227 | ||
1228 | (new Thread(secondaryEventHandler)).start(); | |
1229 | } | |
1230 | ||
1231 | /** | |
1232 | * Yield to the secondary thread. | |
1233 | */ | |
1234 | public final void yield() { | |
a69ed767 KL |
1235 | if (debugThreads) { |
1236 | System.err.printf(System.currentTimeMillis() + " " + | |
1237 | Thread.currentThread() + " yield()\n"); | |
1238 | } | |
1239 | ||
d36057df KL |
1240 | assert (secondaryEventReceiver != null); |
1241 | ||
1242 | while (secondaryEventReceiver != null) { | |
1243 | synchronized (primaryEventHandler) { | |
1244 | try { | |
1245 | primaryEventHandler.wait(); | |
1246 | } catch (InterruptedException e) { | |
1247 | // SQUASH | |
8e688b92 | 1248 | } |
d36057df KL |
1249 | } |
1250 | } | |
1251 | } | |
7b5261bc | 1252 | |
d36057df KL |
1253 | /** |
1254 | * Do stuff when there is no user input. | |
1255 | */ | |
1256 | private void doIdle() { | |
1257 | if (debugThreads) { | |
1258 | System.err.printf(System.currentTimeMillis() + " " + | |
1259 | Thread.currentThread() + " doIdle()\n"); | |
1260 | } | |
ef368bd0 | 1261 | |
d36057df | 1262 | synchronized (timers) { |
8e688b92 | 1263 | |
d36057df KL |
1264 | if (debugThreads) { |
1265 | System.err.printf(System.currentTimeMillis() + " " + | |
1266 | Thread.currentThread() + " doIdle() 2\n"); | |
1267 | } | |
1268 | ||
1269 | // Run any timers that have timed out | |
1270 | Date now = new Date(); | |
1271 | List<TTimer> keepTimers = new LinkedList<TTimer>(); | |
1272 | for (TTimer timer: timers) { | |
1273 | if (timer.getNextTick().getTime() <= now.getTime()) { | |
1274 | // Something might change, so repaint the screen. | |
1275 | repaint = true; | |
1276 | timer.tick(); | |
1277 | if (timer.recurring) { | |
1278 | keepTimers.add(timer); | |
be72cb5c | 1279 | } |
d36057df KL |
1280 | } else { |
1281 | keepTimers.add(timer); | |
8e688b92 | 1282 | } |
8e688b92 | 1283 | } |
e394cb85 KL |
1284 | timers.clear(); |
1285 | timers.addAll(keepTimers); | |
d36057df | 1286 | } |
7b5261bc | 1287 | |
d36057df KL |
1288 | // Call onIdle's |
1289 | for (TWindow window: windows) { | |
1290 | window.onIdle(); | |
1291 | } | |
1292 | if (desktop != null) { | |
1293 | desktop.onIdle(); | |
1294 | } | |
a69ed767 KL |
1295 | |
1296 | // Run any invokeLaters | |
1297 | synchronized (invokeLaters) { | |
1298 | for (Runnable invoke: invokeLaters) { | |
1299 | invoke.run(); | |
1300 | } | |
1301 | invokeLaters.clear(); | |
1302 | } | |
1303 | ||
d36057df | 1304 | } |
92554d64 | 1305 | |
d36057df KL |
1306 | /** |
1307 | * Wake the sleeping active event handler. | |
1308 | */ | |
1309 | private void wakeEventHandler() { | |
1310 | if (!started) { | |
1311 | return; | |
1312 | } | |
92554d64 | 1313 | |
92554d64 KL |
1314 | if (secondaryEventHandler != null) { |
1315 | synchronized (secondaryEventHandler) { | |
1316 | secondaryEventHandler.notify(); | |
1317 | } | |
d36057df KL |
1318 | } else { |
1319 | assert (primaryEventHandler != null); | |
92554d64 KL |
1320 | synchronized (primaryEventHandler) { |
1321 | primaryEventHandler.notify(); | |
1322 | } | |
7b5261bc | 1323 | } |
d36057df | 1324 | } |
7b5261bc | 1325 | |
d36057df KL |
1326 | // ------------------------------------------------------------------------ |
1327 | // TApplication ----------------------------------------------------------- | |
1328 | // ------------------------------------------------------------------------ | |
92554d64 | 1329 | |
a69ed767 KL |
1330 | /** |
1331 | * Place a command on the run queue, and run it before the next round of | |
1332 | * checking I/O. | |
1333 | * | |
1334 | * @param command the command to run later | |
1335 | */ | |
1336 | public void invokeLater(final Runnable command) { | |
1337 | synchronized (invokeLaters) { | |
1338 | invokeLaters.add(command); | |
1339 | } | |
e23ea538 | 1340 | doRepaint(); |
a69ed767 KL |
1341 | } |
1342 | ||
1343 | /** | |
1344 | * Restore the console to sane defaults. This is meant to be used for | |
1345 | * improper exits (e.g. a caught exception in main()), and should not be | |
1346 | * necessary for normal program termination. | |
1347 | */ | |
1348 | public void restoreConsole() { | |
1349 | if (backend != null) { | |
1350 | if (backend instanceof ECMA48Backend) { | |
1351 | backend.shutdown(); | |
1352 | } | |
1353 | } | |
1354 | } | |
1355 | ||
d36057df KL |
1356 | /** |
1357 | * Get the Backend. | |
1358 | * | |
1359 | * @return the Backend | |
1360 | */ | |
1361 | public final Backend getBackend() { | |
1362 | return backend; | |
4328bb42 KL |
1363 | } |
1364 | ||
1365 | /** | |
d36057df | 1366 | * Get the Screen. |
4328bb42 | 1367 | * |
d36057df | 1368 | * @return the Screen |
4328bb42 | 1369 | */ |
d36057df KL |
1370 | public final Screen getScreen() { |
1371 | if (backend instanceof TWindowBackend) { | |
1372 | // We are being rendered to a TWindow. We can't use its | |
1373 | // getScreen() method because that is how it is rendering to a | |
1374 | // hardware backend somewhere. Instead use its getOtherScreen() | |
1375 | // method. | |
1376 | return ((TWindowBackend) backend).getOtherScreen(); | |
1377 | } else { | |
1378 | return backend.getScreen(); | |
8e688b92 | 1379 | } |
d36057df | 1380 | } |
7b5261bc | 1381 | |
d36057df KL |
1382 | /** |
1383 | * Get the color theme. | |
1384 | * | |
1385 | * @return the theme | |
1386 | */ | |
1387 | public final ColorTheme getTheme() { | |
1388 | return theme; | |
1389 | } | |
7b5261bc | 1390 | |
d36057df KL |
1391 | /** |
1392 | * Repaint the screen on the next update. | |
1393 | */ | |
1394 | public void doRepaint() { | |
1395 | repaint = true; | |
1396 | wakeEventHandler(); | |
1397 | } | |
68c5cd6b | 1398 | |
d36057df KL |
1399 | /** |
1400 | * Get Y coordinate of the top edge of the desktop. | |
1401 | * | |
1402 | * @return Y coordinate of the top edge of the desktop | |
1403 | */ | |
1404 | public final int getDesktopTop() { | |
1405 | return desktopTop; | |
1406 | } | |
68c5cd6b | 1407 | |
d36057df KL |
1408 | /** |
1409 | * Get Y coordinate of the bottom edge of the desktop. | |
1410 | * | |
1411 | * @return Y coordinate of the bottom edge of the desktop | |
1412 | */ | |
1413 | public final int getDesktopBottom() { | |
1414 | return desktopBottom; | |
1415 | } | |
7b5261bc | 1416 | |
d36057df KL |
1417 | /** |
1418 | * Set the TDesktop instance. | |
1419 | * | |
1420 | * @param desktop a TDesktop instance, or null to remove the one that is | |
1421 | * set | |
1422 | */ | |
1423 | public final void setDesktop(final TDesktop desktop) { | |
1424 | if (this.desktop != null) { | |
1425 | this.desktop.onClose(); | |
be72cb5c | 1426 | } |
d36057df | 1427 | this.desktop = desktop; |
4328bb42 KL |
1428 | } |
1429 | ||
a06459bd | 1430 | /** |
d36057df | 1431 | * Get the TDesktop instance. |
a06459bd | 1432 | * |
d36057df | 1433 | * @return the desktop, or null if it is not set |
a06459bd | 1434 | */ |
d36057df KL |
1435 | public final TDesktop getDesktop() { |
1436 | return desktop; | |
1437 | } | |
fca67db0 | 1438 | |
d36057df KL |
1439 | /** |
1440 | * Get the current active window. | |
1441 | * | |
1442 | * @return the active window, or null if it is not set | |
1443 | */ | |
1444 | public final TWindow getActiveWindow() { | |
1445 | return activeWindow; | |
1446 | } | |
fca67db0 | 1447 | |
d36057df KL |
1448 | /** |
1449 | * Get a (shallow) copy of the window list. | |
1450 | * | |
1451 | * @return a copy of the list of windows for this application | |
1452 | */ | |
1453 | public final List<TWindow> getAllWindows() { | |
a69ed767 | 1454 | List<TWindow> result = new ArrayList<TWindow>(); |
d36057df KL |
1455 | result.addAll(windows); |
1456 | return result; | |
1457 | } | |
b6faeac0 | 1458 | |
d36057df KL |
1459 | /** |
1460 | * Get focusFollowsMouse flag. | |
1461 | * | |
1462 | * @return true if focus follows mouse: windows automatically raised if | |
1463 | * the mouse passes over them | |
1464 | */ | |
1465 | public boolean getFocusFollowsMouse() { | |
1466 | return focusFollowsMouse; | |
1467 | } | |
b6faeac0 | 1468 | |
d36057df KL |
1469 | /** |
1470 | * Set focusFollowsMouse flag. | |
1471 | * | |
1472 | * @param focusFollowsMouse if true, focus follows mouse: windows | |
1473 | * automatically raised if the mouse passes over them | |
1474 | */ | |
1475 | public void setFocusFollowsMouse(final boolean focusFollowsMouse) { | |
1476 | this.focusFollowsMouse = focusFollowsMouse; | |
1477 | } | |
e8a11f98 | 1478 | |
e23ea538 KL |
1479 | /** |
1480 | * Display the about dialog. | |
1481 | */ | |
1482 | protected void showAboutDialog() { | |
1483 | String version = getClass().getPackage().getImplementationVersion(); | |
1484 | if (version == null) { | |
1485 | // This is Java 9+, use a hardcoded string here. | |
1ef85570 | 1486 | version = "0.3.2"; |
e23ea538 KL |
1487 | } |
1488 | messageBox(i18n.getString("aboutDialogTitle"), | |
1489 | MessageFormat.format(i18n.getString("aboutDialogText"), version), | |
1490 | TMessageBox.Type.OK); | |
1491 | } | |
1492 | ||
1493 | /** | |
1494 | * Handle the Tool | Open image menu item. | |
1495 | */ | |
1496 | private void openImage() { | |
1497 | try { | |
1498 | List<String> filters = new ArrayList<String>(); | |
1499 | filters.add("^.*\\.[Jj][Pp][Gg]$"); | |
1500 | filters.add("^.*\\.[Jj][Pp][Ee][Gg]$"); | |
1501 | filters.add("^.*\\.[Pp][Nn][Gg]$"); | |
1502 | filters.add("^.*\\.[Gg][Ii][Ff]$"); | |
1503 | filters.add("^.*\\.[Bb][Mm][Pp]$"); | |
1504 | String filename = fileOpenBox(".", TFileOpenBox.Type.OPEN, filters); | |
1505 | if (filename != null) { | |
1506 | new TImageWindow(this, new File(filename)); | |
1507 | } | |
1508 | } catch (IOException e) { | |
1509 | // Show this exception to the user. | |
1510 | new TExceptionDialog(this, e); | |
1511 | } | |
1512 | } | |
1513 | ||
9696a8f6 KL |
1514 | /** |
1515 | * Check if application is still running. | |
1516 | * | |
1517 | * @return true if the application is running | |
1518 | */ | |
1519 | public final boolean isRunning() { | |
1520 | if (quit == true) { | |
1521 | return false; | |
1522 | } | |
1523 | return true; | |
1524 | } | |
1525 | ||
d36057df KL |
1526 | // ------------------------------------------------------------------------ |
1527 | // Screen refresh loop ---------------------------------------------------- | |
1528 | // ------------------------------------------------------------------------ | |
fca67db0 | 1529 | |
d36057df KL |
1530 | /** |
1531 | * Invert the cell color at a position. This is used to track the mouse. | |
1532 | * | |
1533 | * @param x column position | |
1534 | * @param y row position | |
1535 | */ | |
1536 | private void invertCell(final int x, final int y) { | |
3af53a35 KL |
1537 | invertCell(x, y, false); |
1538 | } | |
1539 | ||
1540 | /** | |
1541 | * Invert the cell color at a position. This is used to track the mouse. | |
1542 | * | |
1543 | * @param x column position | |
1544 | * @param y row position | |
1545 | * @param onlyThisCell if true, only invert this cell | |
1546 | */ | |
1547 | private void invertCell(final int x, final int y, | |
1548 | final boolean onlyThisCell) { | |
1549 | ||
d36057df KL |
1550 | if (debugThreads) { |
1551 | System.err.printf("%d %s invertCell() %d %d\n", | |
1552 | System.currentTimeMillis(), Thread.currentThread(), x, y); | |
978a5d8f KL |
1553 | |
1554 | if (activeWindow != null) { | |
1555 | System.err.println("activeWindow.hasHiddenMouse() " + | |
1556 | activeWindow.hasHiddenMouse()); | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | // If this cell is on top of a visible window that has requested a | |
1561 | // hidden mouse, bail out. | |
1562 | if ((activeWindow != null) && (activeMenu == null)) { | |
1563 | if ((activeWindow.hasHiddenMouse() == true) | |
1564 | && (x > activeWindow.getX()) | |
1565 | && (x < activeWindow.getX() + activeWindow.getWidth() - 1) | |
1566 | && (y > activeWindow.getY()) | |
1567 | && (y < activeWindow.getY() + activeWindow.getHeight() - 1) | |
1568 | ) { | |
1569 | return; | |
1570 | } | |
d36057df | 1571 | } |
978a5d8f | 1572 | |
a69ed767 KL |
1573 | Cell cell = getScreen().getCharXY(x, y); |
1574 | if (cell.isImage()) { | |
1575 | cell.invertImage(); | |
051e2913 | 1576 | } else { |
a69ed767 KL |
1577 | if (cell.getForeColorRGB() < 0) { |
1578 | cell.setForeColor(cell.getForeColor().invert()); | |
1579 | } else { | |
1580 | cell.setForeColorRGB(cell.getForeColorRGB() ^ 0x00ffffff); | |
1581 | } | |
1582 | if (cell.getBackColorRGB() < 0) { | |
1583 | cell.setBackColor(cell.getBackColor().invert()); | |
1584 | } else { | |
1585 | cell.setBackColorRGB(cell.getBackColorRGB() ^ 0x00ffffff); | |
1586 | } | |
051e2913 | 1587 | } |
a69ed767 | 1588 | getScreen().putCharXY(x, y, cell); |
3af53a35 KL |
1589 | if ((onlyThisCell == true) || (cell.getWidth() == Cell.Width.SINGLE)) { |
1590 | return; | |
1591 | } | |
1592 | ||
1593 | // This cell is one half of a fullwidth glyph. Invert the other | |
1594 | // half. | |
1595 | if (cell.getWidth() == Cell.Width.LEFT) { | |
1596 | if (x < getScreen().getWidth() - 1) { | |
1597 | Cell rightHalf = getScreen().getCharXY(x + 1, y); | |
1598 | if (rightHalf.getWidth() == Cell.Width.RIGHT) { | |
1599 | invertCell(x + 1, y, true); | |
1600 | return; | |
1601 | } | |
1602 | } | |
1603 | } | |
1604 | assert (cell.getWidth() == Cell.Width.RIGHT); | |
1605 | ||
1606 | if (x > 0) { | |
1607 | Cell leftHalf = getScreen().getCharXY(x - 1, y); | |
1608 | if (leftHalf.getWidth() == Cell.Width.LEFT) { | |
1609 | invertCell(x - 1, y, true); | |
1610 | } | |
1611 | } | |
d36057df | 1612 | } |
fca67db0 | 1613 | |
d36057df KL |
1614 | /** |
1615 | * Draw everything. | |
1616 | */ | |
1617 | private void drawAll() { | |
1618 | boolean menuIsActive = false; | |
fca67db0 | 1619 | |
d36057df KL |
1620 | if (debugThreads) { |
1621 | System.err.printf("%d %s drawAll() enter\n", | |
1622 | System.currentTimeMillis(), Thread.currentThread()); | |
fca67db0 | 1623 | } |
a06459bd | 1624 | |
a69ed767 | 1625 | // I don't think this does anything useful anymore... |
d36057df KL |
1626 | if (!repaint) { |
1627 | if (debugThreads) { | |
1628 | System.err.printf("%d %s drawAll() !repaint\n", | |
1629 | System.currentTimeMillis(), Thread.currentThread()); | |
e826b451 | 1630 | } |
a69ed767 KL |
1631 | if ((oldDrawnMouseX != mouseX) || (oldDrawnMouseY != mouseY)) { |
1632 | if (debugThreads) { | |
1633 | System.err.printf("%d %s drawAll() !repaint MOUSE\n", | |
1634 | System.currentTimeMillis(), Thread.currentThread()); | |
fca67db0 | 1635 | } |
a69ed767 KL |
1636 | |
1637 | // The only thing that has happened is the mouse moved. | |
1638 | ||
1639 | // Redraw the old cell at that position, and save the cell at | |
1640 | // the new mouse position. | |
1641 | if (debugThreads) { | |
1642 | System.err.printf("%d %s restoreImage() %d %d\n", | |
1643 | System.currentTimeMillis(), Thread.currentThread(), | |
1644 | oldDrawnMouseX, oldDrawnMouseY); | |
2ce6dab2 | 1645 | } |
a69ed767 KL |
1646 | oldDrawnMouseCell.restoreImage(); |
1647 | getScreen().putCharXY(oldDrawnMouseX, oldDrawnMouseY, | |
1648 | oldDrawnMouseCell); | |
1649 | oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY); | |
e6469faa | 1650 | if (backend instanceof ECMA48Backend) { |
a69ed767 KL |
1651 | // Special case: the entire row containing the mouse has |
1652 | // to be re-drawn if it has any image data, AND any rows | |
1653 | // in between. | |
1654 | if (oldDrawnMouseY != mouseY) { | |
1655 | for (int i = oldDrawnMouseY; ;) { | |
1656 | getScreen().unsetImageRow(i); | |
1657 | if (i == mouseY) { | |
1658 | break; | |
1659 | } | |
1660 | if (oldDrawnMouseY < mouseY) { | |
1661 | i++; | |
1662 | } else { | |
1663 | i--; | |
1664 | } | |
1665 | } | |
1666 | } else { | |
1667 | getScreen().unsetImageRow(mouseY); | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | // Draw mouse at the new position. | |
1672 | invertCell(mouseX, mouseY); | |
1673 | ||
1674 | oldDrawnMouseX = mouseX; | |
1675 | oldDrawnMouseY = mouseY; | |
1676 | } | |
e6469faa | 1677 | if (getScreen().isDirty()) { |
a69ed767 | 1678 | backend.flushScreen(); |
fca67db0 | 1679 | } |
a69ed767 | 1680 | return; |
fca67db0 KL |
1681 | } |
1682 | ||
d36057df KL |
1683 | if (debugThreads) { |
1684 | System.err.printf("%d %s drawAll() REDRAW\n", | |
1685 | System.currentTimeMillis(), Thread.currentThread()); | |
fca67db0 KL |
1686 | } |
1687 | ||
d36057df KL |
1688 | // If true, the cursor is not visible |
1689 | boolean cursor = false; | |
92453213 | 1690 | |
d36057df KL |
1691 | // Start with a clean screen |
1692 | getScreen().clear(); | |
b6faeac0 | 1693 | |
d36057df KL |
1694 | // Draw the desktop |
1695 | if (desktop != null) { | |
1696 | desktop.drawChildren(); | |
1697 | } | |
0ee88b6d | 1698 | |
d36057df | 1699 | // Draw each window in reverse Z order |
a69ed767 | 1700 | List<TWindow> sorted = new ArrayList<TWindow>(windows); |
d36057df KL |
1701 | Collections.sort(sorted); |
1702 | TWindow topLevel = null; | |
1703 | if (sorted.size() > 0) { | |
1704 | topLevel = sorted.get(0); | |
1705 | } | |
1706 | Collections.reverse(sorted); | |
1707 | for (TWindow window: sorted) { | |
1708 | if (window.isShown()) { | |
1709 | window.drawChildren(); | |
b6faeac0 | 1710 | } |
fca67db0 | 1711 | } |
d36057df KL |
1712 | |
1713 | // Draw the blank menubar line - reset the screen clipping first so | |
1714 | // it won't trim it out. | |
1715 | getScreen().resetClipping(); | |
1716 | getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ', | |
1717 | theme.getColor("tmenu")); | |
1718 | // Now draw the menus. | |
1719 | int x = 1; | |
1720 | for (TMenu menu: menus) { | |
1721 | CellAttributes menuColor; | |
1722 | CellAttributes menuMnemonicColor; | |
1723 | if (menu.isActive()) { | |
1724 | menuIsActive = true; | |
1725 | menuColor = theme.getColor("tmenu.highlighted"); | |
1726 | menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); | |
1727 | topLevel = menu; | |
1728 | } else { | |
1729 | menuColor = theme.getColor("tmenu"); | |
1730 | menuMnemonicColor = theme.getColor("tmenu.mnemonic"); | |
1731 | } | |
1732 | // Draw the menu title | |
1733 | getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ', | |
1734 | menuColor); | |
1735 | getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); | |
1736 | // Draw the highlight character | |
1737 | getScreen().putCharXY(x + 1 + menu.getMnemonic().getShortcutIdx(), | |
1738 | 0, menu.getMnemonic().getShortcut(), menuMnemonicColor); | |
1739 | ||
1740 | if (menu.isActive()) { | |
a69ed767 | 1741 | ((TWindow) menu).drawChildren(); |
d36057df KL |
1742 | // Reset the screen clipping so we can draw the next title. |
1743 | getScreen().resetClipping(); | |
0ee88b6d | 1744 | } |
d36057df | 1745 | x += menu.getTitle().length() + 2; |
0ee88b6d | 1746 | } |
0ee88b6d | 1747 | |
d36057df KL |
1748 | for (TMenu menu: subMenus) { |
1749 | // Reset the screen clipping so we can draw the next sub-menu. | |
1750 | getScreen().resetClipping(); | |
a69ed767 | 1751 | ((TWindow) menu).drawChildren(); |
d36057df | 1752 | } |
a69ed767 | 1753 | getScreen().resetClipping(); |
b6faeac0 | 1754 | |
d36057df KL |
1755 | // Draw the status bar of the top-level window |
1756 | TStatusBar statusBar = null; | |
1757 | if (topLevel != null) { | |
1758 | statusBar = topLevel.getStatusBar(); | |
1759 | } | |
1760 | if (statusBar != null) { | |
1761 | getScreen().resetClipping(); | |
1762 | statusBar.setWidth(getScreen().getWidth()); | |
1763 | statusBar.setY(getScreen().getHeight() - topLevel.getY()); | |
1764 | statusBar.draw(); | |
1765 | } else { | |
1766 | CellAttributes barColor = new CellAttributes(); | |
1767 | barColor.setTo(getTheme().getColor("tstatusbar.text")); | |
1768 | getScreen().hLineXY(0, desktopBottom, getScreen().getWidth(), ' ', | |
1769 | barColor); | |
1770 | } | |
b6faeac0 | 1771 | |
d36057df | 1772 | // Draw the mouse pointer |
a69ed767 KL |
1773 | if (debugThreads) { |
1774 | System.err.printf("%d %s restoreImage() %d %d\n", | |
1775 | System.currentTimeMillis(), Thread.currentThread(), | |
1776 | oldDrawnMouseX, oldDrawnMouseY); | |
1777 | } | |
1778 | oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY); | |
e6469faa | 1779 | if (backend instanceof ECMA48Backend) { |
a69ed767 KL |
1780 | // Special case: the entire row containing the mouse has to be |
1781 | // re-drawn if it has any image data, AND any rows in between. | |
1782 | if (oldDrawnMouseY != mouseY) { | |
1783 | for (int i = oldDrawnMouseY; ;) { | |
1784 | getScreen().unsetImageRow(i); | |
1785 | if (i == mouseY) { | |
1786 | break; | |
1787 | } | |
1788 | if (oldDrawnMouseY < mouseY) { | |
1789 | i++; | |
1790 | } else { | |
1791 | i--; | |
1792 | } | |
1793 | } | |
1794 | } else { | |
1795 | getScreen().unsetImageRow(mouseY); | |
1796 | } | |
1797 | } | |
d36057df | 1798 | invertCell(mouseX, mouseY); |
a69ed767 KL |
1799 | oldDrawnMouseX = mouseX; |
1800 | oldDrawnMouseY = mouseY; | |
b6faeac0 | 1801 | |
d36057df KL |
1802 | // Place the cursor if it is visible |
1803 | if (!menuIsActive) { | |
1804 | TWidget activeWidget = null; | |
1805 | if (sorted.size() > 0) { | |
1806 | activeWidget = sorted.get(sorted.size() - 1).getActiveChild(); | |
1807 | if (activeWidget.isCursorVisible()) { | |
1808 | if ((activeWidget.getCursorAbsoluteY() < desktopBottom) | |
1809 | && (activeWidget.getCursorAbsoluteY() > desktopTop) | |
1810 | ) { | |
1811 | getScreen().putCursor(true, | |
1812 | activeWidget.getCursorAbsoluteX(), | |
1813 | activeWidget.getCursorAbsoluteY()); | |
1814 | cursor = true; | |
b6faeac0 | 1815 | } else { |
a69ed767 KL |
1816 | // Turn off the cursor. Also place it at 0,0. |
1817 | getScreen().putCursor(false, 0, 0); | |
d36057df | 1818 | cursor = false; |
b6faeac0 KL |
1819 | } |
1820 | } | |
e8a11f98 KL |
1821 | } |
1822 | } | |
1823 | ||
d36057df KL |
1824 | // Kill the cursor |
1825 | if (!cursor) { | |
1826 | getScreen().hideCursor(); | |
b6faeac0 | 1827 | } |
c6940ed9 | 1828 | |
d36057df | 1829 | // Flush the screen contents |
e6469faa | 1830 | if (getScreen().isDirty()) { |
9696a8f6 KL |
1831 | if (debugThreads) { |
1832 | System.err.printf("%d %s backend.flushScreen()\n", | |
1833 | System.currentTimeMillis(), Thread.currentThread()); | |
1834 | } | |
d36057df | 1835 | backend.flushScreen(); |
be72cb5c KL |
1836 | } |
1837 | ||
d36057df | 1838 | repaint = false; |
a06459bd KL |
1839 | } |
1840 | ||
4328bb42 | 1841 | /** |
d36057df | 1842 | * Force this application to exit. |
4328bb42 | 1843 | */ |
d36057df KL |
1844 | public void exit() { |
1845 | quit = true; | |
1846 | synchronized (this) { | |
1847 | this.notify(); | |
92453213 | 1848 | } |
4328bb42 | 1849 | } |
7d4115a5 | 1850 | |
abb84744 KL |
1851 | /** |
1852 | * Subclasses can use this hook to cleanup resources. Called as the last | |
1853 | * step of TApplication.run(). | |
1854 | */ | |
1855 | public void onExit() { | |
1856 | // Default does nothing. | |
1857 | } | |
1858 | ||
2ce6dab2 KL |
1859 | // ------------------------------------------------------------------------ |
1860 | // TWindow management ----------------------------------------------------- | |
1861 | // ------------------------------------------------------------------------ | |
4328bb42 | 1862 | |
92453213 KL |
1863 | /** |
1864 | * Return the total number of windows. | |
1865 | * | |
1866 | * @return the total number of windows | |
1867 | */ | |
1868 | public final int windowCount() { | |
1869 | return windows.size(); | |
1870 | } | |
1871 | ||
1872 | /** | |
8c236a98 | 1873 | * Return the number of windows that are showing. |
92453213 | 1874 | * |
8c236a98 | 1875 | * @return the number of windows that are showing on screen |
92453213 KL |
1876 | */ |
1877 | public final int shownWindowCount() { | |
1878 | int n = 0; | |
1879 | for (TWindow w: windows) { | |
1880 | if (w.isShown()) { | |
1881 | n++; | |
1882 | } | |
1883 | } | |
1884 | return n; | |
1885 | } | |
1886 | ||
8c236a98 KL |
1887 | /** |
1888 | * Return the number of windows that are hidden. | |
1889 | * | |
1890 | * @return the number of windows that are hidden | |
1891 | */ | |
1892 | public final int hiddenWindowCount() { | |
1893 | int n = 0; | |
1894 | for (TWindow w: windows) { | |
1895 | if (w.isHidden()) { | |
1896 | n++; | |
1897 | } | |
1898 | } | |
1899 | return n; | |
1900 | } | |
1901 | ||
92453213 KL |
1902 | /** |
1903 | * Check if a window instance is in this application's window list. | |
1904 | * | |
1905 | * @param window window to look for | |
1906 | * @return true if this window is in the list | |
1907 | */ | |
1908 | public final boolean hasWindow(final TWindow window) { | |
1909 | if (windows.size() == 0) { | |
1910 | return false; | |
1911 | } | |
1912 | for (TWindow w: windows) { | |
1913 | if (w == window) { | |
8c236a98 | 1914 | assert (window.getApplication() == this); |
92453213 KL |
1915 | return true; |
1916 | } | |
1917 | } | |
1918 | return false; | |
1919 | } | |
1920 | ||
1921 | /** | |
1922 | * Activate a window: bring it to the top and have it receive events. | |
1923 | * | |
1924 | * @param window the window to become the new active window | |
1925 | */ | |
1926 | public void activateWindow(final TWindow window) { | |
1927 | if (hasWindow(window) == false) { | |
1928 | /* | |
1929 | * Someone has a handle to a window I don't have. Ignore this | |
1930 | * request. | |
1931 | */ | |
1932 | return; | |
1933 | } | |
1934 | ||
fe0770f9 KL |
1935 | // Whatever window might be moving/dragging, stop it now. |
1936 | for (TWindow w: windows) { | |
1937 | if (w.inMovements()) { | |
1938 | w.stopMovements(); | |
1939 | } | |
1940 | } | |
1941 | ||
92453213 KL |
1942 | assert (windows.size() > 0); |
1943 | ||
1944 | if (window.isHidden()) { | |
1945 | // Unhiding will also activate. | |
1946 | showWindow(window); | |
1947 | return; | |
1948 | } | |
1949 | assert (window.isShown()); | |
1950 | ||
1951 | if (windows.size() == 1) { | |
1952 | assert (window == windows.get(0)); | |
1953 | if (activeWindow == null) { | |
1954 | activeWindow = window; | |
1955 | window.setZ(0); | |
1956 | activeWindow.setActive(true); | |
1957 | activeWindow.onFocus(); | |
1958 | } | |
1959 | ||
1960 | assert (window.isActive()); | |
1961 | assert (activeWindow == window); | |
1962 | return; | |
1963 | } | |
1964 | ||
1965 | if (activeWindow == window) { | |
1966 | assert (window.isActive()); | |
1967 | ||
1968 | // Window is already active, do nothing. | |
1969 | return; | |
1970 | } | |
1971 | ||
1972 | assert (!window.isActive()); | |
1973 | if (activeWindow != null) { | |
9696a8f6 KL |
1974 | // TODO: see if this assertion is really necessary. |
1975 | // assert (activeWindow.getZ() == 0); | |
92453213 | 1976 | |
92453213 | 1977 | activeWindow.setActive(false); |
a69ed767 KL |
1978 | |
1979 | // Increment every window Z that is on top of window | |
1980 | for (TWindow w: windows) { | |
1981 | if (w == window) { | |
1982 | continue; | |
1983 | } | |
1984 | if (w.getZ() < window.getZ()) { | |
1985 | w.setZ(w.getZ() + 1); | |
1986 | } | |
1987 | } | |
499fdccf KL |
1988 | |
1989 | // Unset activeWindow now before unfocus, so that a window | |
1990 | // lifecycle change inside onUnfocus() doesn't call | |
1991 | // switchWindow() and lead to a stack overflow. | |
1992 | TWindow oldActiveWindow = activeWindow; | |
1993 | activeWindow = null; | |
1994 | oldActiveWindow.onUnfocus(); | |
92453213 KL |
1995 | } |
1996 | activeWindow = window; | |
1997 | activeWindow.setZ(0); | |
1998 | activeWindow.setActive(true); | |
1999 | activeWindow.onFocus(); | |
2000 | return; | |
2001 | } | |
2002 | ||
2003 | /** | |
2004 | * Hide a window. | |
2005 | * | |
2006 | * @param window the window to hide | |
2007 | */ | |
2008 | public void hideWindow(final TWindow window) { | |
2009 | if (hasWindow(window) == false) { | |
2010 | /* | |
2011 | * Someone has a handle to a window I don't have. Ignore this | |
2012 | * request. | |
2013 | */ | |
2014 | return; | |
2015 | } | |
2016 | ||
fe0770f9 KL |
2017 | // Whatever window might be moving/dragging, stop it now. |
2018 | for (TWindow w: windows) { | |
2019 | if (w.inMovements()) { | |
2020 | w.stopMovements(); | |
2021 | } | |
2022 | } | |
2023 | ||
92453213 KL |
2024 | assert (windows.size() > 0); |
2025 | ||
2026 | if (!window.hidden) { | |
2027 | if (window == activeWindow) { | |
2028 | if (shownWindowCount() > 1) { | |
2029 | switchWindow(true); | |
2030 | } else { | |
2031 | activeWindow = null; | |
2032 | window.setActive(false); | |
2033 | window.onUnfocus(); | |
2034 | } | |
2035 | } | |
2036 | window.hidden = true; | |
2037 | window.onHide(); | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | /** | |
2042 | * Show a window. | |
2043 | * | |
2044 | * @param window the window to show | |
2045 | */ | |
2046 | public void showWindow(final TWindow window) { | |
2047 | if (hasWindow(window) == false) { | |
2048 | /* | |
2049 | * Someone has a handle to a window I don't have. Ignore this | |
2050 | * request. | |
2051 | */ | |
2052 | return; | |
2053 | } | |
2054 | ||
fe0770f9 KL |
2055 | // Whatever window might be moving/dragging, stop it now. |
2056 | for (TWindow w: windows) { | |
2057 | if (w.inMovements()) { | |
2058 | w.stopMovements(); | |
2059 | } | |
2060 | } | |
2061 | ||
92453213 KL |
2062 | assert (windows.size() > 0); |
2063 | ||
2064 | if (window.hidden) { | |
2065 | window.hidden = false; | |
2066 | window.onShow(); | |
2067 | activateWindow(window); | |
2068 | } | |
2069 | } | |
2070 | ||
48e27807 KL |
2071 | /** |
2072 | * Close window. Note that the window's destructor is NOT called by this | |
2073 | * method, instead the GC is assumed to do the cleanup. | |
2074 | * | |
2075 | * @param window the window to remove | |
2076 | */ | |
2077 | public final void closeWindow(final TWindow window) { | |
92453213 KL |
2078 | if (hasWindow(window) == false) { |
2079 | /* | |
2080 | * Someone has a handle to a window I don't have. Ignore this | |
2081 | * request. | |
2082 | */ | |
2083 | return; | |
2084 | } | |
2085 | ||
a69ed767 KL |
2086 | // Let window know that it is about to be closed, while it is still |
2087 | // visible on screen. | |
2088 | window.onPreClose(); | |
2089 | ||
bb35d919 | 2090 | synchronized (windows) { |
fe0770f9 KL |
2091 | // Whatever window might be moving/dragging, stop it now. |
2092 | for (TWindow w: windows) { | |
2093 | if (w.inMovements()) { | |
2094 | w.stopMovements(); | |
2095 | } | |
2096 | } | |
2097 | ||
bb35d919 KL |
2098 | int z = window.getZ(); |
2099 | window.setZ(-1); | |
efb7af1f | 2100 | window.onUnfocus(); |
e23ea538 | 2101 | windows.remove(window); |
bb35d919 | 2102 | Collections.sort(windows); |
92453213 | 2103 | activeWindow = null; |
e23ea538 KL |
2104 | int newZ = 0; |
2105 | boolean foundNextWindow = false; | |
2106 | ||
bb35d919 | 2107 | for (TWindow w: windows) { |
e23ea538 KL |
2108 | w.setZ(newZ); |
2109 | newZ++; | |
3eacc236 KL |
2110 | |
2111 | // Do not activate a hidden window. | |
2112 | if (w.isHidden()) { | |
2113 | continue; | |
2114 | } | |
2115 | ||
e23ea538 KL |
2116 | if (foundNextWindow == false) { |
2117 | foundNextWindow = true; | |
2118 | w.setActive(true); | |
2119 | w.onFocus(); | |
2120 | assert (activeWindow == null); | |
2121 | activeWindow = w; | |
2122 | continue; | |
2123 | } | |
2124 | ||
2125 | if (w.isActive()) { | |
2126 | w.setActive(false); | |
2127 | w.onUnfocus(); | |
48e27807 KL |
2128 | } |
2129 | } | |
2130 | } | |
2131 | ||
2132 | // Perform window cleanup | |
2133 | window.onClose(); | |
2134 | ||
48e27807 | 2135 | // Check if we are closing a TMessageBox or similar |
c6940ed9 KL |
2136 | if (secondaryEventReceiver != null) { |
2137 | assert (secondaryEventHandler != null); | |
48e27807 KL |
2138 | |
2139 | // Do not send events to the secondaryEventReceiver anymore, the | |
2140 | // window is closed. | |
2141 | secondaryEventReceiver = null; | |
2142 | ||
92554d64 KL |
2143 | // Wake the secondary thread, it will wake the primary as it |
2144 | // exits. | |
2145 | synchronized (secondaryEventHandler) { | |
2146 | secondaryEventHandler.notify(); | |
48e27807 KL |
2147 | } |
2148 | } | |
92453213 KL |
2149 | |
2150 | // Permit desktop to be active if it is the only thing left. | |
2151 | if (desktop != null) { | |
2152 | if (windows.size() == 0) { | |
2153 | desktop.setActive(true); | |
2154 | } | |
2155 | } | |
48e27807 KL |
2156 | } |
2157 | ||
2158 | /** | |
2159 | * Switch to the next window. | |
2160 | * | |
2161 | * @param forward if true, then switch to the next window in the list, | |
2162 | * otherwise switch to the previous window in the list | |
2163 | */ | |
2164 | public final void switchWindow(final boolean forward) { | |
8c236a98 KL |
2165 | // Only switch if there are multiple visible windows |
2166 | if (shownWindowCount() < 2) { | |
48e27807 KL |
2167 | return; |
2168 | } | |
92453213 | 2169 | assert (activeWindow != null); |
48e27807 | 2170 | |
bb35d919 | 2171 | synchronized (windows) { |
fe0770f9 KL |
2172 | // Whatever window might be moving/dragging, stop it now. |
2173 | for (TWindow w: windows) { | |
2174 | if (w.inMovements()) { | |
2175 | w.stopMovements(); | |
2176 | } | |
2177 | } | |
bb35d919 KL |
2178 | |
2179 | // Swap z/active between active window and the next in the list | |
2180 | int activeWindowI = -1; | |
2181 | for (int i = 0; i < windows.size(); i++) { | |
92453213 KL |
2182 | if (windows.get(i) == activeWindow) { |
2183 | assert (activeWindow.isActive()); | |
bb35d919 KL |
2184 | activeWindowI = i; |
2185 | break; | |
92453213 KL |
2186 | } else { |
2187 | assert (!windows.get(0).isActive()); | |
bb35d919 | 2188 | } |
48e27807 | 2189 | } |
bb35d919 | 2190 | assert (activeWindowI >= 0); |
48e27807 | 2191 | |
bb35d919 | 2192 | // Do not switch if a window is modal |
92453213 | 2193 | if (activeWindow.isModal()) { |
bb35d919 KL |
2194 | return; |
2195 | } | |
48e27807 | 2196 | |
8c236a98 KL |
2197 | int nextWindowI = activeWindowI; |
2198 | for (;;) { | |
2199 | if (forward) { | |
2200 | nextWindowI++; | |
2201 | nextWindowI %= windows.size(); | |
bb35d919 | 2202 | } else { |
8c236a98 KL |
2203 | nextWindowI--; |
2204 | if (nextWindowI < 0) { | |
2205 | nextWindowI = windows.size() - 1; | |
2206 | } | |
bb35d919 | 2207 | } |
bb35d919 | 2208 | |
8c236a98 KL |
2209 | if (windows.get(nextWindowI).isShown()) { |
2210 | activateWindow(windows.get(nextWindowI)); | |
2211 | break; | |
2212 | } | |
2213 | } | |
bb35d919 | 2214 | } // synchronized (windows) |
48e27807 | 2215 | |
48e27807 KL |
2216 | } |
2217 | ||
2218 | /** | |
051e2913 KL |
2219 | * Add a window to my window list and make it active. Note package |
2220 | * private access. | |
48e27807 KL |
2221 | * |
2222 | * @param window new window to add | |
2223 | */ | |
051e2913 | 2224 | final void addWindowToApplication(final TWindow window) { |
a7986f7b KL |
2225 | |
2226 | // Do not add menu windows to the window list. | |
2227 | if (window instanceof TMenu) { | |
2228 | return; | |
2229 | } | |
2230 | ||
0ee88b6d KL |
2231 | // Do not add the desktop to the window list. |
2232 | if (window instanceof TDesktop) { | |
2233 | return; | |
2234 | } | |
2235 | ||
bb35d919 | 2236 | synchronized (windows) { |
051e2913 KL |
2237 | if (windows.contains(window)) { |
2238 | throw new IllegalArgumentException("Window " + window + | |
2239 | " is already in window list"); | |
2240 | } | |
2241 | ||
fe0770f9 KL |
2242 | // Whatever window might be moving/dragging, stop it now. |
2243 | for (TWindow w: windows) { | |
2244 | if (w.inMovements()) { | |
2245 | w.stopMovements(); | |
2246 | } | |
2247 | } | |
2248 | ||
2ce6dab2 KL |
2249 | // Do not allow a modal window to spawn a non-modal window. If a |
2250 | // modal window is active, then this window will become modal | |
2251 | // too. | |
2252 | if (modalWindowActive()) { | |
2253 | window.flags |= TWindow.MODAL; | |
a7986f7b | 2254 | window.flags |= TWindow.CENTERED; |
92453213 | 2255 | window.hidden = false; |
bb35d919 | 2256 | } |
92453213 KL |
2257 | if (window.isShown()) { |
2258 | for (TWindow w: windows) { | |
2259 | if (w.isActive()) { | |
2260 | w.setActive(false); | |
2261 | w.onUnfocus(); | |
2262 | } | |
2263 | w.setZ(w.getZ() + 1); | |
efb7af1f | 2264 | } |
bb35d919 KL |
2265 | } |
2266 | windows.add(window); | |
92453213 KL |
2267 | if (window.isShown()) { |
2268 | activeWindow = window; | |
2269 | activeWindow.setZ(0); | |
2270 | activeWindow.setActive(true); | |
2271 | activeWindow.onFocus(); | |
2272 | } | |
a7986f7b KL |
2273 | |
2274 | if (((window.flags & TWindow.CENTERED) == 0) | |
d36057df KL |
2275 | && ((window.flags & TWindow.ABSOLUTEXY) == 0) |
2276 | && (smartWindowPlacement == true) | |
2277 | ) { | |
a7986f7b KL |
2278 | |
2279 | doSmartPlacement(window); | |
2280 | } | |
48e27807 | 2281 | } |
92453213 KL |
2282 | |
2283 | // Desktop cannot be active over any other window. | |
2284 | if (desktop != null) { | |
2285 | desktop.setActive(false); | |
2286 | } | |
48e27807 KL |
2287 | } |
2288 | ||
fca67db0 KL |
2289 | /** |
2290 | * Check if there is a system-modal window on top. | |
2291 | * | |
2292 | * @return true if the active window is modal | |
2293 | */ | |
2294 | private boolean modalWindowActive() { | |
2295 | if (windows.size() == 0) { | |
2296 | return false; | |
2297 | } | |
2ce6dab2 KL |
2298 | |
2299 | for (TWindow w: windows) { | |
2300 | if (w.isModal()) { | |
2301 | return true; | |
2302 | } | |
2303 | } | |
2304 | ||
2305 | return false; | |
2306 | } | |
2307 | ||
9696a8f6 KL |
2308 | /** |
2309 | * Check if there is a window with overridden menu flag on top. | |
2310 | * | |
2311 | * @return true if the active window is overriding the menu | |
2312 | */ | |
2313 | private boolean overrideMenuWindowActive() { | |
2314 | if (activeWindow != null) { | |
2315 | if (activeWindow.hasOverriddenMenu()) { | |
2316 | return true; | |
2317 | } | |
2318 | } | |
2319 | ||
2320 | return false; | |
2321 | } | |
2322 | ||
2ce6dab2 KL |
2323 | /** |
2324 | * Close all open windows. | |
2325 | */ | |
2326 | private void closeAllWindows() { | |
2327 | // Don't do anything if we are in the menu | |
2328 | if (activeMenu != null) { | |
2329 | return; | |
2330 | } | |
2331 | while (windows.size() > 0) { | |
2332 | closeWindow(windows.get(0)); | |
2333 | } | |
fca67db0 KL |
2334 | } |
2335 | ||
2ce6dab2 KL |
2336 | /** |
2337 | * Re-layout the open windows as non-overlapping tiles. This produces | |
2338 | * almost the same results as Turbo Pascal 7.0's IDE. | |
2339 | */ | |
2340 | private void tileWindows() { | |
2341 | synchronized (windows) { | |
2342 | // Don't do anything if we are in the menu | |
2343 | if (activeMenu != null) { | |
2344 | return; | |
2345 | } | |
2346 | int z = windows.size(); | |
2347 | if (z == 0) { | |
2348 | return; | |
2349 | } | |
2350 | int a = 0; | |
2351 | int b = 0; | |
2352 | a = (int)(Math.sqrt(z)); | |
2353 | int c = 0; | |
2354 | while (c < a) { | |
2355 | b = (z - c) / a; | |
2356 | if (((a * b) + c) == z) { | |
2357 | break; | |
2358 | } | |
2359 | c++; | |
2360 | } | |
2361 | assert (a > 0); | |
2362 | assert (b > 0); | |
2363 | assert (c < a); | |
2364 | int newWidth = (getScreen().getWidth() / a); | |
2365 | int newHeight1 = ((getScreen().getHeight() - 1) / b); | |
2366 | int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); | |
2367 | ||
a69ed767 | 2368 | List<TWindow> sorted = new ArrayList<TWindow>(windows); |
2ce6dab2 KL |
2369 | Collections.sort(sorted); |
2370 | Collections.reverse(sorted); | |
2371 | for (int i = 0; i < sorted.size(); i++) { | |
2372 | int logicalX = i / b; | |
2373 | int logicalY = i % b; | |
2374 | if (i >= ((a - 1) * b)) { | |
2375 | logicalX = a - 1; | |
2376 | logicalY = i - ((a - 1) * b); | |
2377 | } | |
2378 | ||
2379 | TWindow w = sorted.get(i); | |
7d922e0d KL |
2380 | int oldWidth = w.getWidth(); |
2381 | int oldHeight = w.getHeight(); | |
2382 | ||
2ce6dab2 KL |
2383 | w.setX(logicalX * newWidth); |
2384 | w.setWidth(newWidth); | |
2385 | if (i >= ((a - 1) * b)) { | |
2386 | w.setY((logicalY * newHeight2) + 1); | |
2387 | w.setHeight(newHeight2); | |
2388 | } else { | |
2389 | w.setY((logicalY * newHeight1) + 1); | |
2390 | w.setHeight(newHeight1); | |
2391 | } | |
7d922e0d KL |
2392 | if ((w.getWidth() != oldWidth) |
2393 | || (w.getHeight() != oldHeight) | |
2394 | ) { | |
2395 | w.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, | |
2396 | w.getWidth(), w.getHeight())); | |
2397 | } | |
2ce6dab2 KL |
2398 | } |
2399 | } | |
2400 | } | |
2401 | ||
2402 | /** | |
2403 | * Re-layout the open windows as overlapping cascaded windows. | |
2404 | */ | |
2405 | private void cascadeWindows() { | |
2406 | synchronized (windows) { | |
2407 | // Don't do anything if we are in the menu | |
2408 | if (activeMenu != null) { | |
2409 | return; | |
2410 | } | |
2411 | int x = 0; | |
2412 | int y = 1; | |
a69ed767 | 2413 | List<TWindow> sorted = new ArrayList<TWindow>(windows); |
2ce6dab2 KL |
2414 | Collections.sort(sorted); |
2415 | Collections.reverse(sorted); | |
2416 | for (TWindow window: sorted) { | |
2417 | window.setX(x); | |
2418 | window.setY(y); | |
2419 | x++; | |
2420 | y++; | |
2421 | if (x > getScreen().getWidth()) { | |
2422 | x = 0; | |
2423 | } | |
2424 | if (y >= getScreen().getHeight()) { | |
2425 | y = 1; | |
2426 | } | |
2427 | } | |
2428 | } | |
2429 | } | |
2430 | ||
a7986f7b KL |
2431 | /** |
2432 | * Place a window to minimize its overlap with other windows. | |
2433 | * | |
2434 | * @param window the window to place | |
2435 | */ | |
2436 | public final void doSmartPlacement(final TWindow window) { | |
2437 | // This is a pretty dumb algorithm, but seems to work. The hardest | |
2438 | // part is computing these "overlap" values seeking a minimum average | |
2439 | // overlap. | |
2440 | int xMin = 0; | |
2441 | int yMin = desktopTop; | |
2442 | int xMax = getScreen().getWidth() - window.getWidth() + 1; | |
2443 | int yMax = desktopBottom - window.getHeight() + 1; | |
2444 | if (xMax < xMin) { | |
2445 | xMax = xMin; | |
2446 | } | |
2447 | if (yMax < yMin) { | |
2448 | yMax = yMin; | |
2449 | } | |
2450 | ||
2451 | if ((xMin == xMax) && (yMin == yMax)) { | |
2452 | // No work to do, bail out. | |
2453 | return; | |
2454 | } | |
2455 | ||
2456 | // Compute the overlap matrix without the new window. | |
2457 | int width = getScreen().getWidth(); | |
2458 | int height = getScreen().getHeight(); | |
2459 | int overlapMatrix[][] = new int[width][height]; | |
2460 | for (TWindow w: windows) { | |
2461 | if (window == w) { | |
2462 | continue; | |
2463 | } | |
2464 | for (int x = w.getX(); x < w.getX() + w.getWidth(); x++) { | |
9ff1c0e3 KL |
2465 | if (x < 0) { |
2466 | continue; | |
2467 | } | |
8c236a98 | 2468 | if (x >= width) { |
a7986f7b KL |
2469 | continue; |
2470 | } | |
2471 | for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) { | |
9ff1c0e3 KL |
2472 | if (y < 0) { |
2473 | continue; | |
2474 | } | |
8c236a98 | 2475 | if (y >= height) { |
a7986f7b KL |
2476 | continue; |
2477 | } | |
2478 | overlapMatrix[x][y]++; | |
2479 | } | |
2480 | } | |
2481 | } | |
2482 | ||
2483 | long oldOverlapTotal = 0; | |
2484 | long oldOverlapN = 0; | |
2485 | for (int x = 0; x < width; x++) { | |
2486 | for (int y = 0; y < height; y++) { | |
2487 | oldOverlapTotal += overlapMatrix[x][y]; | |
2488 | if (overlapMatrix[x][y] > 0) { | |
2489 | oldOverlapN++; | |
2490 | } | |
2491 | } | |
2492 | } | |
2493 | ||
2494 | ||
2495 | double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN; | |
2496 | boolean first = true; | |
2497 | int windowX = window.getX(); | |
2498 | int windowY = window.getY(); | |
2499 | ||
2500 | // For each possible (x, y) position for the new window, compute a | |
2501 | // new overlap matrix. | |
2502 | for (int x = xMin; x < xMax; x++) { | |
2503 | for (int y = yMin; y < yMax; y++) { | |
2504 | ||
2505 | // Start with the matrix minus this window. | |
2506 | int newMatrix[][] = new int[width][height]; | |
2507 | for (int mx = 0; mx < width; mx++) { | |
2508 | for (int my = 0; my < height; my++) { | |
2509 | newMatrix[mx][my] = overlapMatrix[mx][my]; | |
2510 | } | |
2511 | } | |
2512 | ||
2513 | // Add this window's values to the new overlap matrix. | |
2514 | long newOverlapTotal = 0; | |
2515 | long newOverlapN = 0; | |
2516 | // Start by adding each new cell. | |
2517 | for (int wx = x; wx < x + window.getWidth(); wx++) { | |
8c236a98 | 2518 | if (wx >= width) { |
a7986f7b KL |
2519 | continue; |
2520 | } | |
2521 | for (int wy = y; wy < y + window.getHeight(); wy++) { | |
8c236a98 | 2522 | if (wy >= height) { |
a7986f7b KL |
2523 | continue; |
2524 | } | |
2525 | newMatrix[wx][wy]++; | |
2526 | } | |
2527 | } | |
2528 | // Now figure out the new value for total coverage. | |
2529 | for (int mx = 0; mx < width; mx++) { | |
2530 | for (int my = 0; my < height; my++) { | |
2531 | newOverlapTotal += newMatrix[x][y]; | |
2532 | if (newMatrix[mx][my] > 0) { | |
2533 | newOverlapN++; | |
2534 | } | |
2535 | } | |
2536 | } | |
2537 | double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN; | |
2538 | ||
2539 | if (first) { | |
2540 | // First time: just record what we got. | |
2541 | oldOverlapAvg = newOverlapAvg; | |
2542 | first = false; | |
2543 | } else { | |
2544 | // All other times: pick a new best (x, y) and save the | |
2545 | // overlap value. | |
2546 | if (newOverlapAvg < oldOverlapAvg) { | |
2547 | windowX = x; | |
2548 | windowY = y; | |
2549 | oldOverlapAvg = newOverlapAvg; | |
2550 | } | |
2551 | } | |
2552 | ||
2553 | } // for (int x = xMin; x < xMax; x++) | |
2554 | ||
2555 | } // for (int y = yMin; y < yMax; y++) | |
2556 | ||
2557 | // Finally, set the window's new coordinates. | |
2558 | window.setX(windowX); | |
2559 | window.setY(windowY); | |
2560 | } | |
2561 | ||
a69ed767 | 2562 | // ------------------------------------------------------------------------ |
2ce6dab2 KL |
2563 | // TMenu management ------------------------------------------------------- |
2564 | // ------------------------------------------------------------------------ | |
2565 | ||
fca67db0 KL |
2566 | /** |
2567 | * Check if a mouse event would hit either the active menu or any open | |
2568 | * sub-menus. | |
2569 | * | |
2570 | * @param mouse mouse event | |
2571 | * @return true if the mouse would hit the active menu or an open | |
2572 | * sub-menu | |
2573 | */ | |
2574 | private boolean mouseOnMenu(final TMouseEvent mouse) { | |
2575 | assert (activeMenu != null); | |
a69ed767 | 2576 | List<TMenu> menus = new ArrayList<TMenu>(subMenus); |
fca67db0 KL |
2577 | Collections.reverse(menus); |
2578 | for (TMenu menu: menus) { | |
2579 | if (menu.mouseWouldHit(mouse)) { | |
2580 | return true; | |
2581 | } | |
2582 | } | |
2583 | return activeMenu.mouseWouldHit(mouse); | |
2584 | } | |
2585 | ||
2586 | /** | |
2587 | * See if we need to switch window or activate the menu based on | |
2588 | * a mouse click. | |
2589 | * | |
2590 | * @param mouse mouse event | |
2591 | */ | |
2592 | private void checkSwitchFocus(final TMouseEvent mouse) { | |
2593 | ||
2594 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
2595 | && (activeMenu != null) | |
2596 | && (mouse.getAbsoluteY() != 0) | |
2597 | && (!mouseOnMenu(mouse)) | |
2598 | ) { | |
2599 | // They clicked outside the active menu, turn it off | |
2600 | activeMenu.setActive(false); | |
2601 | activeMenu = null; | |
2602 | for (TMenu menu: subMenus) { | |
2603 | menu.setActive(false); | |
2604 | } | |
2605 | subMenus.clear(); | |
2606 | // Continue checks | |
2607 | } | |
2608 | ||
2609 | // See if they hit the menu bar | |
2610 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) | |
7c870d89 | 2611 | && (mouse.isMouse1()) |
fca67db0 | 2612 | && (!modalWindowActive()) |
9696a8f6 | 2613 | && (!overrideMenuWindowActive()) |
fca67db0 KL |
2614 | && (mouse.getAbsoluteY() == 0) |
2615 | ) { | |
2616 | ||
2617 | for (TMenu menu: subMenus) { | |
2618 | menu.setActive(false); | |
2619 | } | |
2620 | subMenus.clear(); | |
2621 | ||
2622 | // They selected the menu, go activate it | |
2623 | for (TMenu menu: menus) { | |
159f076d KL |
2624 | if ((mouse.getAbsoluteX() >= menu.getTitleX()) |
2625 | && (mouse.getAbsoluteX() < menu.getTitleX() | |
fca67db0 KL |
2626 | + menu.getTitle().length() + 2) |
2627 | ) { | |
2628 | menu.setActive(true); | |
2629 | activeMenu = menu; | |
2630 | } else { | |
2631 | menu.setActive(false); | |
2632 | } | |
2633 | } | |
fca67db0 KL |
2634 | return; |
2635 | } | |
2636 | ||
2637 | // See if they hit the menu bar | |
2638 | if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) | |
7c870d89 | 2639 | && (mouse.isMouse1()) |
fca67db0 KL |
2640 | && (activeMenu != null) |
2641 | && (mouse.getAbsoluteY() == 0) | |
2642 | ) { | |
2643 | ||
2644 | TMenu oldMenu = activeMenu; | |
2645 | for (TMenu menu: subMenus) { | |
2646 | menu.setActive(false); | |
2647 | } | |
2648 | subMenus.clear(); | |
2649 | ||
2650 | // See if we should switch menus | |
2651 | for (TMenu menu: menus) { | |
159f076d KL |
2652 | if ((mouse.getAbsoluteX() >= menu.getTitleX()) |
2653 | && (mouse.getAbsoluteX() < menu.getTitleX() | |
fca67db0 KL |
2654 | + menu.getTitle().length() + 2) |
2655 | ) { | |
2656 | menu.setActive(true); | |
2657 | activeMenu = menu; | |
2658 | } | |
2659 | } | |
2660 | if (oldMenu != activeMenu) { | |
2661 | // They switched menus | |
2662 | oldMenu.setActive(false); | |
2663 | } | |
fca67db0 KL |
2664 | return; |
2665 | } | |
2666 | ||
72fca17b KL |
2667 | // If a menu is still active, don't switch windows |
2668 | if (activeMenu != null) { | |
fca67db0 KL |
2669 | return; |
2670 | } | |
2671 | ||
72fca17b KL |
2672 | // Only switch if there are multiple windows |
2673 | if (windows.size() < 2) { | |
fca67db0 KL |
2674 | return; |
2675 | } | |
2676 | ||
72fca17b KL |
2677 | if (((focusFollowsMouse == true) |
2678 | && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)) | |
e23ea538 | 2679 | || (mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) |
72fca17b KL |
2680 | ) { |
2681 | synchronized (windows) { | |
2682 | Collections.sort(windows); | |
2683 | if (windows.get(0).isModal()) { | |
2684 | // Modal windows don't switch | |
2685 | return; | |
2686 | } | |
fca67db0 | 2687 | |
72fca17b KL |
2688 | for (TWindow window: windows) { |
2689 | assert (!window.isModal()); | |
92453213 | 2690 | |
72fca17b KL |
2691 | if (window.isHidden()) { |
2692 | assert (!window.isActive()); | |
2693 | continue; | |
2694 | } | |
92453213 | 2695 | |
72fca17b KL |
2696 | if (window.mouseWouldHit(mouse)) { |
2697 | if (window == windows.get(0)) { | |
2698 | // Clicked on the same window, nothing to do | |
2699 | assert (window.isActive()); | |
2700 | return; | |
2701 | } | |
2702 | ||
2703 | // We will be switching to another window | |
2704 | assert (windows.get(0).isActive()); | |
2705 | assert (windows.get(0) == activeWindow); | |
2706 | assert (!window.isActive()); | |
a69ed767 KL |
2707 | if (activeWindow != null) { |
2708 | activeWindow.onUnfocus(); | |
2709 | activeWindow.setActive(false); | |
2710 | activeWindow.setZ(window.getZ()); | |
2711 | } | |
72fca17b KL |
2712 | activeWindow = window; |
2713 | window.setZ(0); | |
2714 | window.setActive(true); | |
2715 | window.onFocus(); | |
bb35d919 KL |
2716 | return; |
2717 | } | |
fca67db0 | 2718 | } |
fca67db0 | 2719 | } |
72fca17b KL |
2720 | |
2721 | // Clicked on the background, nothing to do | |
2722 | return; | |
fca67db0 KL |
2723 | } |
2724 | ||
72fca17b KL |
2725 | // Nothing to do: this isn't a mouse up, or focus isn't following |
2726 | // mouse. | |
fca67db0 KL |
2727 | return; |
2728 | } | |
2729 | ||
2730 | /** | |
2731 | * Turn off the menu. | |
2732 | */ | |
928811d8 | 2733 | public final void closeMenu() { |
fca67db0 KL |
2734 | if (activeMenu != null) { |
2735 | activeMenu.setActive(false); | |
2736 | activeMenu = null; | |
2737 | for (TMenu menu: subMenus) { | |
2738 | menu.setActive(false); | |
2739 | } | |
2740 | subMenus.clear(); | |
2741 | } | |
fca67db0 KL |
2742 | } |
2743 | ||
e8a11f98 KL |
2744 | /** |
2745 | * Get a (shallow) copy of the menu list. | |
2746 | * | |
2747 | * @return a copy of the menu list | |
2748 | */ | |
2749 | public final List<TMenu> getAllMenus() { | |
a69ed767 | 2750 | return new ArrayList<TMenu>(menus); |
e8a11f98 KL |
2751 | } |
2752 | ||
2753 | /** | |
2754 | * Add a top-level menu to the list. | |
2755 | * | |
2756 | * @param menu the menu to add | |
2757 | * @throws IllegalArgumentException if the menu is already used in | |
2758 | * another TApplication | |
2759 | */ | |
2760 | public final void addMenu(final TMenu menu) { | |
2761 | if ((menu.getApplication() != null) | |
2762 | && (menu.getApplication() != this) | |
2763 | ) { | |
2764 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
2765 | "part of application " + menu.getApplication()); | |
2766 | } | |
2767 | closeMenu(); | |
2768 | menus.add(menu); | |
2769 | recomputeMenuX(); | |
2770 | } | |
2771 | ||
2772 | /** | |
2773 | * Remove a top-level menu from the list. | |
2774 | * | |
2775 | * @param menu the menu to remove | |
2776 | * @throws IllegalArgumentException if the menu is already used in | |
2777 | * another TApplication | |
2778 | */ | |
2779 | public final void removeMenu(final TMenu menu) { | |
2780 | if ((menu.getApplication() != null) | |
2781 | && (menu.getApplication() != this) | |
2782 | ) { | |
2783 | throw new IllegalArgumentException("Menu " + menu + " is already " + | |
2784 | "part of application " + menu.getApplication()); | |
2785 | } | |
2786 | closeMenu(); | |
2787 | menus.remove(menu); | |
2788 | recomputeMenuX(); | |
2789 | } | |
2790 | ||
fca67db0 KL |
2791 | /** |
2792 | * Turn off a sub-menu. | |
2793 | */ | |
928811d8 | 2794 | public final void closeSubMenu() { |
fca67db0 KL |
2795 | assert (activeMenu != null); |
2796 | TMenu item = subMenus.get(subMenus.size() - 1); | |
2797 | assert (item != null); | |
2798 | item.setActive(false); | |
2799 | subMenus.remove(subMenus.size() - 1); | |
fca67db0 KL |
2800 | } |
2801 | ||
2802 | /** | |
2803 | * Switch to the next menu. | |
2804 | * | |
2805 | * @param forward if true, then switch to the next menu in the list, | |
2806 | * otherwise switch to the previous menu in the list | |
2807 | */ | |
928811d8 | 2808 | public final void switchMenu(final boolean forward) { |
fca67db0 KL |
2809 | assert (activeMenu != null); |
2810 | ||
2811 | for (TMenu menu: subMenus) { | |
2812 | menu.setActive(false); | |
2813 | } | |
2814 | subMenus.clear(); | |
2815 | ||
2816 | for (int i = 0; i < menus.size(); i++) { | |
2817 | if (activeMenu == menus.get(i)) { | |
2818 | if (forward) { | |
2819 | if (i < menus.size() - 1) { | |
2820 | i++; | |
a69ed767 KL |
2821 | } else { |
2822 | i = 0; | |
fca67db0 KL |
2823 | } |
2824 | } else { | |
2825 | if (i > 0) { | |
2826 | i--; | |
a69ed767 KL |
2827 | } else { |
2828 | i = menus.size() - 1; | |
fca67db0 KL |
2829 | } |
2830 | } | |
2831 | activeMenu.setActive(false); | |
2832 | activeMenu = menus.get(i); | |
2833 | activeMenu.setActive(true); | |
fca67db0 KL |
2834 | return; |
2835 | } | |
2836 | } | |
2837 | } | |
2838 | ||
928811d8 | 2839 | /** |
efb7af1f KL |
2840 | * Add a menu item to the global list. If it has a keyboard accelerator, |
2841 | * that will be added the global hash. | |
928811d8 | 2842 | * |
efb7af1f | 2843 | * @param item the menu item |
928811d8 | 2844 | */ |
efb7af1f KL |
2845 | public final void addMenuItem(final TMenuItem item) { |
2846 | menuItems.add(item); | |
2847 | ||
2848 | TKeypress key = item.getKey(); | |
2849 | if (key != null) { | |
2850 | synchronized (accelerators) { | |
2851 | assert (accelerators.get(key) == null); | |
2852 | accelerators.put(key.toLowerCase(), item); | |
2853 | } | |
2854 | } | |
2855 | } | |
2856 | ||
2857 | /** | |
2858 | * Disable one menu item. | |
2859 | * | |
2860 | * @param id the menu item ID | |
2861 | */ | |
2862 | public final void disableMenuItem(final int id) { | |
2863 | for (TMenuItem item: menuItems) { | |
2864 | if (item.getId() == id) { | |
2865 | item.setEnabled(false); | |
2866 | } | |
2867 | } | |
2868 | } | |
e826b451 | 2869 | |
efb7af1f KL |
2870 | /** |
2871 | * Disable the range of menu items with ID's between lower and upper, | |
2872 | * inclusive. | |
2873 | * | |
2874 | * @param lower the lowest menu item ID | |
2875 | * @param upper the highest menu item ID | |
2876 | */ | |
2877 | public final void disableMenuItems(final int lower, final int upper) { | |
2878 | for (TMenuItem item: menuItems) { | |
2879 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
2880 | item.setEnabled(false); | |
a69ed767 | 2881 | item.getParent().activate(0); |
efb7af1f KL |
2882 | } |
2883 | } | |
2884 | } | |
2885 | ||
2886 | /** | |
2887 | * Enable one menu item. | |
2888 | * | |
2889 | * @param id the menu item ID | |
2890 | */ | |
2891 | public final void enableMenuItem(final int id) { | |
2892 | for (TMenuItem item: menuItems) { | |
2893 | if (item.getId() == id) { | |
2894 | item.setEnabled(true); | |
a69ed767 | 2895 | item.getParent().activate(0); |
efb7af1f KL |
2896 | } |
2897 | } | |
2898 | } | |
2899 | ||
2900 | /** | |
2901 | * Enable the range of menu items with ID's between lower and upper, | |
2902 | * inclusive. | |
2903 | * | |
2904 | * @param lower the lowest menu item ID | |
2905 | * @param upper the highest menu item ID | |
2906 | */ | |
2907 | public final void enableMenuItems(final int lower, final int upper) { | |
2908 | for (TMenuItem item: menuItems) { | |
2909 | if ((item.getId() >= lower) && (item.getId() <= upper)) { | |
2910 | item.setEnabled(true); | |
a69ed767 | 2911 | item.getParent().activate(0); |
efb7af1f | 2912 | } |
e826b451 | 2913 | } |
928811d8 KL |
2914 | } |
2915 | ||
77961919 KL |
2916 | /** |
2917 | * Get the menu item associated with this ID. | |
2918 | * | |
2919 | * @param id the menu item ID | |
2920 | * @return the menu item, or null if not found | |
2921 | */ | |
2922 | public final TMenuItem getMenuItem(final int id) { | |
2923 | for (TMenuItem item: menuItems) { | |
2924 | if (item.getId() == id) { | |
2925 | return item; | |
2926 | } | |
2927 | } | |
2928 | return null; | |
2929 | } | |
2930 | ||
928811d8 KL |
2931 | /** |
2932 | * Recompute menu x positions based on their title length. | |
2933 | */ | |
2934 | public final void recomputeMenuX() { | |
2935 | int x = 0; | |
2936 | for (TMenu menu: menus) { | |
2937 | menu.setX(x); | |
159f076d | 2938 | menu.setTitleX(x); |
928811d8 | 2939 | x += menu.getTitle().length() + 2; |
68c5cd6b KL |
2940 | |
2941 | // Don't let the menu window exceed the screen width | |
2942 | int rightEdge = menu.getX() + menu.getWidth(); | |
2943 | if (rightEdge > getScreen().getWidth()) { | |
2944 | menu.setX(getScreen().getWidth() - menu.getWidth()); | |
2945 | } | |
928811d8 KL |
2946 | } |
2947 | } | |
2948 | ||
b2d49e0f KL |
2949 | /** |
2950 | * Post an event to process. | |
2951 | * | |
2952 | * @param event new event to add to the queue | |
2953 | */ | |
2954 | public final void postEvent(final TInputEvent event) { | |
2955 | synchronized (this) { | |
2956 | synchronized (fillEventQueue) { | |
2957 | fillEventQueue.add(event); | |
2958 | } | |
2959 | if (debugThreads) { | |
2960 | System.err.println(System.currentTimeMillis() + " " + | |
2961 | Thread.currentThread() + " postEvent() wake up main"); | |
2962 | } | |
2963 | this.notify(); | |
2964 | } | |
2965 | } | |
2966 | ||
928811d8 KL |
2967 | /** |
2968 | * Post an event to process and turn off the menu. | |
2969 | * | |
2970 | * @param event new event to add to the queue | |
2971 | */ | |
5dfd1c11 | 2972 | public final void postMenuEvent(final TInputEvent event) { |
be72cb5c KL |
2973 | synchronized (this) { |
2974 | synchronized (fillEventQueue) { | |
2975 | fillEventQueue.add(event); | |
2976 | } | |
2977 | if (debugThreads) { | |
2978 | System.err.println(System.currentTimeMillis() + " " + | |
2979 | Thread.currentThread() + " postMenuEvent() wake up main"); | |
2980 | } | |
2981 | closeMenu(); | |
2982 | this.notify(); | |
8e688b92 | 2983 | } |
928811d8 KL |
2984 | } |
2985 | ||
2986 | /** | |
2987 | * Add a sub-menu to the list of open sub-menus. | |
2988 | * | |
2989 | * @param menu sub-menu | |
2990 | */ | |
2991 | public final void addSubMenu(final TMenu menu) { | |
2992 | subMenus.add(menu); | |
2993 | } | |
2994 | ||
8e688b92 KL |
2995 | /** |
2996 | * Convenience function to add a top-level menu. | |
2997 | * | |
2998 | * @param title menu title | |
2999 | * @return the new menu | |
3000 | */ | |
87a17f3c | 3001 | public final TMenu addMenu(final String title) { |
8e688b92 KL |
3002 | int x = 0; |
3003 | int y = 0; | |
3004 | TMenu menu = new TMenu(this, x, y, title); | |
3005 | menus.add(menu); | |
3006 | recomputeMenuX(); | |
3007 | return menu; | |
3008 | } | |
3009 | ||
e23ea538 KL |
3010 | /** |
3011 | * Convenience function to add a default tools (hamburger) menu. | |
3012 | * | |
3013 | * @return the new menu | |
3014 | */ | |
3015 | public final TMenu addToolMenu() { | |
3016 | TMenu toolMenu = addMenu(i18n.getString("toolMenuTitle")); | |
3017 | toolMenu.addDefaultItem(TMenu.MID_REPAINT); | |
3018 | toolMenu.addDefaultItem(TMenu.MID_VIEW_IMAGE); | |
3019 | toolMenu.addDefaultItem(TMenu.MID_CHANGE_FONT); | |
3020 | TStatusBar toolStatusBar = toolMenu.newStatusBar(i18n. | |
3021 | getString("toolMenuStatus")); | |
3022 | toolStatusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
3023 | return toolMenu; | |
3024 | } | |
3025 | ||
8e688b92 KL |
3026 | /** |
3027 | * Convenience function to add a default "File" menu. | |
3028 | * | |
3029 | * @return the new menu | |
3030 | */ | |
3031 | public final TMenu addFileMenu() { | |
339652cc | 3032 | TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle")); |
8e688b92 | 3033 | fileMenu.addDefaultItem(TMenu.MID_SHELL); |
b9724916 | 3034 | fileMenu.addSeparator(); |
8e688b92 | 3035 | fileMenu.addDefaultItem(TMenu.MID_EXIT); |
339652cc KL |
3036 | TStatusBar statusBar = fileMenu.newStatusBar(i18n. |
3037 | getString("fileMenuStatus")); | |
3038 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
3039 | return fileMenu; |
3040 | } | |
3041 | ||
3042 | /** | |
3043 | * Convenience function to add a default "Edit" menu. | |
3044 | * | |
3045 | * @return the new menu | |
3046 | */ | |
3047 | public final TMenu addEditMenu() { | |
339652cc | 3048 | TMenu editMenu = addMenu(i18n.getString("editMenuTitle")); |
8e688b92 KL |
3049 | editMenu.addDefaultItem(TMenu.MID_CUT); |
3050 | editMenu.addDefaultItem(TMenu.MID_COPY); | |
3051 | editMenu.addDefaultItem(TMenu.MID_PASTE); | |
3052 | editMenu.addDefaultItem(TMenu.MID_CLEAR); | |
339652cc KL |
3053 | TStatusBar statusBar = editMenu.newStatusBar(i18n. |
3054 | getString("editMenuStatus")); | |
3055 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
3056 | return editMenu; |
3057 | } | |
3058 | ||
3059 | /** | |
3060 | * Convenience function to add a default "Window" menu. | |
3061 | * | |
3062 | * @return the new menu | |
3063 | */ | |
c6940ed9 | 3064 | public final TMenu addWindowMenu() { |
339652cc | 3065 | TMenu windowMenu = addMenu(i18n.getString("windowMenuTitle")); |
8e688b92 KL |
3066 | windowMenu.addDefaultItem(TMenu.MID_TILE); |
3067 | windowMenu.addDefaultItem(TMenu.MID_CASCADE); | |
3068 | windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL); | |
3069 | windowMenu.addSeparator(); | |
3070 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE); | |
3071 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM); | |
3072 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT); | |
3073 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS); | |
3074 | windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE); | |
339652cc KL |
3075 | TStatusBar statusBar = windowMenu.newStatusBar(i18n. |
3076 | getString("windowMenuStatus")); | |
3077 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
8e688b92 KL |
3078 | return windowMenu; |
3079 | } | |
3080 | ||
55d2b2c2 KL |
3081 | /** |
3082 | * Convenience function to add a default "Help" menu. | |
3083 | * | |
3084 | * @return the new menu | |
3085 | */ | |
3086 | public final TMenu addHelpMenu() { | |
339652cc | 3087 | TMenu helpMenu = addMenu(i18n.getString("helpMenuTitle")); |
55d2b2c2 KL |
3088 | helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS); |
3089 | helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX); | |
3090 | helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH); | |
3091 | helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS); | |
3092 | helpMenu.addDefaultItem(TMenu.MID_HELP_HELP); | |
3093 | helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE); | |
3094 | helpMenu.addSeparator(); | |
3095 | helpMenu.addDefaultItem(TMenu.MID_ABOUT); | |
339652cc KL |
3096 | TStatusBar statusBar = helpMenu.newStatusBar(i18n. |
3097 | getString("helpMenuStatus")); | |
3098 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
55d2b2c2 KL |
3099 | return helpMenu; |
3100 | } | |
3101 | ||
1dac6b8d KL |
3102 | /** |
3103 | * Convenience function to add a default "Table" menu. | |
3104 | * | |
3105 | * @return the new menu | |
3106 | */ | |
3107 | public final TMenu addTableMenu() { | |
3108 | TMenu tableMenu = addMenu(i18n.getString("tableMenuTitle")); | |
2b427404 KL |
3109 | tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_COLUMN, false); |
3110 | tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_ROW, false); | |
3111 | tableMenu.addSeparator(); | |
3112 | ||
77961919 KL |
3113 | TSubMenu viewMenu = tableMenu.addSubMenu(i18n. |
3114 | getString("tableSubMenuView")); | |
3115 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_ROW_LABELS, false); | |
3116 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS, false); | |
3117 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW, false); | |
3118 | viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN, false); | |
3119 | ||
1dac6b8d KL |
3120 | TSubMenu borderMenu = tableMenu.addSubMenu(i18n. |
3121 | getString("tableSubMenuBorders")); | |
3122 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_NONE, false); | |
3123 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_ALL, false); | |
e9bb3c1e KL |
3124 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_NONE, false); |
3125 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_ALL, false); | |
1dac6b8d KL |
3126 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_RIGHT, false); |
3127 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_LEFT, false); | |
3128 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_TOP, false); | |
3129 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_BOTTOM, false); | |
3130 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM, false); | |
3131 | borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM, false); | |
3132 | TSubMenu deleteMenu = tableMenu.addSubMenu(i18n. | |
3133 | getString("tableSubMenuDelete")); | |
3134 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_LEFT, false); | |
3135 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_UP, false); | |
3136 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_ROW, false); | |
3137 | deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_COLUMN, false); | |
3138 | TSubMenu insertMenu = tableMenu.addSubMenu(i18n. | |
3139 | getString("tableSubMenuInsert")); | |
3140 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_LEFT, false); | |
3141 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_RIGHT, false); | |
3142 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_ABOVE, false); | |
3143 | insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_BELOW, false); | |
3144 | TSubMenu columnMenu = tableMenu.addSubMenu(i18n. | |
3145 | getString("tableSubMenuColumn")); | |
3146 | columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_NARROW, false); | |
3147 | columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_WIDEN, false); | |
3148 | TSubMenu fileMenu = tableMenu.addSubMenu(i18n. | |
3149 | getString("tableSubMenuFile")); | |
f528c340 | 3150 | fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_OPEN_CSV, false); |
1dac6b8d KL |
3151 | fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_CSV, false); |
3152 | fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_TEXT, false); | |
3153 | ||
3154 | TStatusBar statusBar = tableMenu.newStatusBar(i18n. | |
3155 | getString("tableMenuStatus")); | |
3156 | statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); | |
3157 | return tableMenu; | |
3158 | } | |
3159 | ||
2ce6dab2 KL |
3160 | // ------------------------------------------------------------------------ |
3161 | // TTimer management ------------------------------------------------------ | |
3162 | // ------------------------------------------------------------------------ | |
3163 | ||
8e688b92 | 3164 | /** |
2ce6dab2 KL |
3165 | * Get the amount of time I can sleep before missing a Timer tick. |
3166 | * | |
3167 | * @param timeout = initial (maximum) timeout in millis | |
3168 | * @return number of milliseconds between now and the next timer event | |
8e688b92 | 3169 | */ |
2ce6dab2 KL |
3170 | private long getSleepTime(final long timeout) { |
3171 | Date now = new Date(); | |
3172 | long nowTime = now.getTime(); | |
3173 | long sleepTime = timeout; | |
2ce6dab2 | 3174 | |
be72cb5c KL |
3175 | synchronized (timers) { |
3176 | for (TTimer timer: timers) { | |
3177 | long nextTickTime = timer.getNextTick().getTime(); | |
3178 | if (nextTickTime < nowTime) { | |
3179 | return 0; | |
3180 | } | |
3181 | ||
3182 | long timeDifference = nextTickTime - nowTime; | |
3183 | if (timeDifference < sleepTime) { | |
3184 | sleepTime = timeDifference; | |
3185 | } | |
8e688b92 KL |
3186 | } |
3187 | } | |
be72cb5c | 3188 | |
2ce6dab2 KL |
3189 | assert (sleepTime >= 0); |
3190 | assert (sleepTime <= timeout); | |
3191 | return sleepTime; | |
8e688b92 KL |
3192 | } |
3193 | ||
d502a0e9 KL |
3194 | /** |
3195 | * Convenience function to add a timer. | |
3196 | * | |
3197 | * @param duration number of milliseconds to wait between ticks | |
3198 | * @param recurring if true, re-schedule this timer after every tick | |
3199 | * @param action function to call when button is pressed | |
c6940ed9 | 3200 | * @return the timer |
d502a0e9 KL |
3201 | */ |
3202 | public final TTimer addTimer(final long duration, final boolean recurring, | |
3203 | final TAction action) { | |
3204 | ||
3205 | TTimer timer = new TTimer(duration, recurring, action); | |
3206 | synchronized (timers) { | |
3207 | timers.add(timer); | |
3208 | } | |
3209 | return timer; | |
3210 | } | |
3211 | ||
3212 | /** | |
3213 | * Convenience function to remove a timer. | |
3214 | * | |
3215 | * @param timer timer to remove | |
3216 | */ | |
3217 | public final void removeTimer(final TTimer timer) { | |
3218 | synchronized (timers) { | |
3219 | timers.remove(timer); | |
3220 | } | |
3221 | } | |
3222 | ||
2ce6dab2 KL |
3223 | // ------------------------------------------------------------------------ |
3224 | // Other TWindow constructors --------------------------------------------- | |
3225 | // ------------------------------------------------------------------------ | |
3226 | ||
c6940ed9 KL |
3227 | /** |
3228 | * Convenience function to spawn a message box. | |
3229 | * | |
3230 | * @param title window title, will be centered along the top border | |
3231 | * @param caption message to display. Use embedded newlines to get a | |
3232 | * multi-line box. | |
3233 | * @return the new message box | |
3234 | */ | |
3235 | public final TMessageBox messageBox(final String title, | |
3236 | final String caption) { | |
3237 | ||
3238 | return new TMessageBox(this, title, caption, TMessageBox.Type.OK); | |
3239 | } | |
3240 | ||
3241 | /** | |
3242 | * Convenience function to spawn a message box. | |
3243 | * | |
3244 | * @param title window title, will be centered along the top border | |
3245 | * @param caption message to display. Use embedded newlines to get a | |
3246 | * multi-line box. | |
3247 | * @param type one of the TMessageBox.Type constants. Default is | |
3248 | * Type.OK. | |
3249 | * @return the new message box | |
3250 | */ | |
3251 | public final TMessageBox messageBox(final String title, | |
3252 | final String caption, final TMessageBox.Type type) { | |
3253 | ||
3254 | return new TMessageBox(this, title, caption, type); | |
3255 | } | |
3256 | ||
3257 | /** | |
3258 | * Convenience function to spawn an input box. | |
3259 | * | |
3260 | * @param title window title, will be centered along the top border | |
3261 | * @param caption message to display. Use embedded newlines to get a | |
3262 | * multi-line box. | |
3263 | * @return the new input box | |
3264 | */ | |
3265 | public final TInputBox inputBox(final String title, final String caption) { | |
3266 | ||
3267 | return new TInputBox(this, title, caption); | |
3268 | } | |
3269 | ||
3270 | /** | |
3271 | * Convenience function to spawn an input box. | |
3272 | * | |
3273 | * @param title window title, will be centered along the top border | |
3274 | * @param caption message to display. Use embedded newlines to get a | |
3275 | * multi-line box. | |
3276 | * @param text initial text to seed the field with | |
3277 | * @return the new input box | |
3278 | */ | |
3279 | public final TInputBox inputBox(final String title, final String caption, | |
3280 | final String text) { | |
3281 | ||
3282 | return new TInputBox(this, title, caption, text); | |
3283 | } | |
1ac2ccb1 | 3284 | |
72b6bd90 KL |
3285 | /** |
3286 | * Convenience function to spawn an input box. | |
3287 | * | |
3288 | * @param title window title, will be centered along the top border | |
3289 | * @param caption message to display. Use embedded newlines to get a | |
3290 | * multi-line box. | |
3291 | * @param text initial text to seed the field with | |
3292 | * @param type one of the Type constants. Default is Type.OK. | |
3293 | * @return the new input box | |
3294 | */ | |
3295 | public final TInputBox inputBox(final String title, final String caption, | |
3296 | final String text, final TInputBox.Type type) { | |
3297 | ||
3298 | return new TInputBox(this, title, caption, text, type); | |
3299 | } | |
3300 | ||
34a42e78 KL |
3301 | /** |
3302 | * Convenience function to open a terminal window. | |
3303 | * | |
3304 | * @param x column relative to parent | |
3305 | * @param y row relative to parent | |
3306 | * @return the terminal new window | |
3307 | */ | |
3308 | public final TTerminalWindow openTerminal(final int x, final int y) { | |
3309 | return openTerminal(x, y, TWindow.RESIZABLE); | |
3310 | } | |
3311 | ||
a69ed767 KL |
3312 | /** |
3313 | * Convenience function to open a terminal window. | |
3314 | * | |
3315 | * @param x column relative to parent | |
3316 | * @param y row relative to parent | |
3317 | * @param closeOnExit if true, close the window when the command exits | |
3318 | * @return the terminal new window | |
3319 | */ | |
3320 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3321 | final boolean closeOnExit) { | |
3322 | ||
3323 | return openTerminal(x, y, TWindow.RESIZABLE, closeOnExit); | |
3324 | } | |
3325 | ||
34a42e78 KL |
3326 | /** |
3327 | * Convenience function to open a terminal window. | |
3328 | * | |
3329 | * @param x column relative to parent | |
3330 | * @param y row relative to parent | |
3331 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3332 | * @return the terminal new window | |
3333 | */ | |
3334 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3335 | final int flags) { | |
3336 | ||
3337 | return new TTerminalWindow(this, x, y, flags); | |
3338 | } | |
3339 | ||
a69ed767 KL |
3340 | /** |
3341 | * Convenience function to open a terminal window. | |
3342 | * | |
3343 | * @param x column relative to parent | |
3344 | * @param y row relative to parent | |
3345 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3346 | * @param closeOnExit if true, close the window when the command exits | |
3347 | * @return the terminal new window | |
3348 | */ | |
3349 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3350 | final int flags, final boolean closeOnExit) { | |
3351 | ||
3352 | return new TTerminalWindow(this, x, y, flags, closeOnExit); | |
3353 | } | |
3354 | ||
6f8ff91a KL |
3355 | /** |
3356 | * Convenience function to open a terminal window and execute a custom | |
3357 | * command line inside it. | |
3358 | * | |
3359 | * @param x column relative to parent | |
3360 | * @param y row relative to parent | |
3361 | * @param commandLine the command line to execute | |
3362 | * @return the terminal new window | |
3363 | */ | |
3364 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3365 | final String commandLine) { | |
3366 | ||
3367 | return openTerminal(x, y, TWindow.RESIZABLE, commandLine); | |
3368 | } | |
3369 | ||
a69ed767 KL |
3370 | /** |
3371 | * Convenience function to open a terminal window and execute a custom | |
3372 | * command line inside it. | |
3373 | * | |
3374 | * @param x column relative to parent | |
3375 | * @param y row relative to parent | |
3376 | * @param commandLine the command line to execute | |
3377 | * @param closeOnExit if true, close the window when the command exits | |
3378 | * @return the terminal new window | |
3379 | */ | |
3380 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3381 | final String commandLine, final boolean closeOnExit) { | |
3382 | ||
3383 | return openTerminal(x, y, TWindow.RESIZABLE, commandLine, closeOnExit); | |
3384 | } | |
3385 | ||
a0d734e6 KL |
3386 | /** |
3387 | * Convenience function to open a terminal window and execute a custom | |
3388 | * command line inside it. | |
3389 | * | |
3390 | * @param x column relative to parent | |
3391 | * @param y row relative to parent | |
3392 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3393 | * @param command the command line to execute | |
3394 | * @return the terminal new window | |
3395 | */ | |
3396 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3397 | final int flags, final String [] command) { | |
3398 | ||
3399 | return new TTerminalWindow(this, x, y, flags, command); | |
3400 | } | |
3401 | ||
a69ed767 KL |
3402 | /** |
3403 | * Convenience function to open a terminal window and execute a custom | |
3404 | * command line inside it. | |
3405 | * | |
3406 | * @param x column relative to parent | |
3407 | * @param y row relative to parent | |
3408 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3409 | * @param command the command line to execute | |
3410 | * @param closeOnExit if true, close the window when the command exits | |
3411 | * @return the terminal new window | |
3412 | */ | |
3413 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3414 | final int flags, final String [] command, final boolean closeOnExit) { | |
3415 | ||
3416 | return new TTerminalWindow(this, x, y, flags, command, closeOnExit); | |
3417 | } | |
3418 | ||
6f8ff91a KL |
3419 | /** |
3420 | * Convenience function to open a terminal window and execute a custom | |
3421 | * command line inside it. | |
3422 | * | |
3423 | * @param x column relative to parent | |
3424 | * @param y row relative to parent | |
3425 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3426 | * @param commandLine the command line to execute | |
3427 | * @return the terminal new window | |
3428 | */ | |
3429 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3430 | final int flags, final String commandLine) { | |
3431 | ||
00691e80 | 3432 | return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+")); |
6f8ff91a KL |
3433 | } |
3434 | ||
a69ed767 KL |
3435 | /** |
3436 | * Convenience function to open a terminal window and execute a custom | |
3437 | * command line inside it. | |
3438 | * | |
3439 | * @param x column relative to parent | |
3440 | * @param y row relative to parent | |
3441 | * @param flags mask of CENTERED, MODAL, or RESIZABLE | |
3442 | * @param commandLine the command line to execute | |
3443 | * @param closeOnExit if true, close the window when the command exits | |
3444 | * @return the terminal new window | |
3445 | */ | |
3446 | public final TTerminalWindow openTerminal(final int x, final int y, | |
3447 | final int flags, final String commandLine, final boolean closeOnExit) { | |
3448 | ||
00691e80 | 3449 | return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+"), |
a69ed767 KL |
3450 | closeOnExit); |
3451 | } | |
3452 | ||
0d47c546 KL |
3453 | /** |
3454 | * Convenience function to spawn an file open box. | |
3455 | * | |
3456 | * @param path path of selected file | |
3457 | * @return the result of the new file open box | |
329fd62e | 3458 | * @throws IOException if java.io operation throws |
0d47c546 KL |
3459 | */ |
3460 | public final String fileOpenBox(final String path) throws IOException { | |
3461 | ||
3462 | TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN); | |
3463 | return box.getFilename(); | |
3464 | } | |
3465 | ||
3466 | /** | |
3467 | * Convenience function to spawn an file open box. | |
3468 | * | |
3469 | * @param path path of selected file | |
3470 | * @param type one of the Type constants | |
3471 | * @return the result of the new file open box | |
329fd62e | 3472 | * @throws IOException if java.io operation throws |
0d47c546 KL |
3473 | */ |
3474 | public final String fileOpenBox(final String path, | |
3475 | final TFileOpenBox.Type type) throws IOException { | |
3476 | ||
3477 | TFileOpenBox box = new TFileOpenBox(this, path, type); | |
3478 | return box.getFilename(); | |
3479 | } | |
3480 | ||
a69ed767 KL |
3481 | /** |
3482 | * Convenience function to spawn a file open box. | |
3483 | * | |
3484 | * @param path path of selected file | |
3485 | * @param type one of the Type constants | |
3486 | * @param filter a string that files must match to be displayed | |
3487 | * @return the result of the new file open box | |
3488 | * @throws IOException of a java.io operation throws | |
3489 | */ | |
3490 | public final String fileOpenBox(final String path, | |
3491 | final TFileOpenBox.Type type, final String filter) throws IOException { | |
3492 | ||
3493 | ArrayList<String> filters = new ArrayList<String>(); | |
3494 | filters.add(filter); | |
3495 | ||
3496 | TFileOpenBox box = new TFileOpenBox(this, path, type, filters); | |
3497 | return box.getFilename(); | |
3498 | } | |
3499 | ||
3500 | /** | |
3501 | * Convenience function to spawn a file open box. | |
3502 | * | |
3503 | * @param path path of selected file | |
3504 | * @param type one of the Type constants | |
3505 | * @param filters a list of strings that files must match to be displayed | |
3506 | * @return the result of the new file open box | |
3507 | * @throws IOException of a java.io operation throws | |
3508 | */ | |
3509 | public final String fileOpenBox(final String path, | |
3510 | final TFileOpenBox.Type type, | |
3511 | final List<String> filters) throws IOException { | |
3512 | ||
3513 | TFileOpenBox box = new TFileOpenBox(this, path, type, filters); | |
3514 | return box.getFilename(); | |
3515 | } | |
3516 | ||
92453213 KL |
3517 | /** |
3518 | * Convenience function to create a new window and make it active. | |
3519 | * Window will be located at (0, 0). | |
3520 | * | |
3521 | * @param title window title, will be centered along the top border | |
3522 | * @param width width of window | |
3523 | * @param height height of window | |
43ad7b6c | 3524 | * @return the new window |
92453213 KL |
3525 | */ |
3526 | public final TWindow addWindow(final String title, final int width, | |
3527 | final int height) { | |
3528 | ||
3529 | TWindow window = new TWindow(this, title, 0, 0, width, height); | |
3530 | return window; | |
3531 | } | |
1978ad50 | 3532 | |
92453213 KL |
3533 | /** |
3534 | * Convenience function to create a new window and make it active. | |
3535 | * Window will be located at (0, 0). | |
3536 | * | |
3537 | * @param title window title, will be centered along the top border | |
3538 | * @param width width of window | |
3539 | * @param height height of window | |
3540 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL | |
43ad7b6c | 3541 | * @return the new window |
92453213 KL |
3542 | */ |
3543 | public final TWindow addWindow(final String title, | |
3544 | final int width, final int height, final int flags) { | |
3545 | ||
3546 | TWindow window = new TWindow(this, title, 0, 0, width, height, flags); | |
3547 | return window; | |
3548 | } | |
3549 | ||
3550 | /** | |
3551 | * Convenience function to create a new window and make it active. | |
3552 | * | |
3553 | * @param title window title, will be centered along the top border | |
3554 | * @param x column relative to parent | |
3555 | * @param y row relative to parent | |
3556 | * @param width width of window | |
3557 | * @param height height of window | |
43ad7b6c | 3558 | * @return the new window |
92453213 KL |
3559 | */ |
3560 | public final TWindow addWindow(final String title, | |
3561 | final int x, final int y, final int width, final int height) { | |
3562 | ||
3563 | TWindow window = new TWindow(this, title, x, y, width, height); | |
3564 | return window; | |
3565 | } | |
3566 | ||
3567 | /** | |
3568 | * Convenience function to create a new window and make it active. | |
3569 | * | |
92453213 KL |
3570 | * @param title window title, will be centered along the top border |
3571 | * @param x column relative to parent | |
3572 | * @param y row relative to parent | |
3573 | * @param width width of window | |
3574 | * @param height height of window | |
3575 | * @param flags mask of RESIZABLE, CENTERED, or MODAL | |
43ad7b6c | 3576 | * @return the new window |
92453213 KL |
3577 | */ |
3578 | public final TWindow addWindow(final String title, | |
3579 | final int x, final int y, final int width, final int height, | |
3580 | final int flags) { | |
3581 | ||
3582 | TWindow window = new TWindow(this, title, x, y, width, height, flags); | |
3583 | return window; | |
3584 | } | |
3585 | ||
7d4115a5 | 3586 | } |