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