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