Fix lag in TTerminalWindow
[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++) {
8c236a98 1944 if (x >= width) {
a7986f7b
KL
1945 continue;
1946 }
1947 for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) {
8c236a98 1948 if (y >= height) {
a7986f7b
KL
1949 continue;
1950 }
1951 overlapMatrix[x][y]++;
1952 }
1953 }
1954 }
1955
1956 long oldOverlapTotal = 0;
1957 long oldOverlapN = 0;
1958 for (int x = 0; x < width; x++) {
1959 for (int y = 0; y < height; y++) {
1960 oldOverlapTotal += overlapMatrix[x][y];
1961 if (overlapMatrix[x][y] > 0) {
1962 oldOverlapN++;
1963 }
1964 }
1965 }
1966
1967
1968 double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN;
1969 boolean first = true;
1970 int windowX = window.getX();
1971 int windowY = window.getY();
1972
1973 // For each possible (x, y) position for the new window, compute a
1974 // new overlap matrix.
1975 for (int x = xMin; x < xMax; x++) {
1976 for (int y = yMin; y < yMax; y++) {
1977
1978 // Start with the matrix minus this window.
1979 int newMatrix[][] = new int[width][height];
1980 for (int mx = 0; mx < width; mx++) {
1981 for (int my = 0; my < height; my++) {
1982 newMatrix[mx][my] = overlapMatrix[mx][my];
1983 }
1984 }
1985
1986 // Add this window's values to the new overlap matrix.
1987 long newOverlapTotal = 0;
1988 long newOverlapN = 0;
1989 // Start by adding each new cell.
1990 for (int wx = x; wx < x + window.getWidth(); wx++) {
8c236a98 1991 if (wx >= width) {
a7986f7b
KL
1992 continue;
1993 }
1994 for (int wy = y; wy < y + window.getHeight(); wy++) {
8c236a98 1995 if (wy >= height) {
a7986f7b
KL
1996 continue;
1997 }
1998 newMatrix[wx][wy]++;
1999 }
2000 }
2001 // Now figure out the new value for total coverage.
2002 for (int mx = 0; mx < width; mx++) {
2003 for (int my = 0; my < height; my++) {
2004 newOverlapTotal += newMatrix[x][y];
2005 if (newMatrix[mx][my] > 0) {
2006 newOverlapN++;
2007 }
2008 }
2009 }
2010 double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN;
2011
2012 if (first) {
2013 // First time: just record what we got.
2014 oldOverlapAvg = newOverlapAvg;
2015 first = false;
2016 } else {
2017 // All other times: pick a new best (x, y) and save the
2018 // overlap value.
2019 if (newOverlapAvg < oldOverlapAvg) {
2020 windowX = x;
2021 windowY = y;
2022 oldOverlapAvg = newOverlapAvg;
2023 }
2024 }
2025
2026 } // for (int x = xMin; x < xMax; x++)
2027
2028 } // for (int y = yMin; y < yMax; y++)
2029
2030 // Finally, set the window's new coordinates.
2031 window.setX(windowX);
2032 window.setY(windowY);
2033 }
2034
2ce6dab2
KL
2035 // ------------------------------------------------------------------------
2036 // TMenu management -------------------------------------------------------
2037 // ------------------------------------------------------------------------
2038
fca67db0
KL
2039 /**
2040 * Check if a mouse event would hit either the active menu or any open
2041 * sub-menus.
2042 *
2043 * @param mouse mouse event
2044 * @return true if the mouse would hit the active menu or an open
2045 * sub-menu
2046 */
2047 private boolean mouseOnMenu(final TMouseEvent mouse) {
2048 assert (activeMenu != null);
2049 List<TMenu> menus = new LinkedList<TMenu>(subMenus);
2050 Collections.reverse(menus);
2051 for (TMenu menu: menus) {
2052 if (menu.mouseWouldHit(mouse)) {
2053 return true;
2054 }
2055 }
2056 return activeMenu.mouseWouldHit(mouse);
2057 }
2058
2059 /**
2060 * See if we need to switch window or activate the menu based on
2061 * a mouse click.
2062 *
2063 * @param mouse mouse event
2064 */
2065 private void checkSwitchFocus(final TMouseEvent mouse) {
2066
2067 if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
2068 && (activeMenu != null)
2069 && (mouse.getAbsoluteY() != 0)
2070 && (!mouseOnMenu(mouse))
2071 ) {
2072 // They clicked outside the active menu, turn it off
2073 activeMenu.setActive(false);
2074 activeMenu = null;
2075 for (TMenu menu: subMenus) {
2076 menu.setActive(false);
2077 }
2078 subMenus.clear();
2079 // Continue checks
2080 }
2081
2082 // See if they hit the menu bar
2083 if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
7c870d89 2084 && (mouse.isMouse1())
fca67db0
KL
2085 && (!modalWindowActive())
2086 && (mouse.getAbsoluteY() == 0)
2087 ) {
2088
2089 for (TMenu menu: subMenus) {
2090 menu.setActive(false);
2091 }
2092 subMenus.clear();
2093
2094 // They selected the menu, go activate it
2095 for (TMenu menu: menus) {
159f076d
KL
2096 if ((mouse.getAbsoluteX() >= menu.getTitleX())
2097 && (mouse.getAbsoluteX() < menu.getTitleX()
fca67db0
KL
2098 + menu.getTitle().length() + 2)
2099 ) {
2100 menu.setActive(true);
2101 activeMenu = menu;
2102 } else {
2103 menu.setActive(false);
2104 }
2105 }
fca67db0
KL
2106 return;
2107 }
2108
2109 // See if they hit the menu bar
2110 if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)
7c870d89 2111 && (mouse.isMouse1())
fca67db0
KL
2112 && (activeMenu != null)
2113 && (mouse.getAbsoluteY() == 0)
2114 ) {
2115
2116 TMenu oldMenu = activeMenu;
2117 for (TMenu menu: subMenus) {
2118 menu.setActive(false);
2119 }
2120 subMenus.clear();
2121
2122 // See if we should switch menus
2123 for (TMenu menu: menus) {
159f076d
KL
2124 if ((mouse.getAbsoluteX() >= menu.getTitleX())
2125 && (mouse.getAbsoluteX() < menu.getTitleX()
fca67db0
KL
2126 + menu.getTitle().length() + 2)
2127 ) {
2128 menu.setActive(true);
2129 activeMenu = menu;
2130 }
2131 }
2132 if (oldMenu != activeMenu) {
2133 // They switched menus
2134 oldMenu.setActive(false);
2135 }
fca67db0
KL
2136 return;
2137 }
2138
72fca17b
KL
2139 // If a menu is still active, don't switch windows
2140 if (activeMenu != null) {
fca67db0
KL
2141 return;
2142 }
2143
72fca17b
KL
2144 // Only switch if there are multiple windows
2145 if (windows.size() < 2) {
fca67db0
KL
2146 return;
2147 }
2148
72fca17b
KL
2149 if (((focusFollowsMouse == true)
2150 && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION))
2151 || (mouse.getType() == TMouseEvent.Type.MOUSE_UP)
2152 ) {
2153 synchronized (windows) {
2154 Collections.sort(windows);
2155 if (windows.get(0).isModal()) {
2156 // Modal windows don't switch
2157 return;
2158 }
fca67db0 2159
72fca17b
KL
2160 for (TWindow window: windows) {
2161 assert (!window.isModal());
92453213 2162
72fca17b
KL
2163 if (window.isHidden()) {
2164 assert (!window.isActive());
2165 continue;
2166 }
92453213 2167
72fca17b
KL
2168 if (window.mouseWouldHit(mouse)) {
2169 if (window == windows.get(0)) {
2170 // Clicked on the same window, nothing to do
2171 assert (window.isActive());
2172 return;
2173 }
2174
2175 // We will be switching to another window
2176 assert (windows.get(0).isActive());
2177 assert (windows.get(0) == activeWindow);
2178 assert (!window.isActive());
2179 activeWindow.onUnfocus();
2180 activeWindow.setActive(false);
2181 activeWindow.setZ(window.getZ());
2182 activeWindow = window;
2183 window.setZ(0);
2184 window.setActive(true);
2185 window.onFocus();
bb35d919
KL
2186 return;
2187 }
fca67db0 2188 }
fca67db0 2189 }
72fca17b
KL
2190
2191 // Clicked on the background, nothing to do
2192 return;
fca67db0
KL
2193 }
2194
72fca17b
KL
2195 // Nothing to do: this isn't a mouse up, or focus isn't following
2196 // mouse.
fca67db0
KL
2197 return;
2198 }
2199
2200 /**
2201 * Turn off the menu.
2202 */
928811d8 2203 public final void closeMenu() {
fca67db0
KL
2204 if (activeMenu != null) {
2205 activeMenu.setActive(false);
2206 activeMenu = null;
2207 for (TMenu menu: subMenus) {
2208 menu.setActive(false);
2209 }
2210 subMenus.clear();
2211 }
fca67db0
KL
2212 }
2213
e8a11f98
KL
2214 /**
2215 * Get a (shallow) copy of the menu list.
2216 *
2217 * @return a copy of the menu list
2218 */
2219 public final List<TMenu> getAllMenus() {
2220 return new LinkedList<TMenu>(menus);
2221 }
2222
2223 /**
2224 * Add a top-level menu to the list.
2225 *
2226 * @param menu the menu to add
2227 * @throws IllegalArgumentException if the menu is already used in
2228 * another TApplication
2229 */
2230 public final void addMenu(final TMenu menu) {
2231 if ((menu.getApplication() != null)
2232 && (menu.getApplication() != this)
2233 ) {
2234 throw new IllegalArgumentException("Menu " + menu + " is already " +
2235 "part of application " + menu.getApplication());
2236 }
2237 closeMenu();
2238 menus.add(menu);
2239 recomputeMenuX();
2240 }
2241
2242 /**
2243 * Remove a top-level menu from the list.
2244 *
2245 * @param menu the menu to remove
2246 * @throws IllegalArgumentException if the menu is already used in
2247 * another TApplication
2248 */
2249 public final void removeMenu(final TMenu menu) {
2250 if ((menu.getApplication() != null)
2251 && (menu.getApplication() != this)
2252 ) {
2253 throw new IllegalArgumentException("Menu " + menu + " is already " +
2254 "part of application " + menu.getApplication());
2255 }
2256 closeMenu();
2257 menus.remove(menu);
2258 recomputeMenuX();
2259 }
2260
fca67db0
KL
2261 /**
2262 * Turn off a sub-menu.
2263 */
928811d8 2264 public final void closeSubMenu() {
fca67db0
KL
2265 assert (activeMenu != null);
2266 TMenu item = subMenus.get(subMenus.size() - 1);
2267 assert (item != null);
2268 item.setActive(false);
2269 subMenus.remove(subMenus.size() - 1);
fca67db0
KL
2270 }
2271
2272 /**
2273 * Switch to the next menu.
2274 *
2275 * @param forward if true, then switch to the next menu in the list,
2276 * otherwise switch to the previous menu in the list
2277 */
928811d8 2278 public final void switchMenu(final boolean forward) {
fca67db0
KL
2279 assert (activeMenu != null);
2280
2281 for (TMenu menu: subMenus) {
2282 menu.setActive(false);
2283 }
2284 subMenus.clear();
2285
2286 for (int i = 0; i < menus.size(); i++) {
2287 if (activeMenu == menus.get(i)) {
2288 if (forward) {
2289 if (i < menus.size() - 1) {
2290 i++;
2291 }
2292 } else {
2293 if (i > 0) {
2294 i--;
2295 }
2296 }
2297 activeMenu.setActive(false);
2298 activeMenu = menus.get(i);
2299 activeMenu.setActive(true);
fca67db0
KL
2300 return;
2301 }
2302 }
2303 }
2304
928811d8 2305 /**
efb7af1f
KL
2306 * Add a menu item to the global list. If it has a keyboard accelerator,
2307 * that will be added the global hash.
928811d8 2308 *
efb7af1f 2309 * @param item the menu item
928811d8 2310 */
efb7af1f
KL
2311 public final void addMenuItem(final TMenuItem item) {
2312 menuItems.add(item);
2313
2314 TKeypress key = item.getKey();
2315 if (key != null) {
2316 synchronized (accelerators) {
2317 assert (accelerators.get(key) == null);
2318 accelerators.put(key.toLowerCase(), item);
2319 }
2320 }
2321 }
2322
2323 /**
2324 * Disable one menu item.
2325 *
2326 * @param id the menu item ID
2327 */
2328 public final void disableMenuItem(final int id) {
2329 for (TMenuItem item: menuItems) {
2330 if (item.getId() == id) {
2331 item.setEnabled(false);
2332 }
2333 }
2334 }
e826b451 2335
efb7af1f
KL
2336 /**
2337 * Disable the range of menu items with ID's between lower and upper,
2338 * inclusive.
2339 *
2340 * @param lower the lowest menu item ID
2341 * @param upper the highest menu item ID
2342 */
2343 public final void disableMenuItems(final int lower, final int upper) {
2344 for (TMenuItem item: menuItems) {
2345 if ((item.getId() >= lower) && (item.getId() <= upper)) {
2346 item.setEnabled(false);
2347 }
2348 }
2349 }
2350
2351 /**
2352 * Enable one menu item.
2353 *
2354 * @param id the menu item ID
2355 */
2356 public final void enableMenuItem(final int id) {
2357 for (TMenuItem item: menuItems) {
2358 if (item.getId() == id) {
2359 item.setEnabled(true);
2360 }
2361 }
2362 }
2363
2364 /**
2365 * Enable the range of menu items with ID's between lower and upper,
2366 * inclusive.
2367 *
2368 * @param lower the lowest menu item ID
2369 * @param upper the highest menu item ID
2370 */
2371 public final void enableMenuItems(final int lower, final int upper) {
2372 for (TMenuItem item: menuItems) {
2373 if ((item.getId() >= lower) && (item.getId() <= upper)) {
2374 item.setEnabled(true);
2375 }
e826b451 2376 }
928811d8
KL
2377 }
2378
2379 /**
2380 * Recompute menu x positions based on their title length.
2381 */
2382 public final void recomputeMenuX() {
2383 int x = 0;
2384 for (TMenu menu: menus) {
2385 menu.setX(x);
159f076d 2386 menu.setTitleX(x);
928811d8 2387 x += menu.getTitle().length() + 2;
68c5cd6b
KL
2388
2389 // Don't let the menu window exceed the screen width
2390 int rightEdge = menu.getX() + menu.getWidth();
2391 if (rightEdge > getScreen().getWidth()) {
2392 menu.setX(getScreen().getWidth() - menu.getWidth());
2393 }
928811d8
KL
2394 }
2395 }
2396
b2d49e0f
KL
2397 /**
2398 * Post an event to process.
2399 *
2400 * @param event new event to add to the queue
2401 */
2402 public final void postEvent(final TInputEvent event) {
2403 synchronized (this) {
2404 synchronized (fillEventQueue) {
2405 fillEventQueue.add(event);
2406 }
2407 if (debugThreads) {
2408 System.err.println(System.currentTimeMillis() + " " +
2409 Thread.currentThread() + " postEvent() wake up main");
2410 }
2411 this.notify();
2412 }
2413 }
2414
928811d8
KL
2415 /**
2416 * Post an event to process and turn off the menu.
2417 *
2418 * @param event new event to add to the queue
2419 */
5dfd1c11 2420 public final void postMenuEvent(final TInputEvent event) {
be72cb5c
KL
2421 synchronized (this) {
2422 synchronized (fillEventQueue) {
2423 fillEventQueue.add(event);
2424 }
2425 if (debugThreads) {
2426 System.err.println(System.currentTimeMillis() + " " +
2427 Thread.currentThread() + " postMenuEvent() wake up main");
2428 }
2429 closeMenu();
2430 this.notify();
8e688b92 2431 }
928811d8
KL
2432 }
2433
2434 /**
2435 * Add a sub-menu to the list of open sub-menus.
2436 *
2437 * @param menu sub-menu
2438 */
2439 public final void addSubMenu(final TMenu menu) {
2440 subMenus.add(menu);
2441 }
2442
8e688b92
KL
2443 /**
2444 * Convenience function to add a top-level menu.
2445 *
2446 * @param title menu title
2447 * @return the new menu
2448 */
87a17f3c 2449 public final TMenu addMenu(final String title) {
8e688b92
KL
2450 int x = 0;
2451 int y = 0;
2452 TMenu menu = new TMenu(this, x, y, title);
2453 menus.add(menu);
2454 recomputeMenuX();
2455 return menu;
2456 }
2457
2458 /**
2459 * Convenience function to add a default "File" menu.
2460 *
2461 * @return the new menu
2462 */
2463 public final TMenu addFileMenu() {
339652cc 2464 TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle"));
8e688b92
KL
2465 fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE);
2466 fileMenu.addSeparator();
2467 fileMenu.addDefaultItem(TMenu.MID_SHELL);
2468 fileMenu.addDefaultItem(TMenu.MID_EXIT);
339652cc
KL
2469 TStatusBar statusBar = fileMenu.newStatusBar(i18n.
2470 getString("fileMenuStatus"));
2471 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
8e688b92
KL
2472 return fileMenu;
2473 }
2474
2475 /**
2476 * Convenience function to add a default "Edit" menu.
2477 *
2478 * @return the new menu
2479 */
2480 public final TMenu addEditMenu() {
339652cc 2481 TMenu editMenu = addMenu(i18n.getString("editMenuTitle"));
8e688b92
KL
2482 editMenu.addDefaultItem(TMenu.MID_CUT);
2483 editMenu.addDefaultItem(TMenu.MID_COPY);
2484 editMenu.addDefaultItem(TMenu.MID_PASTE);
2485 editMenu.addDefaultItem(TMenu.MID_CLEAR);
339652cc
KL
2486 TStatusBar statusBar = editMenu.newStatusBar(i18n.
2487 getString("editMenuStatus"));
2488 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
8e688b92
KL
2489 return editMenu;
2490 }
2491
2492 /**
2493 * Convenience function to add a default "Window" menu.
2494 *
2495 * @return the new menu
2496 */
c6940ed9 2497 public final TMenu addWindowMenu() {
339652cc 2498 TMenu windowMenu = addMenu(i18n.getString("windowMenuTitle"));
8e688b92
KL
2499 windowMenu.addDefaultItem(TMenu.MID_TILE);
2500 windowMenu.addDefaultItem(TMenu.MID_CASCADE);
2501 windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL);
2502 windowMenu.addSeparator();
2503 windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE);
2504 windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM);
2505 windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT);
2506 windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS);
2507 windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE);
339652cc
KL
2508 TStatusBar statusBar = windowMenu.newStatusBar(i18n.
2509 getString("windowMenuStatus"));
2510 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
8e688b92
KL
2511 return windowMenu;
2512 }
2513
55d2b2c2
KL
2514 /**
2515 * Convenience function to add a default "Help" menu.
2516 *
2517 * @return the new menu
2518 */
2519 public final TMenu addHelpMenu() {
339652cc 2520 TMenu helpMenu = addMenu(i18n.getString("helpMenuTitle"));
55d2b2c2
KL
2521 helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS);
2522 helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX);
2523 helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH);
2524 helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS);
2525 helpMenu.addDefaultItem(TMenu.MID_HELP_HELP);
2526 helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE);
2527 helpMenu.addSeparator();
2528 helpMenu.addDefaultItem(TMenu.MID_ABOUT);
339652cc
KL
2529 TStatusBar statusBar = helpMenu.newStatusBar(i18n.
2530 getString("helpMenuStatus"));
2531 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
55d2b2c2
KL
2532 return helpMenu;
2533 }
2534
2ce6dab2
KL
2535 // ------------------------------------------------------------------------
2536 // Event handlers ---------------------------------------------------------
2537 // ------------------------------------------------------------------------
2538
8e688b92 2539 /**
2ce6dab2
KL
2540 * Method that TApplication subclasses can override to handle menu or
2541 * posted command events.
2542 *
2543 * @param command command event
2544 * @return if true, this event was consumed
8e688b92 2545 */
2ce6dab2
KL
2546 protected boolean onCommand(final TCommandEvent command) {
2547 // Default: handle cmExit
2548 if (command.equals(cmExit)) {
339652cc
KL
2549 if (messageBox(i18n.getString("exitDialogTitle"),
2550 i18n.getString("exitDialogText"),
2ce6dab2 2551 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
be72cb5c 2552 exit();
2ce6dab2
KL
2553 }
2554 return true;
8e688b92 2555 }
2ce6dab2
KL
2556
2557 if (command.equals(cmShell)) {
2558 openTerminal(0, 0, TWindow.RESIZABLE);
2559 return true;
2560 }
2561
2562 if (command.equals(cmTile)) {
2563 tileWindows();
2564 return true;
2565 }
2566 if (command.equals(cmCascade)) {
2567 cascadeWindows();
2568 return true;
2569 }
2570 if (command.equals(cmCloseAll)) {
2571 closeAllWindows();
2572 return true;
8e688b92 2573 }
2ce6dab2 2574
71a389c9
KL
2575 if (command.equals(cmMenu)) {
2576 if (!modalWindowActive() && (activeMenu == null)) {
2577 if (menus.size() > 0) {
2578 menus.get(0).setActive(true);
2579 activeMenu = menus.get(0);
2580 return true;
2581 }
2582 }
2583 }
2584
2ce6dab2 2585 return false;
8e688b92
KL
2586 }
2587
2588 /**
2ce6dab2
KL
2589 * Method that TApplication subclasses can override to handle menu
2590 * events.
2591 *
2592 * @param menu menu event
2593 * @return if true, this event was consumed
8e688b92 2594 */
2ce6dab2
KL
2595 protected boolean onMenu(final TMenuEvent menu) {
2596
2597 // Default: handle MID_EXIT
2598 if (menu.getId() == TMenu.MID_EXIT) {
339652cc
KL
2599 if (messageBox(i18n.getString("exitDialogTitle"),
2600 i18n.getString("exitDialogText"),
2ce6dab2 2601 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
be72cb5c 2602 exit();
8e688b92 2603 }
2ce6dab2
KL
2604 return true;
2605 }
bb35d919 2606
2ce6dab2
KL
2607 if (menu.getId() == TMenu.MID_SHELL) {
2608 openTerminal(0, 0, TWindow.RESIZABLE);
2609 return true;
2610 }
8e688b92 2611
2ce6dab2
KL
2612 if (menu.getId() == TMenu.MID_TILE) {
2613 tileWindows();
2614 return true;
2615 }
2616 if (menu.getId() == TMenu.MID_CASCADE) {
2617 cascadeWindows();
2618 return true;
2619 }
2620 if (menu.getId() == TMenu.MID_CLOSE_ALL) {
2621 closeAllWindows();
2622 return true;
2623 }
2624 if (menu.getId() == TMenu.MID_ABOUT) {
2625 showAboutDialog();
2626 return true;
2627 }
1c19fdea
KL
2628 if (menu.getId() == TMenu.MID_REPAINT) {
2629 doRepaint();
2630 return true;
2631 }
2ce6dab2
KL
2632 return false;
2633 }
2634
2635 /**
2636 * Method that TApplication subclasses can override to handle keystrokes.
2637 *
2638 * @param keypress keystroke event
2639 * @return if true, this event was consumed
2640 */
2641 protected boolean onKeypress(final TKeypressEvent keypress) {
2642 // Default: only menu shortcuts
2643
2644 // Process Alt-F, Alt-E, etc. menu shortcut keys
2645 if (!keypress.getKey().isFnKey()
2646 && keypress.getKey().isAlt()
2647 && !keypress.getKey().isCtrl()
2648 && (activeMenu == null)
2649 && !modalWindowActive()
2650 ) {
2651
2652 assert (subMenus.size() == 0);
2653
2654 for (TMenu menu: menus) {
2655 if (Character.toLowerCase(menu.getMnemonic().getShortcut())
2656 == Character.toLowerCase(keypress.getKey().getChar())
2657 ) {
2658 activeMenu = menu;
2659 menu.setActive(true);
2660 return true;
bb35d919 2661 }
8e688b92
KL
2662 }
2663 }
2ce6dab2
KL
2664
2665 return false;
8e688b92
KL
2666 }
2667
2ce6dab2
KL
2668 // ------------------------------------------------------------------------
2669 // TTimer management ------------------------------------------------------
2670 // ------------------------------------------------------------------------
2671
8e688b92 2672 /**
2ce6dab2
KL
2673 * Get the amount of time I can sleep before missing a Timer tick.
2674 *
2675 * @param timeout = initial (maximum) timeout in millis
2676 * @return number of milliseconds between now and the next timer event
8e688b92 2677 */
2ce6dab2
KL
2678 private long getSleepTime(final long timeout) {
2679 Date now = new Date();
2680 long nowTime = now.getTime();
2681 long sleepTime = timeout;
2ce6dab2 2682
be72cb5c
KL
2683 synchronized (timers) {
2684 for (TTimer timer: timers) {
2685 long nextTickTime = timer.getNextTick().getTime();
2686 if (nextTickTime < nowTime) {
2687 return 0;
2688 }
2689
2690 long timeDifference = nextTickTime - nowTime;
2691 if (timeDifference < sleepTime) {
2692 sleepTime = timeDifference;
2693 }
8e688b92
KL
2694 }
2695 }
be72cb5c 2696
2ce6dab2
KL
2697 assert (sleepTime >= 0);
2698 assert (sleepTime <= timeout);
2699 return sleepTime;
8e688b92
KL
2700 }
2701
d502a0e9
KL
2702 /**
2703 * Convenience function to add a timer.
2704 *
2705 * @param duration number of milliseconds to wait between ticks
2706 * @param recurring if true, re-schedule this timer after every tick
2707 * @param action function to call when button is pressed
c6940ed9 2708 * @return the timer
d502a0e9
KL
2709 */
2710 public final TTimer addTimer(final long duration, final boolean recurring,
2711 final TAction action) {
2712
2713 TTimer timer = new TTimer(duration, recurring, action);
2714 synchronized (timers) {
2715 timers.add(timer);
2716 }
2717 return timer;
2718 }
2719
2720 /**
2721 * Convenience function to remove a timer.
2722 *
2723 * @param timer timer to remove
2724 */
2725 public final void removeTimer(final TTimer timer) {
2726 synchronized (timers) {
2727 timers.remove(timer);
2728 }
2729 }
2730
2ce6dab2
KL
2731 // ------------------------------------------------------------------------
2732 // Other TWindow constructors ---------------------------------------------
2733 // ------------------------------------------------------------------------
2734
c6940ed9
KL
2735 /**
2736 * Convenience function to spawn a message box.
2737 *
2738 * @param title window title, will be centered along the top border
2739 * @param caption message to display. Use embedded newlines to get a
2740 * multi-line box.
2741 * @return the new message box
2742 */
2743 public final TMessageBox messageBox(final String title,
2744 final String caption) {
2745
2746 return new TMessageBox(this, title, caption, TMessageBox.Type.OK);
2747 }
2748
2749 /**
2750 * Convenience function to spawn a message box.
2751 *
2752 * @param title window title, will be centered along the top border
2753 * @param caption message to display. Use embedded newlines to get a
2754 * multi-line box.
2755 * @param type one of the TMessageBox.Type constants. Default is
2756 * Type.OK.
2757 * @return the new message box
2758 */
2759 public final TMessageBox messageBox(final String title,
2760 final String caption, final TMessageBox.Type type) {
2761
2762 return new TMessageBox(this, title, caption, type);
2763 }
2764
2765 /**
2766 * Convenience function to spawn an input box.
2767 *
2768 * @param title window title, will be centered along the top border
2769 * @param caption message to display. Use embedded newlines to get a
2770 * multi-line box.
2771 * @return the new input box
2772 */
2773 public final TInputBox inputBox(final String title, final String caption) {
2774
2775 return new TInputBox(this, title, caption);
2776 }
2777
2778 /**
2779 * Convenience function to spawn an input box.
2780 *
2781 * @param title window title, will be centered along the top border
2782 * @param caption message to display. Use embedded newlines to get a
2783 * multi-line box.
2784 * @param text initial text to seed the field with
2785 * @return the new input box
2786 */
2787 public final TInputBox inputBox(final String title, final String caption,
2788 final String text) {
2789
2790 return new TInputBox(this, title, caption, text);
2791 }
1ac2ccb1 2792
34a42e78
KL
2793 /**
2794 * Convenience function to open a terminal window.
2795 *
2796 * @param x column relative to parent
2797 * @param y row relative to parent
2798 * @return the terminal new window
2799 */
2800 public final TTerminalWindow openTerminal(final int x, final int y) {
2801 return openTerminal(x, y, TWindow.RESIZABLE);
2802 }
2803
2804 /**
2805 * Convenience function to open a terminal window.
2806 *
2807 * @param x column relative to parent
2808 * @param y row relative to parent
2809 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2810 * @return the terminal new window
2811 */
2812 public final TTerminalWindow openTerminal(final int x, final int y,
2813 final int flags) {
2814
2815 return new TTerminalWindow(this, x, y, flags);
2816 }
2817
6f8ff91a
KL
2818 /**
2819 * Convenience function to open a terminal window and execute a custom
2820 * command line inside it.
2821 *
2822 * @param x column relative to parent
2823 * @param y row relative to parent
2824 * @param commandLine the command line to execute
2825 * @return the terminal new window
2826 */
2827 public final TTerminalWindow openTerminal(final int x, final int y,
2828 final String commandLine) {
2829
2830 return openTerminal(x, y, TWindow.RESIZABLE, commandLine);
2831 }
2832
2833 /**
2834 * Convenience function to open a terminal window and execute a custom
2835 * command line inside it.
2836 *
2837 * @param x column relative to parent
2838 * @param y row relative to parent
2839 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2840 * @param commandLine the command line to execute
2841 * @return the terminal new window
2842 */
2843 public final TTerminalWindow openTerminal(final int x, final int y,
2844 final int flags, final String commandLine) {
2845
2846 return new TTerminalWindow(this, x, y, flags, commandLine);
2847 }
2848
0d47c546
KL
2849 /**
2850 * Convenience function to spawn an file open box.
2851 *
2852 * @param path path of selected file
2853 * @return the result of the new file open box
329fd62e 2854 * @throws IOException if java.io operation throws
0d47c546
KL
2855 */
2856 public final String fileOpenBox(final String path) throws IOException {
2857
2858 TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN);
2859 return box.getFilename();
2860 }
2861
2862 /**
2863 * Convenience function to spawn an file open box.
2864 *
2865 * @param path path of selected file
2866 * @param type one of the Type constants
2867 * @return the result of the new file open box
329fd62e 2868 * @throws IOException if java.io operation throws
0d47c546
KL
2869 */
2870 public final String fileOpenBox(final String path,
2871 final TFileOpenBox.Type type) throws IOException {
2872
2873 TFileOpenBox box = new TFileOpenBox(this, path, type);
2874 return box.getFilename();
2875 }
2876
92453213
KL
2877 /**
2878 * Convenience function to create a new window and make it active.
2879 * Window will be located at (0, 0).
2880 *
2881 * @param title window title, will be centered along the top border
2882 * @param width width of window
2883 * @param height height of window
2884 */
2885 public final TWindow addWindow(final String title, final int width,
2886 final int height) {
2887
2888 TWindow window = new TWindow(this, title, 0, 0, width, height);
2889 return window;
2890 }
1978ad50 2891
92453213
KL
2892 /**
2893 * Convenience function to create a new window and make it active.
2894 * Window will be located at (0, 0).
2895 *
2896 * @param title window title, will be centered along the top border
2897 * @param width width of window
2898 * @param height height of window
2899 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
2900 */
2901 public final TWindow addWindow(final String title,
2902 final int width, final int height, final int flags) {
2903
2904 TWindow window = new TWindow(this, title, 0, 0, width, height, flags);
2905 return window;
2906 }
2907
2908 /**
2909 * Convenience function to create a new window and make it active.
2910 *
2911 * @param title window title, will be centered along the top border
2912 * @param x column relative to parent
2913 * @param y row relative to parent
2914 * @param width width of window
2915 * @param height height of window
2916 */
2917 public final TWindow addWindow(final String title,
2918 final int x, final int y, final int width, final int height) {
2919
2920 TWindow window = new TWindow(this, title, x, y, width, height);
2921 return window;
2922 }
2923
2924 /**
2925 * Convenience function to create a new window and make it active.
2926 *
92453213
KL
2927 * @param title window title, will be centered along the top border
2928 * @param x column relative to parent
2929 * @param y row relative to parent
2930 * @param width width of window
2931 * @param height height of window
2932 * @param flags mask of RESIZABLE, CENTERED, or MODAL
2933 */
2934 public final TWindow addWindow(final String title,
2935 final int x, final int y, final int width, final int height,
2936 final int flags) {
2937
2938 TWindow window = new TWindow(this, title, x, y, width, height, flags);
2939 return window;
2940 }
2941
1978ad50
KL
2942 /**
2943 * Convenience function to open a file in an editor window and make it
2944 * active.
2945 *
2946 * @param file the file to open
2947 * @throws IOException if a java.io operation throws
2948 */
2949 public final TEditorWindow addEditor(final File file) throws IOException {
2950
2951 TEditorWindow editor = new TEditorWindow(this, file);
2952 return editor;
2953 }
2954
7d4115a5 2955}