2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.io
.InputStream
;
34 import java
.io
.OutputStream
;
35 import java
.io
.UnsupportedEncodingException
;
36 import java
.util
.Collections
;
37 import java
.util
.HashMap
;
38 import java
.util
.LinkedList
;
39 import java
.util
.List
;
42 import jexer
.bits
.CellAttributes
;
43 import jexer
.bits
.ColorTheme
;
44 import jexer
.bits
.GraphicsChars
;
45 import jexer
.event
.TCommandEvent
;
46 import jexer
.event
.TInputEvent
;
47 import jexer
.event
.TKeypressEvent
;
48 import jexer
.event
.TMenuEvent
;
49 import jexer
.event
.TMouseEvent
;
50 import jexer
.event
.TResizeEvent
;
51 import jexer
.backend
.Backend
;
52 import jexer
.backend
.ECMA48Backend
;
53 import jexer
.io
.Screen
;
54 import jexer
.menu
.TMenu
;
55 import jexer
.menu
.TMenuItem
;
56 import static jexer
.TCommand
.*;
57 import static jexer
.TKeypress
.*;
60 * TApplication sets up a full Text User Interface application.
62 public class TApplication
{
65 * Access to the physical screen, keyboard, and mouse.
67 private Backend backend
;
74 public final Screen
getScreen() {
75 return backend
.getScreen();
79 * Actual mouse coordinate X.
84 * Actual mouse coordinate Y.
89 * Event queue that is filled by run().
91 private List
<TInputEvent
> fillEventQueue
;
94 * Event queue that will be drained by either primary or secondary
97 private List
<TInputEvent
> drainEventQueue
;
100 * Top-level menus in this application.
102 private List
<TMenu
> menus
;
105 * Stack of activated sub-menus in this application.
107 private List
<TMenu
> subMenus
;
110 * The currently acive menu.
112 private TMenu activeMenu
= null;
115 * Active keyboard accelerators.
117 private Map
<TKeypress
, TMenuItem
> accelerators
;
120 * Windows and widgets pull colors from this ColorTheme.
122 private ColorTheme theme
;
125 * Get the color theme.
129 public final ColorTheme
getTheme() {
134 * The top-level windows (but not menus).
136 private List
<TWindow
> windows
;
139 * When true, exit the application.
141 private boolean quit
= false;
144 * When true, repaint the entire screen.
146 private boolean repaint
= true;
149 * Request full repaint on next screen refresh.
151 public final void setRepaint() {
156 * When true, just flush updates from the screen.
158 private boolean flush
= false;
161 * Y coordinate of the top edge of the desktop. For now this is a
162 * constant. Someday it would be nice to have a multi-line menu or
165 private static final int desktopTop
= 1;
168 * Get Y coordinate of the top edge of the desktop.
170 * @return Y coordinate of the top edge of the desktop
172 public final int getDesktopTop() {
177 * Y coordinate of the bottom edge of the desktop.
179 private int desktopBottom
;
182 * Get Y coordinate of the bottom edge of the desktop.
184 * @return Y coordinate of the bottom edge of the desktop
186 public final int getDesktopBottom() {
187 return desktopBottom
;
191 * Public constructor.
193 * @param input an InputStream connected to the remote user, or null for
194 * System.in. If System.in is used, then on non-Windows systems it will
195 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
196 * mode. input is always converted to a Reader with UTF-8 encoding.
197 * @param output an OutputStream connected to the remote user, or null
198 * for System.out. output is always converted to a Writer with UTF-8
200 * @throws UnsupportedEncodingException if an exception is thrown when
201 * creating the InputStreamReader
203 public TApplication(final InputStream input
,
204 final OutputStream output
) throws UnsupportedEncodingException
{
206 backend
= new ECMA48Backend(input
, output
);
207 theme
= new ColorTheme();
208 desktopBottom
= getScreen().getHeight() - 1;
209 fillEventQueue
= new LinkedList
<TInputEvent
>();
210 drainEventQueue
= new LinkedList
<TInputEvent
>();
211 windows
= new LinkedList
<TWindow
>();
212 menus
= new LinkedList
<TMenu
>();
213 subMenus
= new LinkedList
<TMenu
>();
214 accelerators
= new HashMap
<TKeypress
, TMenuItem
>();
218 * Invert the cell at the mouse pointer position.
220 private void drawMouse() {
221 CellAttributes attr
= getScreen().getAttrXY(mouseX
, mouseY
);
222 attr
.setForeColor(attr
.getForeColor().invert());
223 attr
.setBackColor(attr
.getBackColor().invert());
224 getScreen().putAttrXY(mouseX
, mouseY
, attr
, false);
227 if (windows
.size() == 0) {
235 public final void drawAll() {
236 if ((flush
) && (!repaint
)) {
237 backend
.flushScreen();
246 // If true, the cursor is not visible
247 boolean cursor
= false;
249 // Start with a clean screen
252 // Draw the background
253 CellAttributes background
= theme
.getColor("tapplication.background");
254 getScreen().putAll(GraphicsChars
.HATCH
, background
);
256 // Draw each window in reverse Z order
257 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
258 Collections
.sort(sorted
);
259 Collections
.reverse(sorted
);
260 for (TWindow window
: sorted
) {
261 window
.drawChildren();
264 // Draw the blank menubar line - reset the screen clipping first so
265 // it won't trim it out.
266 getScreen().resetClipping();
267 getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
268 theme
.getColor("tmenu"));
269 // Now draw the menus.
271 for (TMenu menu
: menus
) {
272 CellAttributes menuColor
;
273 CellAttributes menuMnemonicColor
;
274 if (menu
.getActive()) {
275 menuColor
= theme
.getColor("tmenu.highlighted");
276 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic.highlighted");
278 menuColor
= theme
.getColor("tmenu");
279 menuMnemonicColor
= theme
.getColor("tmenu.mnemonic");
281 // Draw the menu title
282 getScreen().hLineXY(x
, 0, menu
.getTitle().length() + 2, ' ',
284 getScreen().putStrXY(x
+ 1, 0, menu
.getTitle(), menuColor
);
285 // Draw the highlight character
286 getScreen().putCharXY(x
+ 1 + menu
.getMnemonic().getShortcutIdx(),
287 0, menu
.getMnemonic().getShortcut(), menuMnemonicColor
);
289 if (menu
.getActive()) {
291 // Reset the screen clipping so we can draw the next title.
292 getScreen().resetClipping();
294 x
+= menu
.getTitle().length() + 2;
297 for (TMenu menu
: subMenus
) {
298 // Reset the screen clipping so we can draw the next sub-menu.
299 getScreen().resetClipping();
303 // Draw the mouse pointer
306 // Place the cursor if it is visible
307 TWidget activeWidget
= null;
308 if (sorted
.size() > 0) {
309 activeWidget
= sorted
.get(sorted
.size() - 1).getActiveChild();
310 if (activeWidget
.visibleCursor()) {
311 getScreen().putCursor(true, activeWidget
.getCursorAbsoluteX(),
312 activeWidget
.getCursorAbsoluteY());
319 getScreen().hideCursor();
322 // Flush the screen contents
323 backend
.flushScreen();
330 * Run this application until it exits.
332 public final void run() {
334 // Timeout is in milliseconds, so default timeout after 1 second
336 int timeout
= getSleepTime(1000);
338 // See if there are any definitely events waiting to be processed
339 // or a screen redraw to do. If so, do not wait if there is no
341 synchronized (drainEventQueue
) {
342 if (drainEventQueue
.size() > 0) {
346 synchronized (fillEventQueue
) {
347 if (fillEventQueue
.size() > 0) {
352 // Pull any pending I/O events
353 backend
.getEvents(fillEventQueue
, timeout
);
355 // Dispatch each event to the appropriate handler, one at a time.
357 TInputEvent event
= null;
358 synchronized (fillEventQueue
) {
359 if (fillEventQueue
.size() == 0) {
362 event
= fillEventQueue
.remove(0);
364 metaHandleEvent(event
);
367 // Process timers and call doIdle()'s
376 // Shutdown the fibers
377 eventQueue.length = 0;
378 if (secondaryEventFiber !is null) {
379 assert(secondaryEventReceiver !is null);
380 secondaryEventReceiver = null;
381 if (secondaryEventFiber.state == Fiber.State.HOLD) {
382 // Wake up the secondary handler so that it can exit.
383 secondaryEventFiber.call();
387 if (primaryEventFiber.state == Fiber.State.HOLD) {
388 // Wake up the primary handler so that it can exit.
389 primaryEventFiber.call();
397 * Peek at certain application-level events, add to eventQueue, and wake
398 * up the consuming Thread.
400 * @param event the input event to consume
402 private void metaHandleEvent(final TInputEvent event
) {
405 System.err.printf(String.format("metaHandleEvents event: %s\n",
406 event)); System.err.flush();
410 // Do no more processing if the application is already trying
415 // Special application-wide events -------------------------------
418 if (event
instanceof TCommandEvent
) {
419 TCommandEvent command
= (TCommandEvent
) event
;
420 if (command
.getCmd().equals(cmAbort
)) {
427 if (event
instanceof TResizeEvent
) {
428 TResizeEvent resize
= (TResizeEvent
) event
;
429 getScreen().setDimensions(resize
.getWidth(),
431 desktopBottom
= getScreen().getHeight() - 1;
438 // Peek at the mouse position
439 if (event
instanceof TMouseEvent
) {
440 TMouseEvent mouse
= (TMouseEvent
) event
;
441 if ((mouseX
!= mouse
.getX()) || (mouseY
!= mouse
.getY())) {
442 mouseX
= mouse
.getX();
443 mouseY
= mouse
.getY();
448 // TODO: change to two separate threads
449 primaryHandleEvent(event
);
453 // Put into the main queue
456 // Have one of the two consumer Fibers peel the events off
458 if (secondaryEventFiber !is null) {
459 assert(secondaryEventFiber.state == Fiber.State.HOLD);
461 // Wake up the secondary handler for these events
462 secondaryEventFiber.call();
464 assert(primaryEventFiber.state == Fiber.State.HOLD);
466 // Wake up the primary handler for these events
467 primaryEventFiber.call();
474 * Dispatch one event to the appropriate widget or application-level
475 * event handler. This is the primary event handler, it has the normal
476 * application-wide event handling.
478 * @param event the input event to consume
479 * @see #secondaryHandleEvent(TInputEvent event)
481 private void primaryHandleEvent(final TInputEvent event
) {
483 // System.err.printf("Handle event: %s\n", event);
485 // Special application-wide events -----------------------------------
487 // Peek at the mouse position
488 if (event
instanceof TMouseEvent
) {
489 // See if we need to switch focus to another window or the menu
490 checkSwitchFocus((TMouseEvent
) event
);
493 // Handle menu events
494 if ((activeMenu
!= null) && !(event
instanceof TCommandEvent
)) {
495 TMenu menu
= activeMenu
;
497 if (event
instanceof TMouseEvent
) {
498 TMouseEvent mouse
= (TMouseEvent
) event
;
500 while (subMenus
.size() > 0) {
501 TMenu subMenu
= subMenus
.get(subMenus
.size() - 1);
502 if (subMenu
.mouseWouldHit(mouse
)) {
505 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
506 && (!mouse
.getMouse1())
507 && (!mouse
.getMouse2())
508 && (!mouse
.getMouse3())
509 && (!mouse
.getMouseWheelUp())
510 && (!mouse
.getMouseWheelDown())
514 // We navigated away from a sub-menu, so close it
518 // Convert the mouse relative x/y to menu coordinates
519 assert (mouse
.getX() == mouse
.getAbsoluteX());
520 assert (mouse
.getY() == mouse
.getAbsoluteY());
521 if (subMenus
.size() > 0) {
522 menu
= subMenus
.get(subMenus
.size() - 1);
524 mouse
.setX(mouse
.getX() - menu
.getX());
525 mouse
.setY(mouse
.getY() - menu
.getY());
527 menu
.handleEvent(event
);
531 if (event
instanceof TKeypressEvent
) {
532 TKeypressEvent keypress
= (TKeypressEvent
) event
;
534 // See if this key matches an accelerator, and if so dispatch the
536 TKeypress keypressLowercase
= keypress
.getKey().toLowerCase();
537 TMenuItem item
= null;
538 synchronized (accelerators
) {
539 item
= accelerators
.get(keypressLowercase
);
542 // Let the menu item dispatch
546 // Handle the keypress
547 if (onKeypress(keypress
)) {
553 if (event
instanceof TCommandEvent
) {
554 if (onCommand((TCommandEvent
) event
)) {
559 if (event
instanceof TMenuEvent
) {
560 if (onMenu((TMenuEvent
) event
)) {
565 // Dispatch events to the active window -------------------------------
566 for (TWindow window
: windows
) {
567 if (window
.getActive()) {
568 if (event
instanceof TMouseEvent
) {
569 TMouseEvent mouse
= (TMouseEvent
) event
;
570 // Convert the mouse relative x/y to window coordinates
571 assert (mouse
.getX() == mouse
.getAbsoluteX());
572 assert (mouse
.getY() == mouse
.getAbsoluteY());
573 mouse
.setX(mouse
.getX() - window
.getX());
574 mouse
.setY(mouse
.getY() - window
.getY());
576 // System.err("TApplication dispatch event: %s\n", event);
577 window
.handleEvent(event
);
583 * Dispatch one event to the appropriate widget or application-level
584 * event handler. This is the secondary event handler used by certain
585 * special dialogs (currently TMessageBox and TFileOpenBox).
587 * @param event the input event to consume
588 * @see #primaryHandleEvent(TInputEvent event)
590 private void secondaryHandleEvent(final TInputEvent event
) {
595 * Do stuff when there is no user input.
597 private void doIdle() {
600 // Now run any timers that have timed out
601 auto now = Clock.currTime;
602 TTimer [] keepTimers;
603 foreach (t; timers) {
604 if (t.nextTick < now) {
606 if (t.recurring == true) {
616 foreach (w; windows) {
623 * Get the amount of time I can sleep before missing a Timer tick.
625 * @param timeout = initial (maximum) timeout
626 * @return number of milliseconds between now and the next timer event
628 protected int getSleepTime(final int timeout
) {
630 auto now = Clock.currTime;
631 auto sleepTime = dur!("msecs")(timeout);
632 foreach (t; timers) {
633 if (t.nextTick < now) {
636 if ((t.nextTick > now) &&
637 ((t.nextTick - now) < sleepTime)
639 sleepTime = t.nextTick - now;
642 assert(sleepTime.total!("msecs")() >= 0);
643 return cast(uint)sleepTime.total!("msecs")();
645 // TODO: fix timers. Until then, come back after 250 millis.
650 * Close window. Note that the window's destructor is NOT called by this
651 * method, instead the GC is assumed to do the cleanup.
653 * @param window the window to remove
655 public final void closeWindow(final TWindow window
) {
656 int z
= window
.getZ();
658 Collections
.sort(windows
);
660 TWindow activeWindow
= null;
661 for (TWindow w
: windows
) {
663 w
.setZ(w
.getZ() - 1);
666 assert (activeWindow
== null);
674 // Perform window cleanup
684 // Check if we are closing a TMessageBox or similar
685 if (secondaryEventReceiver !is null) {
686 assert(secondaryEventFiber !is null);
688 // Do not send events to the secondaryEventReceiver anymore, the
690 secondaryEventReceiver = null;
692 // Special case: if this is called while executing on a
693 // secondaryEventFiber, call it so that widgetEventHandler() can
695 if (secondaryEventFiber.state == Fiber.State.HOLD) {
696 secondaryEventFiber.call();
698 secondaryEventFiber = null;
700 // Unfreeze the logic in handleEvent()
701 if (primaryEventFiber.state == Fiber.State.HOLD) {
702 primaryEventFiber.call();
709 * Switch to the next window.
711 * @param forward if true, then switch to the next window in the list,
712 * otherwise switch to the previous window in the list
714 public final void switchWindow(final boolean forward
) {
715 // Only switch if there are multiple windows
716 if (windows
.size() < 2) {
720 // Swap z/active between active window and the next in the list
721 int activeWindowI
= -1;
722 for (int i
= 0; i
< windows
.size(); i
++) {
723 if (windows
.get(i
).getActive()) {
728 assert (activeWindowI
>= 0);
730 // Do not switch if a window is modal
731 if (windows
.get(activeWindowI
).isModal()) {
737 nextWindowI
= (activeWindowI
+ 1) % windows
.size();
739 if (activeWindowI
== 0) {
740 nextWindowI
= windows
.size() - 1;
742 nextWindowI
= activeWindowI
- 1;
745 windows
.get(activeWindowI
).setActive(false);
746 windows
.get(activeWindowI
).setZ(windows
.get(nextWindowI
).getZ());
747 windows
.get(nextWindowI
).setZ(0);
748 windows
.get(nextWindowI
).setActive(true);
755 * Add a window to my window list and make it active.
757 * @param window new window to add
759 public final void addWindow(final TWindow window
) {
760 // Do not allow a modal window to spawn a non-modal window
761 if ((windows
.size() > 0) && (windows
.get(0).isModal())) {
762 assert (window
.isModal());
764 for (TWindow w
: windows
) {
766 w
.setZ(w
.getZ() + 1);
769 window
.setActive(true);
774 * Check if there is a system-modal window on top.
776 * @return true if the active window is modal
778 private boolean modalWindowActive() {
779 if (windows
.size() == 0) {
782 return windows
.get(windows
.size() - 1).isModal();
786 * Check if a mouse event would hit either the active menu or any open
789 * @param mouse mouse event
790 * @return true if the mouse would hit the active menu or an open
793 private boolean mouseOnMenu(final TMouseEvent mouse
) {
794 assert (activeMenu
!= null);
795 List
<TMenu
> menus
= new LinkedList
<TMenu
>(subMenus
);
796 Collections
.reverse(menus
);
797 for (TMenu menu
: menus
) {
798 if (menu
.mouseWouldHit(mouse
)) {
802 return activeMenu
.mouseWouldHit(mouse
);
806 * See if we need to switch window or activate the menu based on
809 * @param mouse mouse event
811 private void checkSwitchFocus(final TMouseEvent mouse
) {
813 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
814 && (activeMenu
!= null)
815 && (mouse
.getAbsoluteY() != 0)
816 && (!mouseOnMenu(mouse
))
818 // They clicked outside the active menu, turn it off
819 activeMenu
.setActive(false);
821 for (TMenu menu
: subMenus
) {
822 menu
.setActive(false);
828 // See if they hit the menu bar
829 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_DOWN
)
830 && (mouse
.getMouse1())
831 && (!modalWindowActive())
832 && (mouse
.getAbsoluteY() == 0)
835 for (TMenu menu
: subMenus
) {
836 menu
.setActive(false);
840 // They selected the menu, go activate it
841 for (TMenu menu
: menus
) {
842 if ((mouse
.getAbsoluteX() >= menu
.getX())
843 && (mouse
.getAbsoluteX() < menu
.getX()
844 + menu
.getTitle().length() + 2)
846 menu
.setActive(true);
849 menu
.setActive(false);
856 // See if they hit the menu bar
857 if ((mouse
.getType() == TMouseEvent
.Type
.MOUSE_MOTION
)
858 && (mouse
.getMouse1())
859 && (activeMenu
!= null)
860 && (mouse
.getAbsoluteY() == 0)
863 TMenu oldMenu
= activeMenu
;
864 for (TMenu menu
: subMenus
) {
865 menu
.setActive(false);
869 // See if we should switch menus
870 for (TMenu menu
: menus
) {
871 if ((mouse
.getAbsoluteX() >= menu
.getX())
872 && (mouse
.getAbsoluteX() < menu
.getX()
873 + menu
.getTitle().length() + 2)
875 menu
.setActive(true);
879 if (oldMenu
!= activeMenu
) {
880 // They switched menus
881 oldMenu
.setActive(false);
887 // Only switch if there are multiple windows
888 if (windows
.size() < 2) {
892 // Switch on the upclick
893 if (mouse
.getType() != TMouseEvent
.Type
.MOUSE_UP
) {
897 Collections
.sort(windows
);
898 if (windows
.get(0).isModal()) {
899 // Modal windows don't switch
903 for (TWindow window
: windows
) {
904 assert (!window
.isModal());
905 if (window
.mouseWouldHit(mouse
)) {
906 if (window
== windows
.get(0)) {
907 // Clicked on the same window, nothing to do
911 // We will be switching to another window
912 assert (windows
.get(0).getActive());
913 assert (!window
.getActive());
914 windows
.get(0).setActive(false);
915 windows
.get(0).setZ(window
.getZ());
917 window
.setActive(true);
923 // Clicked on the background, nothing to do
930 public final void closeMenu() {
931 if (activeMenu
!= null) {
932 activeMenu
.setActive(false);
934 for (TMenu menu
: subMenus
) {
935 menu
.setActive(false);
943 * Turn off a sub-menu.
945 public final void closeSubMenu() {
946 assert (activeMenu
!= null);
947 TMenu item
= subMenus
.get(subMenus
.size() - 1);
948 assert (item
!= null);
949 item
.setActive(false);
950 subMenus
.remove(subMenus
.size() - 1);
955 * Switch to the next menu.
957 * @param forward if true, then switch to the next menu in the list,
958 * otherwise switch to the previous menu in the list
960 public final void switchMenu(final boolean forward
) {
961 assert (activeMenu
!= null);
963 for (TMenu menu
: subMenus
) {
964 menu
.setActive(false);
968 for (int i
= 0; i
< menus
.size(); i
++) {
969 if (activeMenu
== menus
.get(i
)) {
971 if (i
< menus
.size() - 1) {
979 activeMenu
.setActive(false);
980 activeMenu
= menus
.get(i
);
981 activeMenu
.setActive(true);
989 * Method that TApplication subclasses can override to handle menu or
990 * posted command events.
992 * @param command command event
993 * @return if true, this event was consumed
995 protected boolean onCommand(final TCommandEvent command
) {
998 // Default: handle cmExit
999 if (command.equals(cmExit)) {
1000 if (messageBox("Confirmation", "Exit application?",
1001 TMessageBox.Type.YESNO).result == TMessageBox.Result.YES) {
1008 if (command.equals(cmShell)) {
1009 openTerminal(0, 0, TWindow.Flag.RESIZABLE);
1014 if (command.equals(cmTile)) {
1019 if (command.equals(cmCascade)) {
1024 if (command.equals(cmCloseAll)) {
1034 * Method that TApplication subclasses can override to handle menu
1037 * @param menu menu event
1038 * @return if true, this event was consumed
1040 protected boolean onMenu(final TMenuEvent menu
) {
1042 // Default: handle MID_EXIT
1043 if (menu
.getId() == TMenu
.MID_EXIT
) {
1046 if (messageBox("Confirmation", "Exit application?",
1047 TMessageBox.Type.YESNO).result == TMessageBox.Result.YES) {
1050 // System.err.printf("onMenu MID_EXIT result: quit = %s\n", quit);
1061 if (menu.id == TMenu.MID_SHELL) {
1062 openTerminal(0, 0, TWindow.Flag.RESIZABLE);
1068 if (menu
.getId() == TMenu
.MID_TILE
) {
1073 if (menu
.getId() == TMenu
.MID_CASCADE
) {
1078 if (menu
.getId() == TMenu
.MID_CLOSE_ALL
) {
1087 * Method that TApplication subclasses can override to handle keystrokes.
1089 * @param keypress keystroke event
1090 * @return if true, this event was consumed
1092 protected boolean onKeypress(final TKeypressEvent keypress
) {
1093 // Default: only menu shortcuts
1095 // Process Alt-F, Alt-E, etc. menu shortcut keys
1096 if (!keypress
.getKey().getIsKey()
1097 && keypress
.getKey().getAlt()
1098 && !keypress
.getKey().getCtrl()
1099 && (activeMenu
== null)
1102 assert (subMenus
.size() == 0);
1104 for (TMenu menu
: menus
) {
1105 if (Character
.toLowerCase(menu
.getMnemonic().getShortcut())
1106 == Character
.toLowerCase(keypress
.getKey().getCh())
1109 menu
.setActive(true);
1120 * Add a keyboard accelerator to the global hash.
1122 * @param item menu item this accelerator relates to
1123 * @param keypress keypress that will dispatch a TMenuEvent
1125 public final void addAccelerator(final TMenuItem item
,
1126 final TKeypress keypress
) {
1128 // System.err.printf("addAccelerator: key %s item %s\n", keypress, item);
1130 synchronized (accelerators
) {
1131 assert (accelerators
.get(keypress
) == null);
1132 accelerators
.put(keypress
, item
);
1137 * Recompute menu x positions based on their title length.
1139 public final void recomputeMenuX() {
1141 for (TMenu menu
: menus
) {
1143 x
+= menu
.getTitle().length() + 2;
1148 * Post an event to process and turn off the menu.
1150 * @param event new event to add to the queue
1152 public final void addMenuEvent(final TInputEvent event
) {
1153 synchronized (fillEventQueue
) {
1154 fillEventQueue
.add(event
);
1160 * Add a sub-menu to the list of open sub-menus.
1162 * @param menu sub-menu
1164 public final void addSubMenu(final TMenu menu
) {
1169 * Convenience function to add a top-level menu.
1171 * @param title menu title
1172 * @return the new menu
1174 public final TMenu
addMenu(String title
) {
1177 TMenu menu
= new TMenu(this, x
, y
, title
);
1184 * Convenience function to add a default "File" menu.
1186 * @return the new menu
1188 public final TMenu
addFileMenu() {
1189 TMenu fileMenu
= addMenu("&File");
1190 fileMenu
.addDefaultItem(TMenu
.MID_OPEN_FILE
);
1191 fileMenu
.addSeparator();
1192 fileMenu
.addDefaultItem(TMenu
.MID_SHELL
);
1193 fileMenu
.addDefaultItem(TMenu
.MID_EXIT
);
1198 * Convenience function to add a default "Edit" menu.
1200 * @return the new menu
1202 public final TMenu
addEditMenu() {
1203 TMenu editMenu
= addMenu("&Edit");
1204 editMenu
.addDefaultItem(TMenu
.MID_CUT
);
1205 editMenu
.addDefaultItem(TMenu
.MID_COPY
);
1206 editMenu
.addDefaultItem(TMenu
.MID_PASTE
);
1207 editMenu
.addDefaultItem(TMenu
.MID_CLEAR
);
1212 * Convenience function to add a default "Window" menu.
1214 * @return the new menu
1216 final public TMenu
addWindowMenu() {
1217 TMenu windowMenu
= addMenu("&Window");
1218 windowMenu
.addDefaultItem(TMenu
.MID_TILE
);
1219 windowMenu
.addDefaultItem(TMenu
.MID_CASCADE
);
1220 windowMenu
.addDefaultItem(TMenu
.MID_CLOSE_ALL
);
1221 windowMenu
.addSeparator();
1222 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_MOVE
);
1223 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_ZOOM
);
1224 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_NEXT
);
1225 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_PREVIOUS
);
1226 windowMenu
.addDefaultItem(TMenu
.MID_WINDOW_CLOSE
);
1231 * Close all open windows.
1233 private void closeAllWindows() {
1234 // Don't do anything if we are in the menu
1235 if (activeMenu
!= null) {
1238 for (TWindow window
: windows
) {
1239 closeWindow(window
);
1244 * Re-layout the open windows as non-overlapping tiles. This produces
1245 * almost the same results as Turbo Pascal 7.0's IDE.
1247 private void tileWindows() {
1248 // Don't do anything if we are in the menu
1249 if (activeMenu
!= null) {
1252 int z
= windows
.size();
1258 a
= (int)(Math
.sqrt(z
));
1262 if (((a
* b
) + c
) == z
) {
1270 int newWidth
= (getScreen().getWidth() / a
);
1271 int newHeight1
= ((getScreen().getHeight() - 1) / b
);
1272 int newHeight2
= ((getScreen().getHeight() - 1) / (b
+ c
));
1273 // System.err.printf("Z %s a %s b %s c %s newWidth %s newHeight1 %s newHeight2 %s",
1274 // z, a, b, c, newWidth, newHeight1, newHeight2);
1276 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1277 Collections
.sort(sorted
);
1278 Collections
.reverse(sorted
);
1279 for (int i
= 0; i
< sorted
.size(); i
++) {
1280 int logicalX
= i
/ b
;
1281 int logicalY
= i
% b
;
1282 if (i
>= ((a
- 1) * b
)) {
1284 logicalY
= i
- ((a
- 1) * b
);
1287 TWindow w
= sorted
.get(i
);
1288 w
.setX(logicalX
* newWidth
);
1289 w
.setWidth(newWidth
);
1290 if (i
>= ((a
- 1) * b
)) {
1291 w
.setY((logicalY
* newHeight2
) + 1);
1292 w
.setHeight(newHeight2
);
1294 w
.setY((logicalY
* newHeight1
) + 1);
1295 w
.setHeight(newHeight1
);
1301 * Re-layout the open windows as overlapping cascaded windows.
1303 private void cascadeWindows() {
1304 // Don't do anything if we are in the menu
1305 if (activeMenu
!= null) {
1310 List
<TWindow
> sorted
= new LinkedList
<TWindow
>(windows
);
1311 Collections
.sort(sorted
);
1312 Collections
.reverse(sorted
);
1313 for (TWindow w
: sorted
) {
1318 if (x
> getScreen().getWidth()) {
1321 if (y
>= getScreen().getHeight()) {