Diambiguate ^H from backspace
[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 1625 for (TWindow w: windows) {
3eacc236
KL
1626
1627 // Do not activate a hidden window.
1628 if (w.isHidden()) {
1629 continue;
1630 }
1631
bb35d919
KL
1632 if (w.getZ() > z) {
1633 w.setZ(w.getZ() - 1);
1634 if (w.getZ() == 0) {
1635 w.setActive(true);
efb7af1f 1636 w.onFocus();
bb35d919
KL
1637 assert (activeWindow == null);
1638 activeWindow = w;
1639 } else {
efb7af1f
KL
1640 if (w.isActive()) {
1641 w.setActive(false);
1642 w.onUnfocus();
1643 }
bb35d919 1644 }
48e27807
KL
1645 }
1646 }
1647 }
1648
1649 // Perform window cleanup
1650 window.onClose();
1651
48e27807 1652 // Check if we are closing a TMessageBox or similar
c6940ed9
KL
1653 if (secondaryEventReceiver != null) {
1654 assert (secondaryEventHandler != null);
48e27807
KL
1655
1656 // Do not send events to the secondaryEventReceiver anymore, the
1657 // window is closed.
1658 secondaryEventReceiver = null;
1659
92554d64
KL
1660 // Wake the secondary thread, it will wake the primary as it
1661 // exits.
1662 synchronized (secondaryEventHandler) {
1663 secondaryEventHandler.notify();
48e27807
KL
1664 }
1665 }
92453213
KL
1666
1667 // Permit desktop to be active if it is the only thing left.
1668 if (desktop != null) {
1669 if (windows.size() == 0) {
1670 desktop.setActive(true);
1671 }
1672 }
48e27807
KL
1673 }
1674
1675 /**
1676 * Switch to the next window.
1677 *
1678 * @param forward if true, then switch to the next window in the list,
1679 * otherwise switch to the previous window in the list
1680 */
1681 public final void switchWindow(final boolean forward) {
8c236a98
KL
1682 // Only switch if there are multiple visible windows
1683 if (shownWindowCount() < 2) {
48e27807
KL
1684 return;
1685 }
92453213 1686 assert (activeWindow != null);
48e27807 1687
bb35d919 1688 synchronized (windows) {
fe0770f9
KL
1689 // Whatever window might be moving/dragging, stop it now.
1690 for (TWindow w: windows) {
1691 if (w.inMovements()) {
1692 w.stopMovements();
1693 }
1694 }
bb35d919
KL
1695
1696 // Swap z/active between active window and the next in the list
1697 int activeWindowI = -1;
1698 for (int i = 0; i < windows.size(); i++) {
92453213
KL
1699 if (windows.get(i) == activeWindow) {
1700 assert (activeWindow.isActive());
bb35d919
KL
1701 activeWindowI = i;
1702 break;
92453213
KL
1703 } else {
1704 assert (!windows.get(0).isActive());
bb35d919 1705 }
48e27807 1706 }
bb35d919 1707 assert (activeWindowI >= 0);
48e27807 1708
bb35d919 1709 // Do not switch if a window is modal
92453213 1710 if (activeWindow.isModal()) {
bb35d919
KL
1711 return;
1712 }
48e27807 1713
8c236a98
KL
1714 int nextWindowI = activeWindowI;
1715 for (;;) {
1716 if (forward) {
1717 nextWindowI++;
1718 nextWindowI %= windows.size();
bb35d919 1719 } else {
8c236a98
KL
1720 nextWindowI--;
1721 if (nextWindowI < 0) {
1722 nextWindowI = windows.size() - 1;
1723 }
bb35d919 1724 }
bb35d919 1725
8c236a98
KL
1726 if (windows.get(nextWindowI).isShown()) {
1727 activateWindow(windows.get(nextWindowI));
1728 break;
1729 }
1730 }
bb35d919 1731 } // synchronized (windows)
48e27807 1732
48e27807
KL
1733 }
1734
1735 /**
1736 * Add a window to my window list and make it active.
1737 *
1738 * @param window new window to add
1739 */
1740 public final void addWindow(final TWindow window) {
a7986f7b
KL
1741
1742 // Do not add menu windows to the window list.
1743 if (window instanceof TMenu) {
1744 return;
1745 }
1746
0ee88b6d
KL
1747 // Do not add the desktop to the window list.
1748 if (window instanceof TDesktop) {
1749 return;
1750 }
1751
bb35d919 1752 synchronized (windows) {
fe0770f9
KL
1753 // Whatever window might be moving/dragging, stop it now.
1754 for (TWindow w: windows) {
1755 if (w.inMovements()) {
1756 w.stopMovements();
1757 }
1758 }
1759
2ce6dab2
KL
1760 // Do not allow a modal window to spawn a non-modal window. If a
1761 // modal window is active, then this window will become modal
1762 // too.
1763 if (modalWindowActive()) {
1764 window.flags |= TWindow.MODAL;
a7986f7b 1765 window.flags |= TWindow.CENTERED;
92453213 1766 window.hidden = false;
bb35d919 1767 }
92453213
KL
1768 if (window.isShown()) {
1769 for (TWindow w: windows) {
1770 if (w.isActive()) {
1771 w.setActive(false);
1772 w.onUnfocus();
1773 }
1774 w.setZ(w.getZ() + 1);
efb7af1f 1775 }
bb35d919
KL
1776 }
1777 windows.add(window);
92453213
KL
1778 if (window.isShown()) {
1779 activeWindow = window;
1780 activeWindow.setZ(0);
1781 activeWindow.setActive(true);
1782 activeWindow.onFocus();
1783 }
a7986f7b
KL
1784
1785 if (((window.flags & TWindow.CENTERED) == 0)
1786 && smartWindowPlacement) {
1787
1788 doSmartPlacement(window);
1789 }
48e27807 1790 }
92453213
KL
1791
1792 // Desktop cannot be active over any other window.
1793 if (desktop != null) {
1794 desktop.setActive(false);
1795 }
48e27807
KL
1796 }
1797
fca67db0
KL
1798 /**
1799 * Check if there is a system-modal window on top.
1800 *
1801 * @return true if the active window is modal
1802 */
1803 private boolean modalWindowActive() {
1804 if (windows.size() == 0) {
1805 return false;
1806 }
2ce6dab2
KL
1807
1808 for (TWindow w: windows) {
1809 if (w.isModal()) {
1810 return true;
1811 }
1812 }
1813
1814 return false;
1815 }
1816
1817 /**
1818 * Close all open windows.
1819 */
1820 private void closeAllWindows() {
1821 // Don't do anything if we are in the menu
1822 if (activeMenu != null) {
1823 return;
1824 }
1825 while (windows.size() > 0) {
1826 closeWindow(windows.get(0));
1827 }
fca67db0
KL
1828 }
1829
2ce6dab2
KL
1830 /**
1831 * Re-layout the open windows as non-overlapping tiles. This produces
1832 * almost the same results as Turbo Pascal 7.0's IDE.
1833 */
1834 private void tileWindows() {
1835 synchronized (windows) {
1836 // Don't do anything if we are in the menu
1837 if (activeMenu != null) {
1838 return;
1839 }
1840 int z = windows.size();
1841 if (z == 0) {
1842 return;
1843 }
1844 int a = 0;
1845 int b = 0;
1846 a = (int)(Math.sqrt(z));
1847 int c = 0;
1848 while (c < a) {
1849 b = (z - c) / a;
1850 if (((a * b) + c) == z) {
1851 break;
1852 }
1853 c++;
1854 }
1855 assert (a > 0);
1856 assert (b > 0);
1857 assert (c < a);
1858 int newWidth = (getScreen().getWidth() / a);
1859 int newHeight1 = ((getScreen().getHeight() - 1) / b);
1860 int newHeight2 = ((getScreen().getHeight() - 1) / (b + c));
1861
1862 List<TWindow> sorted = new LinkedList<TWindow>(windows);
1863 Collections.sort(sorted);
1864 Collections.reverse(sorted);
1865 for (int i = 0; i < sorted.size(); i++) {
1866 int logicalX = i / b;
1867 int logicalY = i % b;
1868 if (i >= ((a - 1) * b)) {
1869 logicalX = a - 1;
1870 logicalY = i - ((a - 1) * b);
1871 }
1872
1873 TWindow w = sorted.get(i);
1874 w.setX(logicalX * newWidth);
1875 w.setWidth(newWidth);
1876 if (i >= ((a - 1) * b)) {
1877 w.setY((logicalY * newHeight2) + 1);
1878 w.setHeight(newHeight2);
1879 } else {
1880 w.setY((logicalY * newHeight1) + 1);
1881 w.setHeight(newHeight1);
1882 }
1883 }
1884 }
1885 }
1886
1887 /**
1888 * Re-layout the open windows as overlapping cascaded windows.
1889 */
1890 private void cascadeWindows() {
1891 synchronized (windows) {
1892 // Don't do anything if we are in the menu
1893 if (activeMenu != null) {
1894 return;
1895 }
1896 int x = 0;
1897 int y = 1;
1898 List<TWindow> sorted = new LinkedList<TWindow>(windows);
1899 Collections.sort(sorted);
1900 Collections.reverse(sorted);
1901 for (TWindow window: sorted) {
1902 window.setX(x);
1903 window.setY(y);
1904 x++;
1905 y++;
1906 if (x > getScreen().getWidth()) {
1907 x = 0;
1908 }
1909 if (y >= getScreen().getHeight()) {
1910 y = 1;
1911 }
1912 }
1913 }
1914 }
1915
a7986f7b
KL
1916 /**
1917 * Place a window to minimize its overlap with other windows.
1918 *
1919 * @param window the window to place
1920 */
1921 public final void doSmartPlacement(final TWindow window) {
1922 // This is a pretty dumb algorithm, but seems to work. The hardest
1923 // part is computing these "overlap" values seeking a minimum average
1924 // overlap.
1925 int xMin = 0;
1926 int yMin = desktopTop;
1927 int xMax = getScreen().getWidth() - window.getWidth() + 1;
1928 int yMax = desktopBottom - window.getHeight() + 1;
1929 if (xMax < xMin) {
1930 xMax = xMin;
1931 }
1932 if (yMax < yMin) {
1933 yMax = yMin;
1934 }
1935
1936 if ((xMin == xMax) && (yMin == yMax)) {
1937 // No work to do, bail out.
1938 return;
1939 }
1940
1941 // Compute the overlap matrix without the new window.
1942 int width = getScreen().getWidth();
1943 int height = getScreen().getHeight();
1944 int overlapMatrix[][] = new int[width][height];
1945 for (TWindow w: windows) {
1946 if (window == w) {
1947 continue;
1948 }
1949 for (int x = w.getX(); x < w.getX() + w.getWidth(); x++) {
9ff1c0e3
KL
1950 if (x < 0) {
1951 continue;
1952 }
8c236a98 1953 if (x >= width) {
a7986f7b
KL
1954 continue;
1955 }
1956 for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) {
9ff1c0e3
KL
1957 if (y < 0) {
1958 continue;
1959 }
8c236a98 1960 if (y >= height) {
a7986f7b
KL
1961 continue;
1962 }
1963 overlapMatrix[x][y]++;
1964 }
1965 }
1966 }
1967
1968 long oldOverlapTotal = 0;
1969 long oldOverlapN = 0;
1970 for (int x = 0; x < width; x++) {
1971 for (int y = 0; y < height; y++) {
1972 oldOverlapTotal += overlapMatrix[x][y];
1973 if (overlapMatrix[x][y] > 0) {
1974 oldOverlapN++;
1975 }
1976 }
1977 }
1978
1979
1980 double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN;
1981 boolean first = true;
1982 int windowX = window.getX();
1983 int windowY = window.getY();
1984
1985 // For each possible (x, y) position for the new window, compute a
1986 // new overlap matrix.
1987 for (int x = xMin; x < xMax; x++) {
1988 for (int y = yMin; y < yMax; y++) {
1989
1990 // Start with the matrix minus this window.
1991 int newMatrix[][] = new int[width][height];
1992 for (int mx = 0; mx < width; mx++) {
1993 for (int my = 0; my < height; my++) {
1994 newMatrix[mx][my] = overlapMatrix[mx][my];
1995 }
1996 }
1997
1998 // Add this window's values to the new overlap matrix.
1999 long newOverlapTotal = 0;
2000 long newOverlapN = 0;
2001 // Start by adding each new cell.
2002 for (int wx = x; wx < x + window.getWidth(); wx++) {
8c236a98 2003 if (wx >= width) {
a7986f7b
KL
2004 continue;
2005 }
2006 for (int wy = y; wy < y + window.getHeight(); wy++) {
8c236a98 2007 if (wy >= height) {
a7986f7b
KL
2008 continue;
2009 }
2010 newMatrix[wx][wy]++;
2011 }
2012 }
2013 // Now figure out the new value for total coverage.
2014 for (int mx = 0; mx < width; mx++) {
2015 for (int my = 0; my < height; my++) {
2016 newOverlapTotal += newMatrix[x][y];
2017 if (newMatrix[mx][my] > 0) {
2018 newOverlapN++;
2019 }
2020 }
2021 }
2022 double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN;
2023
2024 if (first) {
2025 // First time: just record what we got.
2026 oldOverlapAvg = newOverlapAvg;
2027 first = false;
2028 } else {
2029 // All other times: pick a new best (x, y) and save the
2030 // overlap value.
2031 if (newOverlapAvg < oldOverlapAvg) {
2032 windowX = x;
2033 windowY = y;
2034 oldOverlapAvg = newOverlapAvg;
2035 }
2036 }
2037
2038 } // for (int x = xMin; x < xMax; x++)
2039
2040 } // for (int y = yMin; y < yMax; y++)
2041
2042 // Finally, set the window's new coordinates.
2043 window.setX(windowX);
2044 window.setY(windowY);
2045 }
2046
2ce6dab2
KL
2047 // ------------------------------------------------------------------------
2048 // TMenu management -------------------------------------------------------
2049 // ------------------------------------------------------------------------
2050
fca67db0
KL
2051 /**
2052 * Check if a mouse event would hit either the active menu or any open
2053 * sub-menus.
2054 *
2055 * @param mouse mouse event
2056 * @return true if the mouse would hit the active menu or an open
2057 * sub-menu
2058 */
2059 private boolean mouseOnMenu(final TMouseEvent mouse) {
2060 assert (activeMenu != null);
2061 List<TMenu> menus = new LinkedList<TMenu>(subMenus);
2062 Collections.reverse(menus);
2063 for (TMenu menu: menus) {
2064 if (menu.mouseWouldHit(mouse)) {
2065 return true;
2066 }
2067 }
2068 return activeMenu.mouseWouldHit(mouse);
2069 }
2070
2071 /**
2072 * See if we need to switch window or activate the menu based on
2073 * a mouse click.
2074 *
2075 * @param mouse mouse event
2076 */
2077 private void checkSwitchFocus(final TMouseEvent mouse) {
2078
2079 if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
2080 && (activeMenu != null)
2081 && (mouse.getAbsoluteY() != 0)
2082 && (!mouseOnMenu(mouse))
2083 ) {
2084 // They clicked outside the active menu, turn it off
2085 activeMenu.setActive(false);
2086 activeMenu = null;
2087 for (TMenu menu: subMenus) {
2088 menu.setActive(false);
2089 }
2090 subMenus.clear();
2091 // Continue checks
2092 }
2093
2094 // See if they hit the menu bar
2095 if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
7c870d89 2096 && (mouse.isMouse1())
fca67db0
KL
2097 && (!modalWindowActive())
2098 && (mouse.getAbsoluteY() == 0)
2099 ) {
2100
2101 for (TMenu menu: subMenus) {
2102 menu.setActive(false);
2103 }
2104 subMenus.clear();
2105
2106 // They selected the menu, go activate it
2107 for (TMenu menu: menus) {
159f076d
KL
2108 if ((mouse.getAbsoluteX() >= menu.getTitleX())
2109 && (mouse.getAbsoluteX() < menu.getTitleX()
fca67db0
KL
2110 + menu.getTitle().length() + 2)
2111 ) {
2112 menu.setActive(true);
2113 activeMenu = menu;
2114 } else {
2115 menu.setActive(false);
2116 }
2117 }
fca67db0
KL
2118 return;
2119 }
2120
2121 // See if they hit the menu bar
2122 if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION)
7c870d89 2123 && (mouse.isMouse1())
fca67db0
KL
2124 && (activeMenu != null)
2125 && (mouse.getAbsoluteY() == 0)
2126 ) {
2127
2128 TMenu oldMenu = activeMenu;
2129 for (TMenu menu: subMenus) {
2130 menu.setActive(false);
2131 }
2132 subMenus.clear();
2133
2134 // See if we should switch menus
2135 for (TMenu menu: menus) {
159f076d
KL
2136 if ((mouse.getAbsoluteX() >= menu.getTitleX())
2137 && (mouse.getAbsoluteX() < menu.getTitleX()
fca67db0
KL
2138 + menu.getTitle().length() + 2)
2139 ) {
2140 menu.setActive(true);
2141 activeMenu = menu;
2142 }
2143 }
2144 if (oldMenu != activeMenu) {
2145 // They switched menus
2146 oldMenu.setActive(false);
2147 }
fca67db0
KL
2148 return;
2149 }
2150
72fca17b
KL
2151 // If a menu is still active, don't switch windows
2152 if (activeMenu != null) {
fca67db0
KL
2153 return;
2154 }
2155
72fca17b
KL
2156 // Only switch if there are multiple windows
2157 if (windows.size() < 2) {
fca67db0
KL
2158 return;
2159 }
2160
72fca17b
KL
2161 if (((focusFollowsMouse == true)
2162 && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION))
2163 || (mouse.getType() == TMouseEvent.Type.MOUSE_UP)
2164 ) {
2165 synchronized (windows) {
2166 Collections.sort(windows);
2167 if (windows.get(0).isModal()) {
2168 // Modal windows don't switch
2169 return;
2170 }
fca67db0 2171
72fca17b
KL
2172 for (TWindow window: windows) {
2173 assert (!window.isModal());
92453213 2174
72fca17b
KL
2175 if (window.isHidden()) {
2176 assert (!window.isActive());
2177 continue;
2178 }
92453213 2179
72fca17b
KL
2180 if (window.mouseWouldHit(mouse)) {
2181 if (window == windows.get(0)) {
2182 // Clicked on the same window, nothing to do
2183 assert (window.isActive());
2184 return;
2185 }
2186
2187 // We will be switching to another window
2188 assert (windows.get(0).isActive());
2189 assert (windows.get(0) == activeWindow);
2190 assert (!window.isActive());
2191 activeWindow.onUnfocus();
2192 activeWindow.setActive(false);
2193 activeWindow.setZ(window.getZ());
2194 activeWindow = window;
2195 window.setZ(0);
2196 window.setActive(true);
2197 window.onFocus();
bb35d919
KL
2198 return;
2199 }
fca67db0 2200 }
fca67db0 2201 }
72fca17b
KL
2202
2203 // Clicked on the background, nothing to do
2204 return;
fca67db0
KL
2205 }
2206
72fca17b
KL
2207 // Nothing to do: this isn't a mouse up, or focus isn't following
2208 // mouse.
fca67db0
KL
2209 return;
2210 }
2211
2212 /**
2213 * Turn off the menu.
2214 */
928811d8 2215 public final void closeMenu() {
fca67db0
KL
2216 if (activeMenu != null) {
2217 activeMenu.setActive(false);
2218 activeMenu = null;
2219 for (TMenu menu: subMenus) {
2220 menu.setActive(false);
2221 }
2222 subMenus.clear();
2223 }
fca67db0
KL
2224 }
2225
e8a11f98
KL
2226 /**
2227 * Get a (shallow) copy of the menu list.
2228 *
2229 * @return a copy of the menu list
2230 */
2231 public final List<TMenu> getAllMenus() {
2232 return new LinkedList<TMenu>(menus);
2233 }
2234
2235 /**
2236 * Add a top-level menu to the list.
2237 *
2238 * @param menu the menu to add
2239 * @throws IllegalArgumentException if the menu is already used in
2240 * another TApplication
2241 */
2242 public final void addMenu(final TMenu menu) {
2243 if ((menu.getApplication() != null)
2244 && (menu.getApplication() != this)
2245 ) {
2246 throw new IllegalArgumentException("Menu " + menu + " is already " +
2247 "part of application " + menu.getApplication());
2248 }
2249 closeMenu();
2250 menus.add(menu);
2251 recomputeMenuX();
2252 }
2253
2254 /**
2255 * Remove a top-level menu from the list.
2256 *
2257 * @param menu the menu to remove
2258 * @throws IllegalArgumentException if the menu is already used in
2259 * another TApplication
2260 */
2261 public final void removeMenu(final TMenu menu) {
2262 if ((menu.getApplication() != null)
2263 && (menu.getApplication() != this)
2264 ) {
2265 throw new IllegalArgumentException("Menu " + menu + " is already " +
2266 "part of application " + menu.getApplication());
2267 }
2268 closeMenu();
2269 menus.remove(menu);
2270 recomputeMenuX();
2271 }
2272
fca67db0
KL
2273 /**
2274 * Turn off a sub-menu.
2275 */
928811d8 2276 public final void closeSubMenu() {
fca67db0
KL
2277 assert (activeMenu != null);
2278 TMenu item = subMenus.get(subMenus.size() - 1);
2279 assert (item != null);
2280 item.setActive(false);
2281 subMenus.remove(subMenus.size() - 1);
fca67db0
KL
2282 }
2283
2284 /**
2285 * Switch to the next menu.
2286 *
2287 * @param forward if true, then switch to the next menu in the list,
2288 * otherwise switch to the previous menu in the list
2289 */
928811d8 2290 public final void switchMenu(final boolean forward) {
fca67db0
KL
2291 assert (activeMenu != null);
2292
2293 for (TMenu menu: subMenus) {
2294 menu.setActive(false);
2295 }
2296 subMenus.clear();
2297
2298 for (int i = 0; i < menus.size(); i++) {
2299 if (activeMenu == menus.get(i)) {
2300 if (forward) {
2301 if (i < menus.size() - 1) {
2302 i++;
2303 }
2304 } else {
2305 if (i > 0) {
2306 i--;
2307 }
2308 }
2309 activeMenu.setActive(false);
2310 activeMenu = menus.get(i);
2311 activeMenu.setActive(true);
fca67db0
KL
2312 return;
2313 }
2314 }
2315 }
2316
928811d8 2317 /**
efb7af1f
KL
2318 * Add a menu item to the global list. If it has a keyboard accelerator,
2319 * that will be added the global hash.
928811d8 2320 *
efb7af1f 2321 * @param item the menu item
928811d8 2322 */
efb7af1f
KL
2323 public final void addMenuItem(final TMenuItem item) {
2324 menuItems.add(item);
2325
2326 TKeypress key = item.getKey();
2327 if (key != null) {
2328 synchronized (accelerators) {
2329 assert (accelerators.get(key) == null);
2330 accelerators.put(key.toLowerCase(), item);
2331 }
2332 }
2333 }
2334
2335 /**
2336 * Disable one menu item.
2337 *
2338 * @param id the menu item ID
2339 */
2340 public final void disableMenuItem(final int id) {
2341 for (TMenuItem item: menuItems) {
2342 if (item.getId() == id) {
2343 item.setEnabled(false);
2344 }
2345 }
2346 }
e826b451 2347
efb7af1f
KL
2348 /**
2349 * Disable the range of menu items with ID's between lower and upper,
2350 * inclusive.
2351 *
2352 * @param lower the lowest menu item ID
2353 * @param upper the highest menu item ID
2354 */
2355 public final void disableMenuItems(final int lower, final int upper) {
2356 for (TMenuItem item: menuItems) {
2357 if ((item.getId() >= lower) && (item.getId() <= upper)) {
2358 item.setEnabled(false);
2359 }
2360 }
2361 }
2362
2363 /**
2364 * Enable one menu item.
2365 *
2366 * @param id the menu item ID
2367 */
2368 public final void enableMenuItem(final int id) {
2369 for (TMenuItem item: menuItems) {
2370 if (item.getId() == id) {
2371 item.setEnabled(true);
2372 }
2373 }
2374 }
2375
2376 /**
2377 * Enable the range of menu items with ID's between lower and upper,
2378 * inclusive.
2379 *
2380 * @param lower the lowest menu item ID
2381 * @param upper the highest menu item ID
2382 */
2383 public final void enableMenuItems(final int lower, final int upper) {
2384 for (TMenuItem item: menuItems) {
2385 if ((item.getId() >= lower) && (item.getId() <= upper)) {
2386 item.setEnabled(true);
2387 }
e826b451 2388 }
928811d8
KL
2389 }
2390
2391 /**
2392 * Recompute menu x positions based on their title length.
2393 */
2394 public final void recomputeMenuX() {
2395 int x = 0;
2396 for (TMenu menu: menus) {
2397 menu.setX(x);
159f076d 2398 menu.setTitleX(x);
928811d8 2399 x += menu.getTitle().length() + 2;
68c5cd6b
KL
2400
2401 // Don't let the menu window exceed the screen width
2402 int rightEdge = menu.getX() + menu.getWidth();
2403 if (rightEdge > getScreen().getWidth()) {
2404 menu.setX(getScreen().getWidth() - menu.getWidth());
2405 }
928811d8
KL
2406 }
2407 }
2408
b2d49e0f
KL
2409 /**
2410 * Post an event to process.
2411 *
2412 * @param event new event to add to the queue
2413 */
2414 public final void postEvent(final TInputEvent event) {
2415 synchronized (this) {
2416 synchronized (fillEventQueue) {
2417 fillEventQueue.add(event);
2418 }
2419 if (debugThreads) {
2420 System.err.println(System.currentTimeMillis() + " " +
2421 Thread.currentThread() + " postEvent() wake up main");
2422 }
2423 this.notify();
2424 }
2425 }
2426
928811d8
KL
2427 /**
2428 * Post an event to process and turn off the menu.
2429 *
2430 * @param event new event to add to the queue
2431 */
5dfd1c11 2432 public final void postMenuEvent(final TInputEvent event) {
be72cb5c
KL
2433 synchronized (this) {
2434 synchronized (fillEventQueue) {
2435 fillEventQueue.add(event);
2436 }
2437 if (debugThreads) {
2438 System.err.println(System.currentTimeMillis() + " " +
2439 Thread.currentThread() + " postMenuEvent() wake up main");
2440 }
2441 closeMenu();
2442 this.notify();
8e688b92 2443 }
928811d8
KL
2444 }
2445
2446 /**
2447 * Add a sub-menu to the list of open sub-menus.
2448 *
2449 * @param menu sub-menu
2450 */
2451 public final void addSubMenu(final TMenu menu) {
2452 subMenus.add(menu);
2453 }
2454
8e688b92
KL
2455 /**
2456 * Convenience function to add a top-level menu.
2457 *
2458 * @param title menu title
2459 * @return the new menu
2460 */
87a17f3c 2461 public final TMenu addMenu(final String title) {
8e688b92
KL
2462 int x = 0;
2463 int y = 0;
2464 TMenu menu = new TMenu(this, x, y, title);
2465 menus.add(menu);
2466 recomputeMenuX();
2467 return menu;
2468 }
2469
2470 /**
2471 * Convenience function to add a default "File" menu.
2472 *
2473 * @return the new menu
2474 */
2475 public final TMenu addFileMenu() {
339652cc 2476 TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle"));
8e688b92
KL
2477 fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE);
2478 fileMenu.addSeparator();
2479 fileMenu.addDefaultItem(TMenu.MID_SHELL);
2480 fileMenu.addDefaultItem(TMenu.MID_EXIT);
339652cc
KL
2481 TStatusBar statusBar = fileMenu.newStatusBar(i18n.
2482 getString("fileMenuStatus"));
2483 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
8e688b92
KL
2484 return fileMenu;
2485 }
2486
2487 /**
2488 * Convenience function to add a default "Edit" menu.
2489 *
2490 * @return the new menu
2491 */
2492 public final TMenu addEditMenu() {
339652cc 2493 TMenu editMenu = addMenu(i18n.getString("editMenuTitle"));
8e688b92
KL
2494 editMenu.addDefaultItem(TMenu.MID_CUT);
2495 editMenu.addDefaultItem(TMenu.MID_COPY);
2496 editMenu.addDefaultItem(TMenu.MID_PASTE);
2497 editMenu.addDefaultItem(TMenu.MID_CLEAR);
339652cc
KL
2498 TStatusBar statusBar = editMenu.newStatusBar(i18n.
2499 getString("editMenuStatus"));
2500 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
8e688b92
KL
2501 return editMenu;
2502 }
2503
2504 /**
2505 * Convenience function to add a default "Window" menu.
2506 *
2507 * @return the new menu
2508 */
c6940ed9 2509 public final TMenu addWindowMenu() {
339652cc 2510 TMenu windowMenu = addMenu(i18n.getString("windowMenuTitle"));
8e688b92
KL
2511 windowMenu.addDefaultItem(TMenu.MID_TILE);
2512 windowMenu.addDefaultItem(TMenu.MID_CASCADE);
2513 windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL);
2514 windowMenu.addSeparator();
2515 windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE);
2516 windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM);
2517 windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT);
2518 windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS);
2519 windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE);
339652cc
KL
2520 TStatusBar statusBar = windowMenu.newStatusBar(i18n.
2521 getString("windowMenuStatus"));
2522 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
8e688b92
KL
2523 return windowMenu;
2524 }
2525
55d2b2c2
KL
2526 /**
2527 * Convenience function to add a default "Help" menu.
2528 *
2529 * @return the new menu
2530 */
2531 public final TMenu addHelpMenu() {
339652cc 2532 TMenu helpMenu = addMenu(i18n.getString("helpMenuTitle"));
55d2b2c2
KL
2533 helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS);
2534 helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX);
2535 helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH);
2536 helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS);
2537 helpMenu.addDefaultItem(TMenu.MID_HELP_HELP);
2538 helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE);
2539 helpMenu.addSeparator();
2540 helpMenu.addDefaultItem(TMenu.MID_ABOUT);
339652cc
KL
2541 TStatusBar statusBar = helpMenu.newStatusBar(i18n.
2542 getString("helpMenuStatus"));
2543 statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
55d2b2c2
KL
2544 return helpMenu;
2545 }
2546
2ce6dab2
KL
2547 // ------------------------------------------------------------------------
2548 // Event handlers ---------------------------------------------------------
2549 // ------------------------------------------------------------------------
2550
8e688b92 2551 /**
2ce6dab2
KL
2552 * Method that TApplication subclasses can override to handle menu or
2553 * posted command events.
2554 *
2555 * @param command command event
2556 * @return if true, this event was consumed
8e688b92 2557 */
2ce6dab2
KL
2558 protected boolean onCommand(final TCommandEvent command) {
2559 // Default: handle cmExit
2560 if (command.equals(cmExit)) {
339652cc
KL
2561 if (messageBox(i18n.getString("exitDialogTitle"),
2562 i18n.getString("exitDialogText"),
2ce6dab2 2563 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
be72cb5c 2564 exit();
2ce6dab2
KL
2565 }
2566 return true;
8e688b92 2567 }
2ce6dab2
KL
2568
2569 if (command.equals(cmShell)) {
2570 openTerminal(0, 0, TWindow.RESIZABLE);
2571 return true;
2572 }
2573
2574 if (command.equals(cmTile)) {
2575 tileWindows();
2576 return true;
2577 }
2578 if (command.equals(cmCascade)) {
2579 cascadeWindows();
2580 return true;
2581 }
2582 if (command.equals(cmCloseAll)) {
2583 closeAllWindows();
2584 return true;
8e688b92 2585 }
2ce6dab2 2586
71a389c9
KL
2587 if (command.equals(cmMenu)) {
2588 if (!modalWindowActive() && (activeMenu == null)) {
2589 if (menus.size() > 0) {
2590 menus.get(0).setActive(true);
2591 activeMenu = menus.get(0);
2592 return true;
2593 }
2594 }
2595 }
2596
2ce6dab2 2597 return false;
8e688b92
KL
2598 }
2599
2600 /**
2ce6dab2
KL
2601 * Method that TApplication subclasses can override to handle menu
2602 * events.
2603 *
2604 * @param menu menu event
2605 * @return if true, this event was consumed
8e688b92 2606 */
2ce6dab2
KL
2607 protected boolean onMenu(final TMenuEvent menu) {
2608
2609 // Default: handle MID_EXIT
2610 if (menu.getId() == TMenu.MID_EXIT) {
339652cc
KL
2611 if (messageBox(i18n.getString("exitDialogTitle"),
2612 i18n.getString("exitDialogText"),
2ce6dab2 2613 TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) {
be72cb5c 2614 exit();
8e688b92 2615 }
2ce6dab2
KL
2616 return true;
2617 }
bb35d919 2618
2ce6dab2
KL
2619 if (menu.getId() == TMenu.MID_SHELL) {
2620 openTerminal(0, 0, TWindow.RESIZABLE);
2621 return true;
2622 }
8e688b92 2623
2ce6dab2
KL
2624 if (menu.getId() == TMenu.MID_TILE) {
2625 tileWindows();
2626 return true;
2627 }
2628 if (menu.getId() == TMenu.MID_CASCADE) {
2629 cascadeWindows();
2630 return true;
2631 }
2632 if (menu.getId() == TMenu.MID_CLOSE_ALL) {
2633 closeAllWindows();
2634 return true;
2635 }
2636 if (menu.getId() == TMenu.MID_ABOUT) {
2637 showAboutDialog();
2638 return true;
2639 }
1c19fdea
KL
2640 if (menu.getId() == TMenu.MID_REPAINT) {
2641 doRepaint();
2642 return true;
2643 }
2ce6dab2
KL
2644 return false;
2645 }
2646
2647 /**
2648 * Method that TApplication subclasses can override to handle keystrokes.
2649 *
2650 * @param keypress keystroke event
2651 * @return if true, this event was consumed
2652 */
2653 protected boolean onKeypress(final TKeypressEvent keypress) {
2654 // Default: only menu shortcuts
2655
2656 // Process Alt-F, Alt-E, etc. menu shortcut keys
2657 if (!keypress.getKey().isFnKey()
2658 && keypress.getKey().isAlt()
2659 && !keypress.getKey().isCtrl()
2660 && (activeMenu == null)
2661 && !modalWindowActive()
2662 ) {
2663
2664 assert (subMenus.size() == 0);
2665
2666 for (TMenu menu: menus) {
2667 if (Character.toLowerCase(menu.getMnemonic().getShortcut())
2668 == Character.toLowerCase(keypress.getKey().getChar())
2669 ) {
2670 activeMenu = menu;
2671 menu.setActive(true);
2672 return true;
bb35d919 2673 }
8e688b92
KL
2674 }
2675 }
2ce6dab2
KL
2676
2677 return false;
8e688b92
KL
2678 }
2679
2ce6dab2
KL
2680 // ------------------------------------------------------------------------
2681 // TTimer management ------------------------------------------------------
2682 // ------------------------------------------------------------------------
2683
8e688b92 2684 /**
2ce6dab2
KL
2685 * Get the amount of time I can sleep before missing a Timer tick.
2686 *
2687 * @param timeout = initial (maximum) timeout in millis
2688 * @return number of milliseconds between now and the next timer event
8e688b92 2689 */
2ce6dab2
KL
2690 private long getSleepTime(final long timeout) {
2691 Date now = new Date();
2692 long nowTime = now.getTime();
2693 long sleepTime = timeout;
2ce6dab2 2694
be72cb5c
KL
2695 synchronized (timers) {
2696 for (TTimer timer: timers) {
2697 long nextTickTime = timer.getNextTick().getTime();
2698 if (nextTickTime < nowTime) {
2699 return 0;
2700 }
2701
2702 long timeDifference = nextTickTime - nowTime;
2703 if (timeDifference < sleepTime) {
2704 sleepTime = timeDifference;
2705 }
8e688b92
KL
2706 }
2707 }
be72cb5c 2708
2ce6dab2
KL
2709 assert (sleepTime >= 0);
2710 assert (sleepTime <= timeout);
2711 return sleepTime;
8e688b92
KL
2712 }
2713
d502a0e9
KL
2714 /**
2715 * Convenience function to add a timer.
2716 *
2717 * @param duration number of milliseconds to wait between ticks
2718 * @param recurring if true, re-schedule this timer after every tick
2719 * @param action function to call when button is pressed
c6940ed9 2720 * @return the timer
d502a0e9
KL
2721 */
2722 public final TTimer addTimer(final long duration, final boolean recurring,
2723 final TAction action) {
2724
2725 TTimer timer = new TTimer(duration, recurring, action);
2726 synchronized (timers) {
2727 timers.add(timer);
2728 }
2729 return timer;
2730 }
2731
2732 /**
2733 * Convenience function to remove a timer.
2734 *
2735 * @param timer timer to remove
2736 */
2737 public final void removeTimer(final TTimer timer) {
2738 synchronized (timers) {
2739 timers.remove(timer);
2740 }
2741 }
2742
2ce6dab2
KL
2743 // ------------------------------------------------------------------------
2744 // Other TWindow constructors ---------------------------------------------
2745 // ------------------------------------------------------------------------
2746
c6940ed9
KL
2747 /**
2748 * Convenience function to spawn a message box.
2749 *
2750 * @param title window title, will be centered along the top border
2751 * @param caption message to display. Use embedded newlines to get a
2752 * multi-line box.
2753 * @return the new message box
2754 */
2755 public final TMessageBox messageBox(final String title,
2756 final String caption) {
2757
2758 return new TMessageBox(this, title, caption, TMessageBox.Type.OK);
2759 }
2760
2761 /**
2762 * Convenience function to spawn a message box.
2763 *
2764 * @param title window title, will be centered along the top border
2765 * @param caption message to display. Use embedded newlines to get a
2766 * multi-line box.
2767 * @param type one of the TMessageBox.Type constants. Default is
2768 * Type.OK.
2769 * @return the new message box
2770 */
2771 public final TMessageBox messageBox(final String title,
2772 final String caption, final TMessageBox.Type type) {
2773
2774 return new TMessageBox(this, title, caption, type);
2775 }
2776
2777 /**
2778 * Convenience function to spawn an input box.
2779 *
2780 * @param title window title, will be centered along the top border
2781 * @param caption message to display. Use embedded newlines to get a
2782 * multi-line box.
2783 * @return the new input box
2784 */
2785 public final TInputBox inputBox(final String title, final String caption) {
2786
2787 return new TInputBox(this, title, caption);
2788 }
2789
2790 /**
2791 * Convenience function to spawn an input box.
2792 *
2793 * @param title window title, will be centered along the top border
2794 * @param caption message to display. Use embedded newlines to get a
2795 * multi-line box.
2796 * @param text initial text to seed the field with
2797 * @return the new input box
2798 */
2799 public final TInputBox inputBox(final String title, final String caption,
2800 final String text) {
2801
2802 return new TInputBox(this, title, caption, text);
2803 }
1ac2ccb1 2804
34a42e78
KL
2805 /**
2806 * Convenience function to open a terminal window.
2807 *
2808 * @param x column relative to parent
2809 * @param y row relative to parent
2810 * @return the terminal new window
2811 */
2812 public final TTerminalWindow openTerminal(final int x, final int y) {
2813 return openTerminal(x, y, TWindow.RESIZABLE);
2814 }
2815
2816 /**
2817 * Convenience function to open a terminal window.
2818 *
2819 * @param x column relative to parent
2820 * @param y row relative to parent
2821 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2822 * @return the terminal new window
2823 */
2824 public final TTerminalWindow openTerminal(final int x, final int y,
2825 final int flags) {
2826
2827 return new TTerminalWindow(this, x, y, flags);
2828 }
2829
6f8ff91a
KL
2830 /**
2831 * Convenience function to open a terminal window and execute a custom
2832 * command line inside it.
2833 *
2834 * @param x column relative to parent
2835 * @param y row relative to parent
2836 * @param commandLine the command line to execute
2837 * @return the terminal new window
2838 */
2839 public final TTerminalWindow openTerminal(final int x, final int y,
2840 final String commandLine) {
2841
2842 return openTerminal(x, y, TWindow.RESIZABLE, commandLine);
2843 }
2844
a0d734e6
KL
2845 /**
2846 * Convenience function to open a terminal window and execute a custom
2847 * command line inside it.
2848 *
2849 * @param x column relative to parent
2850 * @param y row relative to parent
2851 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2852 * @param command the command line to execute
2853 * @return the terminal new window
2854 */
2855 public final TTerminalWindow openTerminal(final int x, final int y,
2856 final int flags, final String [] command) {
2857
2858 return new TTerminalWindow(this, x, y, flags, command);
2859 }
2860
6f8ff91a
KL
2861 /**
2862 * Convenience function to open a terminal window and execute a custom
2863 * command line inside it.
2864 *
2865 * @param x column relative to parent
2866 * @param y row relative to parent
2867 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2868 * @param commandLine the command line to execute
2869 * @return the terminal new window
2870 */
2871 public final TTerminalWindow openTerminal(final int x, final int y,
2872 final int flags, final String commandLine) {
2873
a0d734e6 2874 return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s"));
6f8ff91a
KL
2875 }
2876
0d47c546
KL
2877 /**
2878 * Convenience function to spawn an file open box.
2879 *
2880 * @param path path of selected file
2881 * @return the result of the new file open box
329fd62e 2882 * @throws IOException if java.io operation throws
0d47c546
KL
2883 */
2884 public final String fileOpenBox(final String path) throws IOException {
2885
2886 TFileOpenBox box = new TFileOpenBox(this, path, TFileOpenBox.Type.OPEN);
2887 return box.getFilename();
2888 }
2889
2890 /**
2891 * Convenience function to spawn an file open box.
2892 *
2893 * @param path path of selected file
2894 * @param type one of the Type constants
2895 * @return the result of the new file open box
329fd62e 2896 * @throws IOException if java.io operation throws
0d47c546
KL
2897 */
2898 public final String fileOpenBox(final String path,
2899 final TFileOpenBox.Type type) throws IOException {
2900
2901 TFileOpenBox box = new TFileOpenBox(this, path, type);
2902 return box.getFilename();
2903 }
2904
92453213
KL
2905 /**
2906 * Convenience function to create a new window and make it active.
2907 * Window will be located at (0, 0).
2908 *
2909 * @param title window title, will be centered along the top border
2910 * @param width width of window
2911 * @param height height of window
2912 */
2913 public final TWindow addWindow(final String title, final int width,
2914 final int height) {
2915
2916 TWindow window = new TWindow(this, title, 0, 0, width, height);
2917 return window;
2918 }
1978ad50 2919
92453213
KL
2920 /**
2921 * Convenience function to create a new window and make it active.
2922 * Window will be located at (0, 0).
2923 *
2924 * @param title window title, will be centered along the top border
2925 * @param width width of window
2926 * @param height height of window
2927 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
2928 */
2929 public final TWindow addWindow(final String title,
2930 final int width, final int height, final int flags) {
2931
2932 TWindow window = new TWindow(this, title, 0, 0, width, height, flags);
2933 return window;
2934 }
2935
2936 /**
2937 * Convenience function to create a new window and make it active.
2938 *
2939 * @param title window title, will be centered along the top border
2940 * @param x column relative to parent
2941 * @param y row relative to parent
2942 * @param width width of window
2943 * @param height height of window
2944 */
2945 public final TWindow addWindow(final String title,
2946 final int x, final int y, final int width, final int height) {
2947
2948 TWindow window = new TWindow(this, title, x, y, width, height);
2949 return window;
2950 }
2951
2952 /**
2953 * Convenience function to create a new window and make it active.
2954 *
92453213
KL
2955 * @param title window title, will be centered along the top border
2956 * @param x column relative to parent
2957 * @param y row relative to parent
2958 * @param width width of window
2959 * @param height height of window
2960 * @param flags mask of RESIZABLE, CENTERED, or MODAL
2961 */
2962 public final TWindow addWindow(final String title,
2963 final int x, final int y, final int width, final int height,
2964 final int flags) {
2965
2966 TWindow window = new TWindow(this, title, x, y, width, height, flags);
2967 return window;
2968 }
2969
1978ad50
KL
2970 /**
2971 * Convenience function to open a file in an editor window and make it
2972 * active.
2973 *
2974 * @param file the file to open
2975 * @throws IOException if a java.io operation throws
2976 */
2977 public final TEditorWindow addEditor(final File file) throws IOException {
2978
2979 TEditorWindow editor = new TEditorWindow(this, file);
2980 return editor;
2981 }
2982
7d4115a5 2983}