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