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