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