2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
32 import java
.io
.InputStream
;
33 import java
.io
.IOException
;
34 import java
.io
.OutputStream
;
35 import java
.io
.PrintWriter
;
36 import java
.io
.Reader
;
37 import java
.io
.UnsupportedEncodingException
;
38 import java
.text
.MessageFormat
;
39 import java
.util
.Collections
;
40 import java
.util
.Date
;
41 import java
.util
.HashMap
;
42 import java
.util
.ArrayList
;
43 import java
.util
.LinkedList
;
44 import java
.util
.List
;
46 import java
.util
.ResourceBundle
;
48 import jexer
.bits
.CellAttributes
;
49 import jexer
.bits
.ColorTheme
;
50 import jexer
.event
.TCommandEvent
;
51 import jexer
.event
.TInputEvent
;
52 import jexer
.event
.TKeypressEvent
;
53 import jexer
.event
.TMenuEvent
;
54 import jexer
.event
.TMouseEvent
;
55 import jexer
.event
.TResizeEvent
;
56 import jexer
.backend
.Backend
;
57 import jexer
.backend
.Screen
;
58 import jexer
.backend
.MultiBackend
;
59 import jexer
.backend
.SwingBackend
;
60 import jexer
.backend
.ECMA48Backend
;
61 import jexer
.backend
.TWindowBackend
;
62 import jexer
.menu
.TMenu
;
63 import jexer
.menu
.TMenuItem
;
64 import static jexer
.TCommand
.*;
65 import static jexer
.TKeypress
.*;
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.
72 public class TApplication
implements Runnable
{
77 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TApplication
.class.getName());
79 // ------------------------------------------------------------------------
80 // Constants --------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * If true, emit thread stuff to System.err.
86 private static final boolean debugThreads
= false;
89 * If true, emit events being processed to System.err.
91 private static final boolean debugEvents
= false;
94 * If true, do "smart placement" on new windows that are not specified to
97 private static final boolean smartWindowPlacement
= true;
100 * Two backend types are available.
102 public static enum BackendType
{
109 * An ECMA48 / ANSI X3.64 / XTERM style terminal.
114 * Synonym for ECMA48.
119 // ------------------------------------------------------------------------
120 // Variables --------------------------------------------------------------
121 // ------------------------------------------------------------------------
124 * The primary event handler thread.
126 private volatile WidgetEventHandler primaryEventHandler
;
129 * The secondary event handler thread.
131 private volatile WidgetEventHandler secondaryEventHandler
;
134 * The widget receiving events from the secondary event handler thread.
136 private volatile TWidget secondaryEventReceiver
;
139 * Access to the physical screen, keyboard, and mouse.
141 private Backend backend
;
144 * Actual mouse coordinate X.
149 * Actual mouse coordinate Y.
154 * Old version of mouse coordinate X.
156 private int oldMouseX
;
159 * Old version mouse coordinate Y.
161 private int oldMouseY
;
164 * The last mouse up click time, used to determine if this is a mouse
167 private long lastMouseUpTime
;
170 * The amount of millis between mouse up events to assume a double-click.
172 private long doubleClickTime
= 250;
175 * Event queue that is filled by run().
177 private List
<TInputEvent
> fillEventQueue
;
180 * Event queue that will be drained by either primary or secondary
183 private List
<TInputEvent
> drainEventQueue
;
186 * Top-level menus in this application.
188 private List
<TMenu
> menus
;
191 * Stack of activated sub-menus in this application.
193 private List
<TMenu
> subMenus
;
196 * The currently active menu.
198 private TMenu activeMenu
= null;
201 * Active keyboard accelerators.
203 private Map
<TKeypress
, TMenuItem
> accelerators
;
208 private List
<TMenuItem
> menuItems
;
211 * Windows and widgets pull colors from this ColorTheme.
213 private ColorTheme theme
;
216 * The top-level windows (but not menus).
218 private List
<TWindow
> windows
;
221 * The currently acive window.
223 private TWindow activeWindow
= null;
226 * Timers that are being ticked.
228 private List
<TTimer
> timers
;
231 * When true, the application has been started.
233 private volatile boolean started
= false;
236 * When true, exit the application.
238 private volatile boolean quit
= false;
241 * When true, repaint the entire screen.
243 private volatile boolean repaint
= true;
246 * Y coordinate of the top edge of the desktop. For now this is a
247 * constant. Someday it would be nice to have a multi-line menu or
250 private static final int desktopTop
= 1;
253 * Y coordinate of the bottom edge of the desktop.
255 private int desktopBottom
;
258 * An optional TDesktop background window that is drawn underneath
261 private TDesktop desktop
;
264 * If true, focus follows mouse: windows automatically raised if the
265 * mouse passes over them.
267 private boolean focusFollowsMouse
= false;
270 * WidgetEventHandler is the main event consumer loop. There are at most
271 * two such threads in existence: the primary for normal case and a
272 * secondary that is used for TMessageBox, TInputBox, and similar.
274 private class WidgetEventHandler
implements Runnable
{
276 * The main application.
278 private TApplication application
;
281 * Whether or not this WidgetEventHandler is the primary or secondary
284 private boolean primary
= true;
287 * Public constructor.
289 * @param application the main application
290 * @param primary if true, this is the primary event handler thread
292 public WidgetEventHandler(final TApplication application
,
293 final boolean primary
) {
295 this.application
= application
;
296 this.primary
= primary
;
303 boolean first
= true;
306 while (!application
.quit
) {
308 // Wait until application notifies me
309 while (!application
.quit
) {
311 synchronized (application
.drainEventQueue
) {
312 if (application
.drainEventQueue
.size() > 0) {
321 timeout
= application
.getSleepTime(1000);
325 // A timer needs to fire, break out.
330 System
.err
.printf("%d %s %s sleep %d millis\n",
331 System
.currentTimeMillis(), this,
332 primary ?
"primary" : "secondary", timeout
);
335 synchronized (this) {
340 System
.err
.printf("%d %s %s AWAKE\n",
341 System
.currentTimeMillis(), this,
342 primary ?
"primary" : "secondary");
346 && (application
.secondaryEventReceiver
== null)
348 // Secondary thread, emergency exit. If we got
349 // here then something went wrong with the
350 // handoff between yield() and closeWindow().
351 synchronized (application
.primaryEventHandler
) {
352 application
.primaryEventHandler
.notify();
354 application
.secondaryEventHandler
= null;
355 throw new RuntimeException("secondary exited " +
359 } catch (InterruptedException e
) {
362 } // while (!application.quit)
364 // Pull all events off the queue
366 TInputEvent event
= null;
367 synchronized (application
.drainEventQueue
) {
368 if (application
.drainEventQueue
.size() == 0) {
371 event
= application
.drainEventQueue
.remove(0);
374 // We will have an event to process, so repaint the
375 // screen at the end.
376 application
.repaint
= true;
379 primaryHandleEvent(event
);
381 secondaryHandleEvent(event
);
384 && (application
.secondaryEventReceiver
== null)
386 // Secondary thread, time to exit.
388 // DO NOT UNLOCK. Primary thread just came back from
389 // primaryHandleEvent() and will unlock in the else
390 // block below. Just wake it up.
391 synchronized (application
.primaryEventHandler
) {
392 application
.primaryEventHandler
.notify();
394 // Now eliminate my reference so that
395 // wakeEventHandler() resumes working on the primary.
396 application
.secondaryEventHandler
= null;
404 // Fire timers, update screen.
406 application
.finishEventProcessing();
409 } // while (true) (main runnable loop)
413 // ------------------------------------------------------------------------
414 // Constructors -----------------------------------------------------------
415 // ------------------------------------------------------------------------
418 * Public constructor.
420 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
422 * @param windowWidth the number of text columns to start with
423 * @param windowHeight the number of text rows to start with
424 * @param fontSize the size in points
425 * @throws UnsupportedEncodingException if an exception is thrown when
426 * creating the InputStreamReader
428 public TApplication(final BackendType backendType
, final int windowWidth
,
429 final int windowHeight
, final int fontSize
)
430 throws UnsupportedEncodingException
{
432 switch (backendType
) {
434 backend
= new SwingBackend(this, windowWidth
, windowHeight
,
440 backend
= new ECMA48Backend(this, null, null, windowWidth
,
441 windowHeight
, fontSize
);
444 throw new IllegalArgumentException("Invalid backend type: "
451 * Public constructor.
453 * @param backendType BackendType.XTERM, BackendType.ECMA48 or
455 * @throws UnsupportedEncodingException if an exception is thrown when
456 * creating the InputStreamReader
458 public TApplication(final BackendType backendType
)
459 throws UnsupportedEncodingException
{
461 switch (backendType
) {
463 // The default SwingBackend is 80x25, 20 pt font. If you want to
464 // change that, you can pass the extra arguments to the
465 // SwingBackend constructor here. For example, if you wanted
466 // 90x30, 16 pt font:
468 // backend = new SwingBackend(this, 90, 30, 16);
469 backend
= new SwingBackend(this);
474 backend
= new ECMA48Backend(this, null, null);
477 throw new IllegalArgumentException("Invalid backend type: "
484 * Public constructor. The backend type will be BackendType.ECMA48.
486 * @param input an InputStream connected to the remote user, or null for
487 * System.in. If System.in is used, then on non-Windows systems it will
488 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
489 * mode. input is always converted to a Reader with UTF-8 encoding.
490 * @param output an OutputStream connected to the remote user, or null
491 * for System.out. output is always converted to a Writer with UTF-8
493 * @throws UnsupportedEncodingException if an exception is thrown when
494 * creating the InputStreamReader
496 public TApplication(final InputStream input
,
497 final OutputStream output
) throws UnsupportedEncodingException
{
499 backend
= new ECMA48Backend(this, input
, output
);
504 * Public constructor. The backend type will be BackendType.ECMA48.
506 * @param input the InputStream underlying 'reader'. Its available()
507 * method is used to determine if reader.read() will block or not.
508 * @param reader a Reader connected to the remote user.
509 * @param writer a PrintWriter connected to the remote user.
510 * @param setRawMode if true, set System.in into raw mode with stty.
511 * This should in general not be used. It is here solely for Demo3,
512 * which uses System.in.
513 * @throws IllegalArgumentException if input, reader, or writer are null.
515 public TApplication(final InputStream input
, final Reader reader
,
516 final PrintWriter writer
, final boolean setRawMode
) {
518 backend
= new ECMA48Backend(this, input
, reader
, writer
, setRawMode
);
523 * Public constructor. The backend type will be BackendType.ECMA48.
525 * @param input the InputStream underlying 'reader'. Its available()
526 * method is used to determine if reader.read() will block or not.
527 * @param reader a Reader connected to the remote user.
528 * @param writer a PrintWriter connected to the remote user.
529 * @throws IllegalArgumentException if input, reader, or writer are null.
531 public TApplication(final InputStream input
, final Reader reader
,
532 final PrintWriter writer
) {
534 this(input
, reader
, writer
, false);
538 * Public constructor. This hook enables use with new non-Jexer
541 * @param backend a Backend that is already ready to go.
543 public TApplication(final Backend backend
) {
544 this.backend
= backend
;
545 backend
.setListener(this);
550 * Finish construction once the backend is set.
552 private void TApplicationImpl() {
553 theme
= new ColorTheme();
554 desktopBottom
= getScreen().getHeight() - 1;
555 fillEventQueue
= new ArrayList
<TInputEvent
>();
556 drainEventQueue
= new ArrayList
<TInputEvent
>();
557 windows
= new LinkedList
<TWindow
>();
558 menus
= new LinkedList
<TMenu
>();
559 subMenus
= new LinkedList
<TMenu
>();
560 timers
= new LinkedList
<TTimer
>();
561 accelerators
= new HashMap
<TKeypress
, TMenuItem
>();
562 menuItems
= new ArrayList
<TMenuItem
>();
563 desktop
= new TDesktop(this);
565 // Special case: the Swing backend needs to have a timer to drive its
567 if ((backend
instanceof SwingBackend
)
568 || (backend
instanceof MultiBackend
)
570 // Default to 500 millis, unless a SwingBackend has its own
573 if (backend
instanceof SwingBackend
) {
574 millis
= ((SwingBackend
) backend
).getBlinkMillis();
577 addTimer(millis
, true,
580 TApplication
.this.doRepaint();
588 // ------------------------------------------------------------------------
589 // Runnable ---------------------------------------------------------------
590 // ------------------------------------------------------------------------
593 * Run this application until it exits.
596 // Start the main consumer thread
597 primaryEventHandler
= new WidgetEventHandler(this, true);
598 (new Thread(primaryEventHandler
)).start();
603 synchronized (this) {
604 boolean doWait
= false;
606 if (!backend
.hasEvents()) {
607 synchronized (fillEventQueue
) {
608 if (fillEventQueue
.size() == 0) {
615 // No I/O to dispatch, so wait until the backend
619 System
.err
.println(System
.currentTimeMillis() +
626 System
.err
.println(System
.currentTimeMillis() +
629 } catch (InterruptedException e
) {
630 // I'm awake and don't care why, let's see what's
631 // going on out there.
635 } // synchronized (this)
637 synchronized (fillEventQueue
) {
638 // Pull any pending I/O events
639 backend
.getEvents(fillEventQueue
);
641 // Dispatch each event to the appropriate handler, one at a
644 TInputEvent event
= null;
645 if (fillEventQueue
.size() == 0) {
648 event
= fillEventQueue
.remove(0);
649 metaHandleEvent(event
);
653 // Wake a consumer thread if we have any pending events.
654 if (drainEventQueue
.size() > 0) {
660 // Shutdown the event consumer threads
661 if (secondaryEventHandler
!= null) {
662 synchronized (secondaryEventHandler
) {
663 secondaryEventHandler
.notify();
666 if (primaryEventHandler
!= null) {
667 synchronized (primaryEventHandler
) {
668 primaryEventHandler
.notify();
672 // Shutdown the user I/O thread(s)
675 // Close all the windows. This gives them an opportunity to release
681 // ------------------------------------------------------------------------
682 // Event handlers ---------------------------------------------------------
683 // ------------------------------------------------------------------------
686 * Method that TApplication subclasses can override to handle menu or
687 * posted command events.
689 * @param command command event
690 * @return if true, this event was consumed
692 protected boolean onCommand(final TCommandEvent command
) {
693 // Default: handle cmExit
694 if (command
.equals(cmExit
)) {
695 if (messageBox(i18n
.getString("exitDialogTitle"),
696 i18n
.getString("exitDialogText"),
697 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
703 if (command
.equals(cmShell
)) {
704 openTerminal(0, 0, TWindow
.RESIZABLE
);
708 if (command
.equals(cmTile
)) {
712 if (command
.equals(cmCascade
)) {
716 if (command
.equals(cmCloseAll
)) {
721 if (command
.equals(cmMenu
)) {
722 if (!modalWindowActive() && (activeMenu
== null)) {
723 if (menus
.size() > 0) {
724 menus
.get(0).setActive(true);
725 activeMenu
= menus
.get(0);
735 * Method that TApplication subclasses can override to handle menu
738 * @param menu menu event
739 * @return if true, this event was consumed
741 protected boolean onMenu(final TMenuEvent menu
) {
743 // Default: handle MID_EXIT
744 if (menu
.getId() == TMenu
.MID_EXIT
) {
745 if (messageBox(i18n
.getString("exitDialogTitle"),
746 i18n
.getString("exitDialogText"),
747 TMessageBox
.Type
.YESNO
).getResult() == TMessageBox
.Result
.YES
) {
753 if (menu
.getId() == TMenu
.MID_SHELL
) {
754 openTerminal(0, 0, TWindow
.RESIZABLE
);
758 if (menu
.getId() == TMenu
.MID_TILE
) {
762 if (menu
.getId() == TMenu
.MID_CASCADE
) {
766 if (menu
.getId() == TMenu
.MID_CLOSE_ALL
) {
770 if (menu
.getId() == TMenu
.MID_ABOUT
) {
774 if (menu
.getId() == TMenu
.MID_REPAINT
) {
782 * Method that TApplication subclasses can override to handle keystrokes.
784 * @param keypress keystroke event
785 * @return if true, this event was consumed
787 protected boolean onKeypress(final TKeypressEvent keypress
) {
788 // Default: only menu shortcuts
790 // Process Alt-F, Alt-E, etc. menu shortcut keys
791 if (!keypress
.getKey().isFnKey()
792 && keypress
.getKey().isAlt()
793 && !keypress
.getKey().isCtrl()
794 && (activeMenu
== null)
795 && !modalWindowActive()
798 assert (subMenus
.size() == 0);
800 for (TMenu menu
: menus
) {
801 if (Character
.toLowerCase(menu
.getMnemonic().getShortcut())
802 == Character
.toLowerCase(keypress
.getKey().getChar())
805 menu
.setActive(true);
815 * Process background events, and update the screen.
817 private void finishEventProcessing() {
819 System
.err
.printf(System
.currentTimeMillis() + " " +
820 Thread
.currentThread() + " finishEventProcessing()\n");
823 // Process timers and call doIdle()'s
827 synchronized (getScreen()) {
832 System
.err
.printf(System
.currentTimeMillis() + " " +
833 Thread
.currentThread() + " finishEventProcessing() END\n");
838 * Peek at certain application-level events, add to eventQueue, and wake
839 * up the consuming Thread.
841 * @param event the input event to consume
843 private void metaHandleEvent(final TInputEvent event
) {
846 System
.err
.printf(String
.format("metaHandleEvents event: %s\n",
847 event
)); System
.err
.flush();
851 // Do no more processing if the application is already trying
856 // Special application-wide events -------------------------------
859 if (event
instanceof TCommandEvent
) {
860 TCommandEvent command
= (TCommandEvent
) event
;
861 if (command
.getCmd().equals(cmAbort
)) {
867 synchronized (drainEventQueue
) {
869 if (event
instanceof TResizeEvent
) {
870 TResizeEvent resize
= (TResizeEvent
) event
;
871 synchronized (getScreen()) {
872 getScreen().setDimensions(resize
.getWidth(),
874 desktopBottom
= getScreen().getHeight() - 1;
880 if (desktop
!= null) {
881 desktop
.setDimensions(0, 0, resize
.getWidth(),
882 resize
.getHeight() - 1);
885 // Change menu edges if needed.
888 // We are dirty, redraw the screen.
892 System.err.println("New screen: " + resize.getWidth() +
893 " x " + resize.getHeight());
898 // Put into the main queue
899 drainEventQueue
.add(event
);
904 * Dispatch one event to the appropriate widget or application-level
905 * event handler. This is the primary event handler, it has the normal
906 * application-wide event handling.
908 * @param event the input event to consume
909 * @see #secondaryHandleEvent(TInputEvent event)
911 private void primaryHandleEvent(final TInputEvent event
) {
914 System
.err
.printf("Handle event: %s\n", event
);
916 TMouseEvent doubleClick
= null;
918 // Special application-wide events -----------------------------------
920 // Peek at the mouse position
921 if (event
instanceof TMouseEvent
) {
922 TMouseEvent mouse
= (TMouseEvent
) event
;
923 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
926 mouseX
= mouse
.getX();
927 mouseY
= mouse
.getY();
929 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
930 if ((mouse
.getTime().getTime() - lastMouseUpTime
) <
933 // This is a double-click.
934 doubleClick
= new TMouseEvent(TMouseEvent
.Type
.
936 mouse
.getX(), mouse
.getY(),
937 mouse
.getAbsoluteX(), mouse
.getAbsoluteY(),
938 mouse
.isMouse1(), mouse
.isMouse2(),
940 mouse
.isMouseWheelUp(), mouse
.isMouseWheelDown());
943 // The first click of a potential double-click.
944 lastMouseUpTime
= mouse
.getTime().getTime();
949 // See if we need to switch focus to another window or the menu
950 checkSwitchFocus((TMouseEvent
) event
);
953 // Handle menu events
954 if ((activeMenu
!= null) && !(event
instanceof TCommandEvent
)) {
955 TMenu menu
= activeMenu
;
957 if (event
instanceof TMouseEvent
) {
958 TMouseEvent mouse
= (TMouseEvent
) event
;
960 while (subMenus
.size() > 0) {
961 TMenu subMenu
= subMenus
.get(subMenus
.size() - 1);
962 if (subMenu
.mouseWouldHit(mouse
)) {
965 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
966 && (!mouse
.isMouse1())
967 && (!mouse
.isMouse2())
968 && (!mouse
.isMouse3())
969 && (!mouse
.isMouseWheelUp())
970 && (!mouse
.isMouseWheelDown())
974 // We navigated away from a sub-menu, so close it
978 // Convert the mouse relative x/y to menu coordinates
979 assert (mouse
.getX() == mouse
.getAbsoluteX());
980 assert (mouse
.getY() == mouse
.getAbsoluteY());
981 if (subMenus
.size() > 0) {
982 menu
= subMenus
.get(subMenus
.size() - 1);
984 mouse
.setX(mouse
.getX() - menu
.getX());
985 mouse
.setY(mouse
.getY() - menu
.getY());
987 menu
.handleEvent(event
);
991 if (event
instanceof TKeypressEvent
) {
992 TKeypressEvent keypress
= (TKeypressEvent
) event
;
994 // See if this key matches an accelerator, and is not being
995 // shortcutted by the active window, and if so dispatch the menu
997 boolean windowWillShortcut
= false;
998 if (activeWindow
!= null) {
999 assert (activeWindow
.isShown());
1000 if (activeWindow
.isShortcutKeypress(keypress
.getKey())) {
1001 // We do not process this key, it will be passed to the
1003 windowWillShortcut
= true;
1007 if (!windowWillShortcut
&& !modalWindowActive()) {
1008 TKeypress keypressLowercase
= keypress
.getKey().toLowerCase();
1009 TMenuItem item
= null;
1010 synchronized (accelerators
) {
1011 item
= accelerators
.get(keypressLowercase
);
1014 if (item
.isEnabled()) {
1015 // Let the menu item dispatch
1021 // Handle the keypress
1022 if (onKeypress(keypress
)) {
1028 if (event
instanceof TCommandEvent
) {
1029 if (onCommand((TCommandEvent
) event
)) {
1034 if (event
instanceof TMenuEvent
) {
1035 if (onMenu((TMenuEvent
) event
)) {
1040 // Dispatch events to the active window -------------------------------
1041 boolean dispatchToDesktop
= true;
1042 TWindow window
= activeWindow
;
1043 if (window
!= null) {
1044 assert (window
.isActive());
1045 assert (window
.isShown());
1046 if (event
instanceof TMouseEvent
) {
1047 TMouseEvent mouse
= (TMouseEvent
) event
;
1048 // Convert the mouse relative x/y to window coordinates
1049 assert (mouse
.getX() == mouse
.getAbsoluteX());
1050 assert (mouse
.getY() == mouse
.getAbsoluteY());
1051 mouse
.setX(mouse
.getX() - window
.getX());
1052 mouse
.setY(mouse
.getY() - window
.getY());
1054 if (doubleClick
!= null) {
1055 doubleClick
.setX(doubleClick
.getX() - window
.getX());
1056 doubleClick
.setY(doubleClick
.getY() - window
.getY());
1059 if (window
.mouseWouldHit(mouse
)) {
1060 dispatchToDesktop
= false;
1062 } else if (event
instanceof TKeypressEvent
) {
1063 dispatchToDesktop
= false;
1067 System
.err
.printf("TApplication dispatch event: %s\n",
1070 window
.handleEvent(event
);
1071 if (doubleClick
!= null) {
1072 window
.handleEvent(doubleClick
);
1075 if (dispatchToDesktop
) {
1076 // This event is fair game for the desktop to process.
1077 if (desktop
!= null) {
1078 desktop
.handleEvent(event
);
1079 if (doubleClick
!= null) {
1080 desktop
.handleEvent(doubleClick
);
1087 * Dispatch one event to the appropriate widget or application-level
1088 * event handler. This is the secondary event handler used by certain
1089 * special dialogs (currently TMessageBox and TFileOpenBox).
1091 * @param event the input event to consume
1092 * @see #primaryHandleEvent(TInputEvent event)
1094 private void secondaryHandleEvent(final TInputEvent event
) {
1095 TMouseEvent doubleClick
= null;
1097 // Peek at the mouse position
1098 if (event
instanceof TMouseEvent
) {
1099 TMouseEvent mouse
= (TMouseEvent
) event
;
1100 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
1103 mouseX
= mouse
.getX();
1104 mouseY
= mouse
.getY();
1106 if (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
) {
1107 if ((mouse
.getTime().getTime() - lastMouseUpTime
) <
1110 // This is a double-click.
1111 doubleClick
= new TMouseEvent(TMouseEvent
.Type
.
1113 mouse
.getX(), mouse
.getY(),
1114 mouse
.getAbsoluteX(), mouse
.getAbsoluteY(),
1115 mouse
.isMouse1(), mouse
.isMouse2(),
1117 mouse
.isMouseWheelUp(), mouse
.isMouseWheelDown());
1120 // The first click of a potential double-click.
1121 lastMouseUpTime
= mouse
.getTime().getTime();
1127 secondaryEventReceiver
.handleEvent(event
);
1128 // Note that it is possible for secondaryEventReceiver to be null
1129 // now, because its handleEvent() might have finished out on the
1130 // secondary thread. So put any extra processing inside a null
1132 if (secondaryEventReceiver
!= null) {
1133 if (doubleClick
!= null) {
1134 secondaryEventReceiver
.handleEvent(doubleClick
);
1140 * Enable a widget to override the primary event thread.
1142 * @param widget widget that will receive events
1144 public final void enableSecondaryEventReceiver(final TWidget widget
) {
1146 System
.err
.println(System
.currentTimeMillis() +
1147 " enableSecondaryEventReceiver()");
1150 assert (secondaryEventReceiver
== null);
1151 assert (secondaryEventHandler
== null);
1152 assert ((widget
instanceof TMessageBox
)
1153 || (widget
instanceof TFileOpenBox
));
1154 secondaryEventReceiver
= widget
;
1155 secondaryEventHandler
= new WidgetEventHandler(this, false);
1157 (new Thread(secondaryEventHandler
)).start();
1161 * Yield to the secondary thread.
1163 public final void yield() {
1164 assert (secondaryEventReceiver
!= null);
1166 while (secondaryEventReceiver
!= null) {
1167 synchronized (primaryEventHandler
) {
1169 primaryEventHandler
.wait();
1170 } catch (InterruptedException e
) {
1178 * Do stuff when there is no user input.
1180 private void doIdle() {
1182 System
.err
.printf(System
.currentTimeMillis() + " " +
1183 Thread
.currentThread() + " doIdle()\n");
1186 synchronized (timers
) {
1189 System
.err
.printf(System
.currentTimeMillis() + " " +
1190 Thread
.currentThread() + " doIdle() 2\n");
1193 // Run any timers that have timed out
1194 Date now
= new Date();
1195 List
<TTimer
> keepTimers
= new LinkedList
<TTimer
>();
1196 for (TTimer timer
: timers
) {
1197 if (timer
.getNextTick().getTime() <= now
.getTime()) {
1198 // Something might change, so repaint the screen.
1201 if (timer
.recurring
) {
1202 keepTimers
.add(timer
);
1205 keepTimers
.add(timer
);
1209 timers
.addAll(keepTimers
);
1213 for (TWindow window
: windows
) {
1216 if (desktop
!= null) {
1222 * Wake the sleeping active event handler.
1224 private void wakeEventHandler() {
1229 if (secondaryEventHandler
!= null) {
1230 synchronized (secondaryEventHandler
) {
1231 secondaryEventHandler
.notify();
1234 assert (primaryEventHandler
!= null);
1235 synchronized (primaryEventHandler
) {
1236 primaryEventHandler
.notify();
1241 // ------------------------------------------------------------------------
1242 // TApplication -----------------------------------------------------------
1243 // ------------------------------------------------------------------------
1248 * @return the Backend
1250 public final Backend
getBackend() {
1257 * @return the Screen
1259 public final Screen
getScreen() {
1260 if (backend
instanceof TWindowBackend
) {
1261 // We are being rendered to a TWindow. We can't use its
1262 // getScreen() method because that is how it is rendering to a
1263 // hardware backend somewhere. Instead use its getOtherScreen()
1265 return ((TWindowBackend
) backend
).getOtherScreen();
1267 return backend
.getScreen();
1272 * Get the color theme.
1276 public final ColorTheme
getTheme() {
1281 * Repaint the screen on the next update.
1283 public void doRepaint() {
1289 * Get Y coordinate of the top edge of the desktop.
1291 * @return Y coordinate of the top edge of the desktop
1293 public final int getDesktopTop() {
1298 * Get Y coordinate of the bottom edge of the desktop.
1300 * @return Y coordinate of the bottom edge of the desktop
1302 public final int getDesktopBottom() {
1303 return desktopBottom
;
1307 * Set the TDesktop instance.
1309 * @param desktop a TDesktop instance, or null to remove the one that is
1312 public final void setDesktop(final TDesktop desktop
) {
1313 if (this.desktop
!= null) {
1314 this.desktop
.onClose();
1316 this.desktop
= desktop
;
1320 * Get the TDesktop instance.
1322 * @return the desktop, or null if it is not set
1324 public final TDesktop
getDesktop() {
1329 * Get the current active window.
1331 * @return the active window, or null if it is not set
1333 public final TWindow
getActiveWindow() {
1334 return activeWindow
;
1338 * Get a (shallow) copy of the window list.
1340 * @return a copy of the list of windows for this application
1342 public final List
<TWindow
> getAllWindows() {
1343 List
<TWindow
> result
= new LinkedList
<TWindow
>();
1344 result
.addAll(windows
);
1349 * Get focusFollowsMouse flag.
1351 * @return true if focus follows mouse: windows automatically raised if
1352 * the mouse passes over them
1354 public boolean getFocusFollowsMouse() {
1355 return focusFollowsMouse
;
1359 * Set focusFollowsMouse flag.
1361 * @param focusFollowsMouse if true, focus follows mouse: windows
1362 * automatically raised if the mouse passes over them
1364 public void setFocusFollowsMouse(final boolean focusFollowsMouse
) {
1365 this.focusFollowsMouse
= focusFollowsMouse
;
1369 * Display the about dialog.
1371 protected void showAboutDialog() {
1372 messageBox(i18n
.getString("aboutDialogTitle"),
1373 MessageFormat
.format(i18n
.getString("aboutDialogText"),
1374 this.getClass().getPackage().getImplementationVersion()),
1375 TMessageBox
.Type
.OK
);
1378 // ------------------------------------------------------------------------
1379 // Screen refresh loop ----------------------------------------------------
1380 // ------------------------------------------------------------------------
1383 * Invert the cell color at a position. This is used to track the mouse.
1385 * @param x column position
1386 * @param y row position
1388 private void invertCell(final int x
, final int y
) {
1390 System
.err
.printf("%d %s invertCell() %d %d\n",
1391 System
.currentTimeMillis(), Thread
.currentThread(), x
, y
);
1393 CellAttributes attr
= getScreen().getAttrXY(x
, y
);
1394 if (attr
.getForeColorRGB() < 0) {
1395 attr
.setForeColor(attr
.getForeColor().invert());
1397 attr
.setForeColorRGB(attr
.getForeColorRGB() ^
0x00ffffff);
1399 if (attr
.getBackColorRGB() < 0) {
1400 attr
.setBackColor(attr
.getBackColor().invert());
1402 attr
.setBackColorRGB(attr
.getBackColorRGB() ^
0x00ffffff);
1404 getScreen().putAttrXY(x
, y
, attr
, false);
1410 private void drawAll() {
1411 boolean menuIsActive
= false;
1414 System
.err
.printf("%d %s drawAll() enter\n",
1415 System
.currentTimeMillis(), Thread
.currentThread());
1420 System
.err
.printf("%d %s drawAll() !repaint\n",
1421 System
.currentTimeMillis(), Thread
.currentThread());
1423 synchronized (getScreen()) {
1424 if ((oldMouseX
!= mouseX
) || (oldMouseY
!= mouseY
)) {
1425 // The only thing that has happened is the mouse moved.
1426 // Clear the old position and draw the new position.
1427 invertCell(oldMouseX
, oldMouseY
);
1428 invertCell(mouseX
, mouseY
);
1432 if (getScreen().isDirty()) {
1433 backend
.flushScreen();
1440 System
.err
.printf("%d %s drawAll() REDRAW\n",
1441 System
.currentTimeMillis(), Thread
.currentThread());
1444 // If true, the cursor is not visible
1445 boolean cursor
= false;
1447 // Start with a clean screen
1448 getScreen().clear();
1451 if (desktop
!= null) {
1452 desktop
.drawChildren();
1455 // Draw each window in reverse Z order
1456 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1457 Collections
.sort(sorted
);
1458 TWindow topLevel
= null;
1459 if (sorted
.size() > 0) {
1460 topLevel
= sorted
.get(0);
1462 Collections
.reverse(sorted
);
1463 for (TWindow window
: sorted
) {
1464 if (window
.isShown()) {
1465 window
.drawChildren();
1469 // Draw the blank menubar line - reset the screen clipping first so
1470 // it won't trim it out.
1471 getScreen().resetClipping();
1472 getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
1473 theme
.getColor("tmenu"));
1474 // Now draw the menus.
1476 for (TMenu menu
: menus
) {
1477 CellAttributes menuColor
;
1478 CellAttributes menuMnemonicColor
;
1479 if (menu
.isActive()) {
1480 menuIsActive
= true;
1481 menuColor
= theme
.getColor("tmenu.highlighted");
1482 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic.highlighted");
1485 menuColor
= theme
.getColor("tmenu");
1486 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic");
1488 // Draw the menu title
1489 getScreen().hLineXY(x
, 0, menu
.getTitle().length() + 2, ' ',
1491 getScreen().putStringXY(x
+ 1, 0, menu
.getTitle(), menuColor
);
1492 // Draw the highlight character
1493 getScreen().putCharXY(x
+ 1 + menu
.getMnemonic().getShortcutIdx(),
1494 0, menu
.getMnemonic().getShortcut(), menuMnemonicColor
);
1496 if (menu
.isActive()) {
1497 menu
.drawChildren();
1498 // Reset the screen clipping so we can draw the next title.
1499 getScreen().resetClipping();
1501 x
+= menu
.getTitle().length() + 2;
1504 for (TMenu menu
: subMenus
) {
1505 // Reset the screen clipping so we can draw the next sub-menu.
1506 getScreen().resetClipping();
1507 menu
.drawChildren();
1510 // Draw the status bar of the top-level window
1511 TStatusBar statusBar
= null;
1512 if (topLevel
!= null) {
1513 statusBar
= topLevel
.getStatusBar();
1515 if (statusBar
!= null) {
1516 getScreen().resetClipping();
1517 statusBar
.setWidth(getScreen().getWidth());
1518 statusBar
.setY(getScreen().getHeight() - topLevel
.getY());
1521 CellAttributes barColor
= new CellAttributes();
1522 barColor
.setTo(getTheme().getColor("tstatusbar.text"));
1523 getScreen().hLineXY(0, desktopBottom
, getScreen().getWidth(), ' ',
1527 // Draw the mouse pointer
1528 invertCell(mouseX
, mouseY
);
1532 // Place the cursor if it is visible
1533 if (!menuIsActive
) {
1534 TWidget activeWidget
= null;
1535 if (sorted
.size() > 0) {
1536 activeWidget
= sorted
.get(sorted
.size() - 1).getActiveChild();
1537 if (activeWidget
.isCursorVisible()) {
1538 if ((activeWidget
.getCursorAbsoluteY() < desktopBottom
)
1539 && (activeWidget
.getCursorAbsoluteY() > desktopTop
)
1541 getScreen().putCursor(true,
1542 activeWidget
.getCursorAbsoluteX(),
1543 activeWidget
.getCursorAbsoluteY());
1546 getScreen().putCursor(false,
1547 activeWidget
.getCursorAbsoluteX(),
1548 activeWidget
.getCursorAbsoluteY());
1557 getScreen().hideCursor();
1560 // Flush the screen contents
1561 if (getScreen().isDirty()) {
1562 backend
.flushScreen();
1569 * Force this application to exit.
1571 public void exit() {
1573 synchronized (this) {
1578 // ------------------------------------------------------------------------
1579 // TWindow management -----------------------------------------------------
1580 // ------------------------------------------------------------------------
1583 * Return the total number of windows.
1585 * @return the total number of windows
1587 public final int windowCount() {
1588 return windows
.size();
1592 * Return the number of windows that are showing.
1594 * @return the number of windows that are showing on screen
1596 public final int shownWindowCount() {
1598 for (TWindow w
: windows
) {
1607 * Return the number of windows that are hidden.
1609 * @return the number of windows that are hidden
1611 public final int hiddenWindowCount() {
1613 for (TWindow w
: windows
) {
1622 * Check if a window instance is in this application's window list.
1624 * @param window window to look for
1625 * @return true if this window is in the list
1627 public final boolean hasWindow(final TWindow window
) {
1628 if (windows
.size() == 0) {
1631 for (TWindow w
: windows
) {
1633 assert (window
.getApplication() == this);
1641 * Activate a window: bring it to the top and have it receive events.
1643 * @param window the window to become the new active window
1645 public void activateWindow(final TWindow window
) {
1646 if (hasWindow(window
) == false) {
1648 * Someone has a handle to a window I don't have. Ignore this
1654 // Whatever window might be moving/dragging, stop it now.
1655 for (TWindow w
: windows
) {
1656 if (w
.inMovements()) {
1661 assert (windows
.size() > 0);
1663 if (window
.isHidden()) {
1664 // Unhiding will also activate.
1668 assert (window
.isShown());
1670 if (windows
.size() == 1) {
1671 assert (window
== windows
.get(0));
1672 if (activeWindow
== null) {
1673 activeWindow
= window
;
1675 activeWindow
.setActive(true);
1676 activeWindow
.onFocus();
1679 assert (window
.isActive());
1680 assert (activeWindow
== window
);
1684 if (activeWindow
== window
) {
1685 assert (window
.isActive());
1687 // Window is already active, do nothing.
1691 assert (!window
.isActive());
1692 if (activeWindow
!= null) {
1693 assert (activeWindow
.getZ() == 0);
1695 activeWindow
.setActive(false);
1696 activeWindow
.setZ(window
.getZ());
1698 // Unset activeWindow now before unfocus, so that a window
1699 // lifecycle change inside onUnfocus() doesn't call
1700 // switchWindow() and lead to a stack overflow.
1701 TWindow oldActiveWindow
= activeWindow
;
1702 activeWindow
= null;
1703 oldActiveWindow
.onUnfocus();
1705 activeWindow
= window
;
1706 activeWindow
.setZ(0);
1707 activeWindow
.setActive(true);
1708 activeWindow
.onFocus();
1715 * @param window the window to hide
1717 public void hideWindow(final TWindow window
) {
1718 if (hasWindow(window
) == false) {
1720 * Someone has a handle to a window I don't have. Ignore this
1726 // Whatever window might be moving/dragging, stop it now.
1727 for (TWindow w
: windows
) {
1728 if (w
.inMovements()) {
1733 assert (windows
.size() > 0);
1735 if (!window
.hidden
) {
1736 if (window
== activeWindow
) {
1737 if (shownWindowCount() > 1) {
1740 activeWindow
= null;
1741 window
.setActive(false);
1745 window
.hidden
= true;
1753 * @param window the window to show
1755 public void showWindow(final TWindow window
) {
1756 if (hasWindow(window
) == false) {
1758 * Someone has a handle to a window I don't have. Ignore this
1764 // Whatever window might be moving/dragging, stop it now.
1765 for (TWindow w
: windows
) {
1766 if (w
.inMovements()) {
1771 assert (windows
.size() > 0);
1773 if (window
.hidden
) {
1774 window
.hidden
= false;
1776 activateWindow(window
);
1781 * Close window. Note that the window's destructor is NOT called by this
1782 * method, instead the GC is assumed to do the cleanup.
1784 * @param window the window to remove
1786 public final void closeWindow(final TWindow window
) {
1787 if (hasWindow(window
) == false) {
1789 * Someone has a handle to a window I don't have. Ignore this
1795 synchronized (windows
) {
1796 // Whatever window might be moving/dragging, stop it now.
1797 for (TWindow w
: windows
) {
1798 if (w
.inMovements()) {
1803 int z
= window
.getZ();
1806 Collections
.sort(windows
);
1808 activeWindow
= null;
1809 for (TWindow w
: windows
) {
1811 // Do not activate a hidden window.
1817 w
.setZ(w
.getZ() - 1);
1818 if (w
.getZ() == 0) {
1821 assert (activeWindow
== null);
1833 // Perform window cleanup
1836 // Check if we are closing a TMessageBox or similar
1837 if (secondaryEventReceiver
!= null) {
1838 assert (secondaryEventHandler
!= null);
1840 // Do not send events to the secondaryEventReceiver anymore, the
1841 // window is closed.
1842 secondaryEventReceiver
= null;
1844 // Wake the secondary thread, it will wake the primary as it
1846 synchronized (secondaryEventHandler
) {
1847 secondaryEventHandler
.notify();
1851 // Permit desktop to be active if it is the only thing left.
1852 if (desktop
!= null) {
1853 if (windows
.size() == 0) {
1854 desktop
.setActive(true);
1860 * Switch to the next window.
1862 * @param forward if true, then switch to the next window in the list,
1863 * otherwise switch to the previous window in the list
1865 public final void switchWindow(final boolean forward
) {
1866 // Only switch if there are multiple visible windows
1867 if (shownWindowCount() < 2) {
1870 assert (activeWindow
!= null);
1872 synchronized (windows
) {
1873 // Whatever window might be moving/dragging, stop it now.
1874 for (TWindow w
: windows
) {
1875 if (w
.inMovements()) {
1880 // Swap z/active between active window and the next in the list
1881 int activeWindowI
= -1;
1882 for (int i
= 0; i
< windows
.size(); i
++) {
1883 if (windows
.get(i
) == activeWindow
) {
1884 assert (activeWindow
.isActive());
1888 assert (!windows
.get(0).isActive());
1891 assert (activeWindowI
>= 0);
1893 // Do not switch if a window is modal
1894 if (activeWindow
.isModal()) {
1898 int nextWindowI
= activeWindowI
;
1902 nextWindowI
%= windows
.size();
1905 if (nextWindowI
< 0) {
1906 nextWindowI
= windows
.size() - 1;
1910 if (windows
.get(nextWindowI
).isShown()) {
1911 activateWindow(windows
.get(nextWindowI
));
1915 } // synchronized (windows)
1920 * Add a window to my window list and make it active. Note package
1923 * @param window new window to add
1925 final void addWindowToApplication(final TWindow window
) {
1927 // Do not add menu windows to the window list.
1928 if (window
instanceof TMenu
) {
1932 // Do not add the desktop to the window list.
1933 if (window
instanceof TDesktop
) {
1937 synchronized (windows
) {
1938 if (windows
.contains(window
)) {
1939 throw new IllegalArgumentException("Window " + window
+
1940 " is already in window list");
1943 // Whatever window might be moving/dragging, stop it now.
1944 for (TWindow w
: windows
) {
1945 if (w
.inMovements()) {
1950 // Do not allow a modal window to spawn a non-modal window. If a
1951 // modal window is active, then this window will become modal
1953 if (modalWindowActive()) {
1954 window
.flags
|= TWindow
.MODAL
;
1955 window
.flags
|= TWindow
.CENTERED
;
1956 window
.hidden
= false;
1958 if (window
.isShown()) {
1959 for (TWindow w
: windows
) {
1964 w
.setZ(w
.getZ() + 1);
1967 windows
.add(window
);
1968 if (window
.isShown()) {
1969 activeWindow
= window
;
1970 activeWindow
.setZ(0);
1971 activeWindow
.setActive(true);
1972 activeWindow
.onFocus();
1975 if (((window
.flags
& TWindow
.CENTERED
) == 0)
1976 && ((window
.flags
& TWindow
.ABSOLUTEXY
) == 0)
1977 && (smartWindowPlacement
== true)
1980 doSmartPlacement(window
);
1984 // Desktop cannot be active over any other window.
1985 if (desktop
!= null) {
1986 desktop
.setActive(false);
1991 * Check if there is a system-modal window on top.
1993 * @return true if the active window is modal
1995 private boolean modalWindowActive() {
1996 if (windows
.size() == 0) {
2000 for (TWindow w
: windows
) {
2010 * Close all open windows.
2012 private void closeAllWindows() {
2013 // Don't do anything if we are in the menu
2014 if (activeMenu
!= null) {
2017 while (windows
.size() > 0) {
2018 closeWindow(windows
.get(0));
2023 * Re-layout the open windows as non-overlapping tiles. This produces
2024 * almost the same results as Turbo Pascal 7.0's IDE.
2026 private void tileWindows() {
2027 synchronized (windows
) {
2028 // Don't do anything if we are in the menu
2029 if (activeMenu
!= null) {
2032 int z
= windows
.size();
2038 a
= (int)(Math
.sqrt(z
));
2042 if (((a
* b
) + c
) == z
) {
2050 int newWidth
= (getScreen().getWidth() / a
);
2051 int newHeight1
= ((getScreen().getHeight() - 1) / b
);
2052 int newHeight2
= ((getScreen().getHeight() - 1) / (b
+ c
));
2054 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
2055 Collections
.sort(sorted
);
2056 Collections
.reverse(sorted
);
2057 for (int i
= 0; i
< sorted
.size(); i
++) {
2058 int logicalX
= i
/ b
;
2059 int logicalY
= i
% b
;
2060 if (i
>= ((a
- 1) * b
)) {
2062 logicalY
= i
- ((a
- 1) * b
);
2065 TWindow w
= sorted
.get(i
);
2066 w
.setX(logicalX
* newWidth
);
2067 w
.setWidth(newWidth
);
2068 if (i
>= ((a
- 1) * b
)) {
2069 w
.setY((logicalY
* newHeight2
) + 1);
2070 w
.setHeight(newHeight2
);
2072 w
.setY((logicalY
* newHeight1
) + 1);
2073 w
.setHeight(newHeight1
);
2080 * Re-layout the open windows as overlapping cascaded windows.
2082 private void cascadeWindows() {
2083 synchronized (windows
) {
2084 // Don't do anything if we are in the menu
2085 if (activeMenu
!= null) {
2090 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
2091 Collections
.sort(sorted
);
2092 Collections
.reverse(sorted
);
2093 for (TWindow window
: sorted
) {
2098 if (x
> getScreen().getWidth()) {
2101 if (y
>= getScreen().getHeight()) {
2109 * Place a window to minimize its overlap with other windows.
2111 * @param window the window to place
2113 public final void doSmartPlacement(final TWindow window
) {
2114 // This is a pretty dumb algorithm, but seems to work. The hardest
2115 // part is computing these "overlap" values seeking a minimum average
2118 int yMin
= desktopTop
;
2119 int xMax
= getScreen().getWidth() - window
.getWidth() + 1;
2120 int yMax
= desktopBottom
- window
.getHeight() + 1;
2128 if ((xMin
== xMax
) && (yMin
== yMax
)) {
2129 // No work to do, bail out.
2133 // Compute the overlap matrix without the new window.
2134 int width
= getScreen().getWidth();
2135 int height
= getScreen().getHeight();
2136 int overlapMatrix
[][] = new int[width
][height
];
2137 for (TWindow w
: windows
) {
2141 for (int x
= w
.getX(); x
< w
.getX() + w
.getWidth(); x
++) {
2148 for (int y
= w
.getY(); y
< w
.getY() + w
.getHeight(); y
++) {
2155 overlapMatrix
[x
][y
]++;
2160 long oldOverlapTotal
= 0;
2161 long oldOverlapN
= 0;
2162 for (int x
= 0; x
< width
; x
++) {
2163 for (int y
= 0; y
< height
; y
++) {
2164 oldOverlapTotal
+= overlapMatrix
[x
][y
];
2165 if (overlapMatrix
[x
][y
] > 0) {
2172 double oldOverlapAvg
= (double) oldOverlapTotal
/ (double) oldOverlapN
;
2173 boolean first
= true;
2174 int windowX
= window
.getX();
2175 int windowY
= window
.getY();
2177 // For each possible (x, y) position for the new window, compute a
2178 // new overlap matrix.
2179 for (int x
= xMin
; x
< xMax
; x
++) {
2180 for (int y
= yMin
; y
< yMax
; y
++) {
2182 // Start with the matrix minus this window.
2183 int newMatrix
[][] = new int[width
][height
];
2184 for (int mx
= 0; mx
< width
; mx
++) {
2185 for (int my
= 0; my
< height
; my
++) {
2186 newMatrix
[mx
][my
] = overlapMatrix
[mx
][my
];
2190 // Add this window's values to the new overlap matrix.
2191 long newOverlapTotal
= 0;
2192 long newOverlapN
= 0;
2193 // Start by adding each new cell.
2194 for (int wx
= x
; wx
< x
+ window
.getWidth(); wx
++) {
2198 for (int wy
= y
; wy
< y
+ window
.getHeight(); wy
++) {
2202 newMatrix
[wx
][wy
]++;
2205 // Now figure out the new value for total coverage.
2206 for (int mx
= 0; mx
< width
; mx
++) {
2207 for (int my
= 0; my
< height
; my
++) {
2208 newOverlapTotal
+= newMatrix
[x
][y
];
2209 if (newMatrix
[mx
][my
] > 0) {
2214 double newOverlapAvg
= (double) newOverlapTotal
/ (double) newOverlapN
;
2217 // First time: just record what we got.
2218 oldOverlapAvg
= newOverlapAvg
;
2221 // All other times: pick a new best (x, y) and save the
2223 if (newOverlapAvg
< oldOverlapAvg
) {
2226 oldOverlapAvg
= newOverlapAvg
;
2230 } // for (int x = xMin; x < xMax; x++)
2232 } // for (int y = yMin; y < yMax; y++)
2234 // Finally, set the window's new coordinates.
2235 window
.setX(windowX
);
2236 window
.setY(windowY
);
2239 // ------------------------------------------------------------------------
2240 // TMenu management -------------------------------------------------------
2241 // ------------------------------------------------------------------------
2244 * Check if a mouse event would hit either the active menu or any open
2247 * @param mouse mouse event
2248 * @return true if the mouse would hit the active menu or an open
2251 private boolean mouseOnMenu(final TMouseEvent mouse
) {
2252 assert (activeMenu
!= null);
2253 List
<TMenu
> menus
= new LinkedList
<TMenu
>(subMenus
);
2254 Collections
.reverse(menus
);
2255 for (TMenu menu
: menus
) {
2256 if (menu
.mouseWouldHit(mouse
)) {
2260 return activeMenu
.mouseWouldHit(mouse
);
2264 * See if we need to switch window or activate the menu based on
2267 * @param mouse mouse event
2269 private void checkSwitchFocus(final TMouseEvent mouse
) {
2271 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
2272 && (activeMenu
!= null)
2273 && (mouse
.getAbsoluteY() != 0)
2274 && (!mouseOnMenu(mouse
))
2276 // They clicked outside the active menu, turn it off
2277 activeMenu
.setActive(false);
2279 for (TMenu menu
: subMenus
) {
2280 menu
.setActive(false);
2286 // See if they hit the menu bar
2287 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
2288 && (mouse
.isMouse1())
2289 && (!modalWindowActive())
2290 && (mouse
.getAbsoluteY() == 0)
2293 for (TMenu menu
: subMenus
) {
2294 menu
.setActive(false);
2298 // They selected the menu, go activate it
2299 for (TMenu menu
: menus
) {
2300 if ((mouse
.getAbsoluteX() >= menu
.getTitleX())
2301 && (mouse
.getAbsoluteX() < menu
.getTitleX()
2302 + menu
.getTitle().length() + 2)
2304 menu
.setActive(true);
2307 menu
.setActive(false);
2313 // See if they hit the menu bar
2314 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
2315 && (mouse
.isMouse1())
2316 && (activeMenu
!= null)
2317 && (mouse
.getAbsoluteY() == 0)
2320 TMenu oldMenu
= activeMenu
;
2321 for (TMenu menu
: subMenus
) {
2322 menu
.setActive(false);
2326 // See if we should switch menus
2327 for (TMenu menu
: menus
) {
2328 if ((mouse
.getAbsoluteX() >= menu
.getTitleX())
2329 && (mouse
.getAbsoluteX() < menu
.getTitleX()
2330 + menu
.getTitle().length() + 2)
2332 menu
.setActive(true);
2336 if (oldMenu
!= activeMenu
) {
2337 // They switched menus
2338 oldMenu
.setActive(false);
2343 // If a menu is still active, don't switch windows
2344 if (activeMenu
!= null) {
2348 // Only switch if there are multiple windows
2349 if (windows
.size() < 2) {
2353 if (((focusFollowsMouse
== true)
2354 && (mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
))
2355 || (mouse
.getType() == TMouseEvent
.Type
.MOUSE_UP
)
2357 synchronized (windows
) {
2358 Collections
.sort(windows
);
2359 if (windows
.get(0).isModal()) {
2360 // Modal windows don't switch
2364 for (TWindow window
: windows
) {
2365 assert (!window
.isModal());
2367 if (window
.isHidden()) {
2368 assert (!window
.isActive());
2372 if (window
.mouseWouldHit(mouse
)) {
2373 if (window
== windows
.get(0)) {
2374 // Clicked on the same window, nothing to do
2375 assert (window
.isActive());
2379 // We will be switching to another window
2380 assert (windows
.get(0).isActive());
2381 assert (windows
.get(0) == activeWindow
);
2382 assert (!window
.isActive());
2383 activeWindow
.onUnfocus();
2384 activeWindow
.setActive(false);
2385 activeWindow
.setZ(window
.getZ());
2386 activeWindow
= window
;
2388 window
.setActive(true);
2395 // Clicked on the background, nothing to do
2399 // Nothing to do: this isn't a mouse up, or focus isn't following
2405 * Turn off the menu.
2407 public final void closeMenu() {
2408 if (activeMenu
!= null) {
2409 activeMenu
.setActive(false);
2411 for (TMenu menu
: subMenus
) {
2412 menu
.setActive(false);
2419 * Get a (shallow) copy of the menu list.
2421 * @return a copy of the menu list
2423 public final List
<TMenu
> getAllMenus() {
2424 return new LinkedList
<TMenu
>(menus
);
2428 * Add a top-level menu to the list.
2430 * @param menu the menu to add
2431 * @throws IllegalArgumentException if the menu is already used in
2432 * another TApplication
2434 public final void addMenu(final TMenu menu
) {
2435 if ((menu
.getApplication() != null)
2436 && (menu
.getApplication() != this)
2438 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2439 "part of application " + menu
.getApplication());
2447 * Remove a top-level menu from the list.
2449 * @param menu the menu to remove
2450 * @throws IllegalArgumentException if the menu is already used in
2451 * another TApplication
2453 public final void removeMenu(final TMenu menu
) {
2454 if ((menu
.getApplication() != null)
2455 && (menu
.getApplication() != this)
2457 throw new IllegalArgumentException("Menu " + menu
+ " is already " +
2458 "part of application " + menu
.getApplication());
2466 * Turn off a sub-menu.
2468 public final void closeSubMenu() {
2469 assert (activeMenu
!= null);
2470 TMenu item
= subMenus
.get(subMenus
.size() - 1);
2471 assert (item
!= null);
2472 item
.setActive(false);
2473 subMenus
.remove(subMenus
.size() - 1);
2477 * Switch to the next menu.
2479 * @param forward if true, then switch to the next menu in the list,
2480 * otherwise switch to the previous menu in the list
2482 public final void switchMenu(final boolean forward
) {
2483 assert (activeMenu
!= null);
2485 for (TMenu menu
: subMenus
) {
2486 menu
.setActive(false);
2490 for (int i
= 0; i
< menus
.size(); i
++) {
2491 if (activeMenu
== menus
.get(i
)) {
2493 if (i
< menus
.size() - 1) {
2501 activeMenu
.setActive(false);
2502 activeMenu
= menus
.get(i
);
2503 activeMenu
.setActive(true);
2510 * Add a menu item to the global list. If it has a keyboard accelerator,
2511 * that will be added the global hash.
2513 * @param item the menu item
2515 public final void addMenuItem(final TMenuItem item
) {
2516 menuItems
.add(item
);
2518 TKeypress key
= item
.getKey();
2520 synchronized (accelerators
) {
2521 assert (accelerators
.get(key
) == null);
2522 accelerators
.put(key
.toLowerCase(), item
);
2528 * Disable one menu item.
2530 * @param id the menu item ID
2532 public final void disableMenuItem(final int id
) {
2533 for (TMenuItem item
: menuItems
) {
2534 if (item
.getId() == id
) {
2535 item
.setEnabled(false);
2541 * Disable the range of menu items with ID's between lower and upper,
2544 * @param lower the lowest menu item ID
2545 * @param upper the highest menu item ID
2547 public final void disableMenuItems(final int lower
, final int upper
) {
2548 for (TMenuItem item
: menuItems
) {
2549 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
2550 item
.setEnabled(false);
2556 * Enable one menu item.
2558 * @param id the menu item ID
2560 public final void enableMenuItem(final int id
) {
2561 for (TMenuItem item
: menuItems
) {
2562 if (item
.getId() == id
) {
2563 item
.setEnabled(true);
2569 * Enable the range of menu items with ID's between lower and upper,
2572 * @param lower the lowest menu item ID
2573 * @param upper the highest menu item ID
2575 public final void enableMenuItems(final int lower
, final int upper
) {
2576 for (TMenuItem item
: menuItems
) {
2577 if ((item
.getId() >= lower
) && (item
.getId() <= upper
)) {
2578 item
.setEnabled(true);
2584 * Recompute menu x positions based on their title length.
2586 public final void recomputeMenuX() {
2588 for (TMenu menu
: menus
) {
2591 x
+= menu
.getTitle().length() + 2;
2593 // Don't let the menu window exceed the screen width
2594 int rightEdge
= menu
.getX() + menu
.getWidth();
2595 if (rightEdge
> getScreen().getWidth()) {
2596 menu
.setX(getScreen().getWidth() - menu
.getWidth());
2602 * Post an event to process.
2604 * @param event new event to add to the queue
2606 public final void postEvent(final TInputEvent event
) {
2607 synchronized (this) {
2608 synchronized (fillEventQueue
) {
2609 fillEventQueue
.add(event
);
2612 System
.err
.println(System
.currentTimeMillis() + " " +
2613 Thread
.currentThread() + " postEvent() wake up main");
2620 * Post an event to process and turn off the menu.
2622 * @param event new event to add to the queue
2624 public final void postMenuEvent(final TInputEvent event
) {
2625 synchronized (this) {
2626 synchronized (fillEventQueue
) {
2627 fillEventQueue
.add(event
);
2630 System
.err
.println(System
.currentTimeMillis() + " " +
2631 Thread
.currentThread() + " postMenuEvent() wake up main");
2639 * Add a sub-menu to the list of open sub-menus.
2641 * @param menu sub-menu
2643 public final void addSubMenu(final TMenu menu
) {
2648 * Convenience function to add a top-level menu.
2650 * @param title menu title
2651 * @return the new menu
2653 public final TMenu
addMenu(final String title
) {
2656 TMenu menu
= new TMenu(this, x
, y
, title
);
2663 * Convenience function to add a default "File" menu.
2665 * @return the new menu
2667 public final TMenu
addFileMenu() {
2668 TMenu fileMenu
= addMenu(i18n
.getString("fileMenuTitle"));
2669 fileMenu
.addDefaultItem(TMenu
.MID_OPEN_FILE
);
2670 fileMenu
.addSeparator();
2671 fileMenu
.addDefaultItem(TMenu
.MID_SHELL
);
2672 fileMenu
.addDefaultItem(TMenu
.MID_EXIT
);
2673 TStatusBar statusBar
= fileMenu
.newStatusBar(i18n
.
2674 getString("fileMenuStatus"));
2675 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2680 * Convenience function to add a default "Edit" menu.
2682 * @return the new menu
2684 public final TMenu
addEditMenu() {
2685 TMenu editMenu
= addMenu(i18n
.getString("editMenuTitle"));
2686 editMenu
.addDefaultItem(TMenu
.MID_CUT
);
2687 editMenu
.addDefaultItem(TMenu
.MID_COPY
);
2688 editMenu
.addDefaultItem(TMenu
.MID_PASTE
);
2689 editMenu
.addDefaultItem(TMenu
.MID_CLEAR
);
2690 TStatusBar statusBar
= editMenu
.newStatusBar(i18n
.
2691 getString("editMenuStatus"));
2692 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2697 * Convenience function to add a default "Window" menu.
2699 * @return the new menu
2701 public final TMenu
addWindowMenu() {
2702 TMenu windowMenu
= addMenu(i18n
.getString("windowMenuTitle"));
2703 windowMenu
.addDefaultItem(TMenu
.MID_TILE
);
2704 windowMenu
.addDefaultItem(TMenu
.MID_CASCADE
);
2705 windowMenu
.addDefaultItem(TMenu
.MID_CLOSE_ALL
);
2706 windowMenu
.addSeparator();
2707 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_MOVE
);
2708 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_ZOOM
);
2709 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_NEXT
);
2710 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_PREVIOUS
);
2711 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_CLOSE
);
2712 TStatusBar statusBar
= windowMenu
.newStatusBar(i18n
.
2713 getString("windowMenuStatus"));
2714 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2719 * Convenience function to add a default "Help" menu.
2721 * @return the new menu
2723 public final TMenu
addHelpMenu() {
2724 TMenu helpMenu
= addMenu(i18n
.getString("helpMenuTitle"));
2725 helpMenu
.addDefaultItem(TMenu
.MID_HELP_CONTENTS
);
2726 helpMenu
.addDefaultItem(TMenu
.MID_HELP_INDEX
);
2727 helpMenu
.addDefaultItem(TMenu
.MID_HELP_SEARCH
);
2728 helpMenu
.addDefaultItem(TMenu
.MID_HELP_PREVIOUS
);
2729 helpMenu
.addDefaultItem(TMenu
.MID_HELP_HELP
);
2730 helpMenu
.addDefaultItem(TMenu
.MID_HELP_ACTIVE_FILE
);
2731 helpMenu
.addSeparator();
2732 helpMenu
.addDefaultItem(TMenu
.MID_ABOUT
);
2733 TStatusBar statusBar
= helpMenu
.newStatusBar(i18n
.
2734 getString("helpMenuStatus"));
2735 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, i18n
.getString("Help"));
2739 // ------------------------------------------------------------------------
2740 // TTimer management ------------------------------------------------------
2741 // ------------------------------------------------------------------------
2744 * Get the amount of time I can sleep before missing a Timer tick.
2746 * @param timeout = initial (maximum) timeout in millis
2747 * @return number of milliseconds between now and the next timer event
2749 private long getSleepTime(final long timeout
) {
2750 Date now
= new Date();
2751 long nowTime
= now
.getTime();
2752 long sleepTime
= timeout
;
2754 synchronized (timers
) {
2755 for (TTimer timer
: timers
) {
2756 long nextTickTime
= timer
.getNextTick().getTime();
2757 if (nextTickTime
< nowTime
) {
2761 long timeDifference
= nextTickTime
- nowTime
;
2762 if (timeDifference
< sleepTime
) {
2763 sleepTime
= timeDifference
;
2768 assert (sleepTime
>= 0);
2769 assert (sleepTime
<= timeout
);
2774 * Convenience function to add a timer.
2776 * @param duration number of milliseconds to wait between ticks
2777 * @param recurring if true, re-schedule this timer after every tick
2778 * @param action function to call when button is pressed
2781 public final TTimer
addTimer(final long duration
, final boolean recurring
,
2782 final TAction action
) {
2784 TTimer timer
= new TTimer(duration
, recurring
, action
);
2785 synchronized (timers
) {
2792 * Convenience function to remove a timer.
2794 * @param timer timer to remove
2796 public final void removeTimer(final TTimer timer
) {
2797 synchronized (timers
) {
2798 timers
.remove(timer
);
2802 // ------------------------------------------------------------------------
2803 // Other TWindow constructors ---------------------------------------------
2804 // ------------------------------------------------------------------------
2807 * Convenience function to spawn a message box.
2809 * @param title window title, will be centered along the top border
2810 * @param caption message to display. Use embedded newlines to get a
2812 * @return the new message box
2814 public final TMessageBox
messageBox(final String title
,
2815 final String caption
) {
2817 return new TMessageBox(this, title
, caption
, TMessageBox
.Type
.OK
);
2821 * Convenience function to spawn a message box.
2823 * @param title window title, will be centered along the top border
2824 * @param caption message to display. Use embedded newlines to get a
2826 * @param type one of the TMessageBox.Type constants. Default is
2828 * @return the new message box
2830 public final TMessageBox
messageBox(final String title
,
2831 final String caption
, final TMessageBox
.Type type
) {
2833 return new TMessageBox(this, title
, caption
, type
);
2837 * Convenience function to spawn an input box.
2839 * @param title window title, will be centered along the top border
2840 * @param caption message to display. Use embedded newlines to get a
2842 * @return the new input box
2844 public final TInputBox
inputBox(final String title
, final String caption
) {
2846 return new TInputBox(this, title
, caption
);
2850 * Convenience function to spawn an input box.
2852 * @param title window title, will be centered along the top border
2853 * @param caption message to display. Use embedded newlines to get a
2855 * @param text initial text to seed the field with
2856 * @return the new input box
2858 public final TInputBox
inputBox(final String title
, final String caption
,
2859 final String text
) {
2861 return new TInputBox(this, title
, caption
, text
);
2865 * Convenience function to open a terminal window.
2867 * @param x column relative to parent
2868 * @param y row relative to parent
2869 * @return the terminal new window
2871 public final TTerminalWindow
openTerminal(final int x
, final int y
) {
2872 return openTerminal(x
, y
, TWindow
.RESIZABLE
);
2876 * Convenience function to open a terminal window.
2878 * @param x column relative to parent
2879 * @param y row relative to parent
2880 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2881 * @return the terminal new window
2883 public final TTerminalWindow
openTerminal(final int x
, final int y
,
2886 return new TTerminalWindow(this, x
, y
, flags
);
2890 * Convenience function to open a terminal window and execute a custom
2891 * command line inside it.
2893 * @param x column relative to parent
2894 * @param y row relative to parent
2895 * @param commandLine the command line to execute
2896 * @return the terminal new window
2898 public final TTerminalWindow
openTerminal(final int x
, final int y
,
2899 final String commandLine
) {
2901 return openTerminal(x
, y
, TWindow
.RESIZABLE
, commandLine
);
2905 * Convenience function to open a terminal window and execute a custom
2906 * command line inside it.
2908 * @param x column relative to parent
2909 * @param y row relative to parent
2910 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2911 * @param command the command line to execute
2912 * @return the terminal new window
2914 public final TTerminalWindow
openTerminal(final int x
, final int y
,
2915 final int flags
, final String
[] command
) {
2917 return new TTerminalWindow(this, x
, y
, flags
, command
);
2921 * Convenience function to open a terminal window and execute a custom
2922 * command line inside it.
2924 * @param x column relative to parent
2925 * @param y row relative to parent
2926 * @param flags mask of CENTERED, MODAL, or RESIZABLE
2927 * @param commandLine the command line to execute
2928 * @return the terminal new window
2930 public final TTerminalWindow
openTerminal(final int x
, final int y
,
2931 final int flags
, final String commandLine
) {
2933 return new TTerminalWindow(this, x
, y
, flags
, commandLine
.split("\\s"));
2937 * Convenience function to spawn an file open box.
2939 * @param path path of selected file
2940 * @return the result of the new file open box
2941 * @throws IOException if java.io operation throws
2943 public final String
fileOpenBox(final String path
) throws IOException
{
2945 TFileOpenBox box
= new TFileOpenBox(this, path
, TFileOpenBox
.Type
.OPEN
);
2946 return box
.getFilename();
2950 * Convenience function to spawn an file open box.
2952 * @param path path of selected file
2953 * @param type one of the Type constants
2954 * @return the result of the new file open box
2955 * @throws IOException if java.io operation throws
2957 public final String
fileOpenBox(final String path
,
2958 final TFileOpenBox
.Type type
) throws IOException
{
2960 TFileOpenBox box
= new TFileOpenBox(this, path
, type
);
2961 return box
.getFilename();
2965 * Convenience function to create a new window and make it active.
2966 * Window will be located at (0, 0).
2968 * @param title window title, will be centered along the top border
2969 * @param width width of window
2970 * @param height height of window
2971 * @return the new window
2973 public final TWindow
addWindow(final String title
, final int width
,
2976 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
);
2981 * Convenience function to create a new window and make it active.
2982 * Window will be located at (0, 0).
2984 * @param title window title, will be centered along the top border
2985 * @param width width of window
2986 * @param height height of window
2987 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
2988 * @return the new window
2990 public final TWindow
addWindow(final String title
,
2991 final int width
, final int height
, final int flags
) {
2993 TWindow window
= new TWindow(this, title
, 0, 0, width
, height
, flags
);
2998 * Convenience function to create a new window and make it active.
3000 * @param title window title, will be centered along the top border
3001 * @param x column relative to parent
3002 * @param y row relative to parent
3003 * @param width width of window
3004 * @param height height of window
3005 * @return the new window
3007 public final TWindow
addWindow(final String title
,
3008 final int x
, final int y
, final int width
, final int height
) {
3010 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
);
3015 * Convenience function to create a new window and make it active.
3017 * @param title window title, will be centered along the top border
3018 * @param x column relative to parent
3019 * @param y row relative to parent
3020 * @param width width of window
3021 * @param height height of window
3022 * @param flags mask of RESIZABLE, CENTERED, or MODAL
3023 * @return the new window
3025 public final TWindow
addWindow(final String title
,
3026 final int x
, final int y
, final int width
, final int height
,
3029 TWindow window
= new TWindow(this, title
, x
, y
, width
, height
, flags
);
3034 * Convenience function to open a file in an editor window and make it
3037 * @param file the file to open
3038 * @return the new editor window
3039 * @throws IOException if a java.io operation throws
3041 public final TEditorWindow
addEditor(final File file
) throws IOException
{
3043 TEditorWindow editor
= new TEditorWindow(this, file
);