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