X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTApplication.java;h=4b0efa9412f947a739a651ae802b7e97b68b78d2;hb=0ee88b6d705993df0d9e32cdc08c619605c7d75c;hp=f4cf2d5cb7a54a800c421e21b854b8552247725a;hpb=e16dda65585466c8987bd1efd718431450a96605;p=nikiroo-utils.git diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index f4cf2d5..4b0efa9 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2016 Kevin Lamonte + * Copyright (C) 2017 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -31,6 +31,8 @@ package jexer; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.Reader; import java.io.UnsupportedEncodingException; import java.util.Collections; import java.util.Date; @@ -56,12 +58,17 @@ import jexer.io.Screen; import jexer.menu.TMenu; import jexer.menu.TMenuItem; import static jexer.TCommand.*; +import static jexer.TKeypress.*; /** * TApplication sets up a full Text User Interface application. */ public class TApplication implements Runnable { + // ------------------------------------------------------------------------ + // Public constants ------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * If true, emit thread stuff to System.err. */ @@ -72,6 +79,12 @@ public class TApplication implements Runnable { */ private static final boolean debugEvents = false; + /** + * If true, do "smart placement" on new windows that are not specified to + * be centered. + */ + private static final boolean smartWindowPlacement = true; + /** * Two backend types are available. */ @@ -92,6 +105,10 @@ public class TApplication implements Runnable { XTERM } + // ------------------------------------------------------------------------ + // Primary/secondary event handlers --------------------------------------- + // ------------------------------------------------------------------------ + /** * WidgetEventHandler is the main event consumer loop. There are at most * two such threads in existence: the primary for normal case and a @@ -363,11 +380,24 @@ public class TApplication implements Runnable { lockoutHandleEvent = false; } + // ------------------------------------------------------------------------ + // TApplication attributes ------------------------------------------------ + // ------------------------------------------------------------------------ + /** * Access to the physical screen, keyboard, and mouse. */ private Backend backend; + /** + * Get the Backend. + * + * @return the Backend + */ + public final Backend getBackend() { + return backend; + } + /** * Get the Screen. * @@ -497,6 +527,51 @@ public class TApplication implements Runnable { return desktopBottom; } + /** + * An optional TDesktop background window that is drawn underneath + * everything else. + */ + private TDesktop desktop; + + /** + * Set the TDesktop instance. + * + * @param desktop a TDesktop instance, or null to remove the one that is + * set + */ + public final void setDesktop(final TDesktop desktop) { + if (this.desktop != null) { + this.desktop.onClose(); + } + this.desktop = desktop; + } + + /** + * Get the TDesktop instance. + * + * @return the desktop, or null if it is not set + */ + public final TDesktop getDesktop() { + return desktop; + } + + // ------------------------------------------------------------------------ + // General behavior ------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Display the about dialog. + */ + protected void showAboutDialog() { + messageBox("About", "Jexer Version " + + this.getClass().getPackage().getImplementationVersion(), + TMessageBox.Type.OK); + } + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Public constructor. * @@ -544,6 +619,40 @@ public class TApplication implements Runnable { TApplicationImpl(); } + /** + * Public constructor. The backend type will be BackendType.ECMA48. + * + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @param setRawMode if true, set System.in into raw mode with stty. + * This should in general not be used. It is here solely for Demo3, + * which uses System.in. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public TApplication(final InputStream input, final Reader reader, + final PrintWriter writer, final boolean setRawMode) { + + backend = new ECMA48Backend(this, input, reader, writer, setRawMode); + TApplicationImpl(); + } + + /** + * Public constructor. The backend type will be BackendType.ECMA48. + * + * @param input the InputStream underlying 'reader'. Its available() + * method is used to determine if reader.read() will block or not. + * @param reader a Reader connected to the remote user. + * @param writer a PrintWriter connected to the remote user. + * @throws IllegalArgumentException if input, reader, or writer are null. + */ + public TApplication(final InputStream input, final Reader reader, + final PrintWriter writer) { + + this(input, reader, writer, false); + } + /** * Public constructor. This hook enables use with new non-Jexer * backends. @@ -569,12 +678,17 @@ public class TApplication implements Runnable { timers = new LinkedList(); accelerators = new HashMap(); menuItems = new ArrayList(); + desktop = new TDesktop(this); // Setup the main consumer thread primaryEventHandler = new WidgetEventHandler(this, true); (new Thread(primaryEventHandler)).start(); } + // ------------------------------------------------------------------------ + // Screen refresh loop ---------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Invert the cell color at a position. This is used to track the mouse. * @@ -582,12 +696,13 @@ public class TApplication implements Runnable { * @param y row position */ private void invertCell(final int x, final int y) { - synchronized (getScreen()) { - CellAttributes attr = getScreen().getAttrXY(x, y); - attr.setForeColor(attr.getForeColor().invert()); - attr.setBackColor(attr.getBackColor().invert()); - getScreen().putAttrXY(x, y, attr, false); + if (debugThreads) { + System.err.printf("invertCell() %d %d\n", x, y); } + CellAttributes attr = getScreen().getAttrXY(x, y); + attr.setForeColor(attr.getForeColor().invert()); + attr.setBackColor(attr.getBackColor().invert()); + getScreen().putAttrXY(x, y, attr, false); } /** @@ -599,18 +714,23 @@ public class TApplication implements Runnable { } if (!repaint) { - if ((oldMouseX != mouseX) || (oldMouseY != mouseY)) { - // The only thing that has happened is the mouse moved. - // Clear the old position and draw the new position. - invertCell(oldMouseX, oldMouseY); - invertCell(mouseX, mouseY); - oldMouseX = mouseX; - oldMouseY = mouseY; + if (debugThreads) { + System.err.printf("drawAll() !repaint\n"); } - if (getScreen().isDirty()) { - backend.flushScreen(); + synchronized (getScreen()) { + if ((oldMouseX != mouseX) || (oldMouseY != mouseY)) { + // The only thing that has happened is the mouse moved. + // Clear the old position and draw the new position. + invertCell(oldMouseX, oldMouseY); + invertCell(mouseX, mouseY); + oldMouseX = mouseX; + oldMouseY = mouseY; + } + if (getScreen().isDirty()) { + backend.flushScreen(); + } + return; } - return; } if (debugThreads) { @@ -623,13 +743,18 @@ public class TApplication implements Runnable { // Start with a clean screen getScreen().clear(); - // Draw the background - CellAttributes background = theme.getColor("tapplication.background"); - getScreen().putAll(GraphicsChars.HATCH, background); + // Draw the desktop + if (desktop != null) { + desktop.drawChildren(); + } // Draw each window in reverse Z order List sorted = new LinkedList(windows); Collections.sort(sorted); + TWindow topLevel = null; + if (sorted.size() > 0) { + topLevel = sorted.get(0); + } Collections.reverse(sorted); for (TWindow window: sorted) { window.drawChildren(); @@ -648,6 +773,7 @@ public class TApplication implements Runnable { if (menu.isActive()) { menuColor = theme.getColor("tmenu.highlighted"); menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); + topLevel = menu; } else { menuColor = theme.getColor("tmenu"); menuMnemonicColor = theme.getColor("tmenu.mnemonic"); @@ -674,8 +800,27 @@ public class TApplication implements Runnable { menu.drawChildren(); } + // Draw the status bar of the top-level window + TStatusBar statusBar = null; + if (topLevel != null) { + statusBar = topLevel.getStatusBar(); + } + if (statusBar != null) { + getScreen().resetClipping(); + statusBar.setWidth(getScreen().getWidth()); + statusBar.setY(getScreen().getHeight() - topLevel.getY()); + statusBar.draw(); + } else { + CellAttributes barColor = new CellAttributes(); + barColor.setTo(getTheme().getColor("tstatusbar.text")); + getScreen().hLineXY(0, desktopBottom, getScreen().getWidth(), ' ', + barColor); + } + // Draw the mouse pointer invertCell(mouseX, mouseY); + oldMouseX = mouseX; + oldMouseY = mouseY; // Place the cursor if it is visible TWidget activeWidget = null; @@ -694,11 +839,17 @@ public class TApplication implements Runnable { } // Flush the screen contents - backend.flushScreen(); + if (getScreen().isDirty()) { + backend.flushScreen(); + } repaint = false; } + // ------------------------------------------------------------------------ + // Main loop -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Run this application until it exits. */ @@ -726,6 +877,9 @@ public class TApplication implements Runnable { // wait until either the backend or the consumer threads have // something to do. try { + if (debugThreads) { + System.err.println("sleep " + timeout + " millis"); + } synchronized (this) { this.wait(timeout); } @@ -833,6 +987,10 @@ public class TApplication implements Runnable { oldMouseX = 0; oldMouseY = 0; } + if (desktop != null) { + desktop.setDimensions(0, 0, resize.getWidth(), + resize.getHeight() - 1); + } return; } @@ -916,24 +1074,39 @@ public class TApplication implements Runnable { if (event instanceof TKeypressEvent) { TKeypressEvent keypress = (TKeypressEvent) event; - // See if this key matches an accelerator, and if so dispatch the - // menu event. - TKeypress keypressLowercase = keypress.getKey().toLowerCase(); - TMenuItem item = null; - synchronized (accelerators) { - item = accelerators.get(keypressLowercase); + // See if this key matches an accelerator, and is not being + // shortcutted by the active window, and if so dispatch the menu + // event. + boolean windowWillShortcut = false; + for (TWindow window: windows) { + if (window.isActive()) { + if (window.isShortcutKeypress(keypress.getKey())) { + // We do not process this key, it will be passed to + // the window instead. + windowWillShortcut = true; + } + } } - if (item != null) { - if (item.isEnabled()) { - // Let the menu item dispatch - item.dispatch(); + + if (!windowWillShortcut && !modalWindowActive()) { + TKeypress keypressLowercase = keypress.getKey().toLowerCase(); + TMenuItem item = null; + synchronized (accelerators) { + item = accelerators.get(keypressLowercase); + } + if (item != null) { + if (item.isEnabled()) { + // Let the menu item dispatch + item.dispatch(); + return; + } + } + + // Handle the keypress + if (onKeypress(keypress)) { return; } } - // Handle the keypress - if (onKeypress(keypress)) { - return; - } } if (event instanceof TCommandEvent) { @@ -949,6 +1122,7 @@ public class TApplication implements Runnable { } // Dispatch events to the active window ------------------------------- + boolean dispatchToDesktop = true; for (TWindow window: windows) { if (window.isActive()) { if (event instanceof TMouseEvent) { @@ -958,7 +1132,14 @@ public class TApplication implements Runnable { assert (mouse.getY() == mouse.getAbsoluteY()); mouse.setX(mouse.getX() - window.getX()); mouse.setY(mouse.getY() - window.getY()); + + if (window.mouseWouldHit(mouse)) { + dispatchToDesktop = false; + } + } else if (event instanceof TKeypressEvent) { + dispatchToDesktop = false; } + if (debugEvents) { System.err.printf("TApplication dispatch event: %s\n", event); @@ -967,7 +1148,14 @@ public class TApplication implements Runnable { break; } } + if (dispatchToDesktop) { + // This event is fair game for the desktop to process. + if (desktop != null) { + desktop.handleEvent(event); + } + } } + /** * Dispatch one event to the appropriate widget or application-level * event handler. This is the secondary event handler used by certain @@ -1047,31 +1235,9 @@ public class TApplication implements Runnable { } } - /** - * Get the amount of time I can sleep before missing a Timer tick. - * - * @param timeout = initial (maximum) timeout in millis - * @return number of milliseconds between now and the next timer event - */ - private long getSleepTime(final long timeout) { - Date now = new Date(); - long nowTime = now.getTime(); - long sleepTime = timeout; - for (TTimer timer: timers) { - long nextTickTime = timer.getNextTick().getTime(); - if (nextTickTime < nowTime) { - return 0; - } - - long timeDifference = nextTickTime - nowTime; - if (timeDifference < sleepTime) { - sleepTime = timeDifference; - } - } - assert (sleepTime >= 0); - assert (sleepTime <= timeout); - return sleepTime; - } + // ------------------------------------------------------------------------ + // TWindow management ----------------------------------------------------- + // ------------------------------------------------------------------------ /** * Close window. Note that the window's destructor is NOT called by this @@ -1180,10 +1346,24 @@ public class TApplication implements Runnable { * @param window new window to add */ public final void addWindow(final TWindow window) { + + // Do not add menu windows to the window list. + if (window instanceof TMenu) { + return; + } + + // Do not add the desktop to the window list. + if (window instanceof TDesktop) { + return; + } + synchronized (windows) { - // Do not allow a modal window to spawn a non-modal window - if ((windows.size() > 0) && (windows.get(0).isModal())) { - assert (window.isModal()); + // Do not allow a modal window to spawn a non-modal window. If a + // modal window is active, then this window will become modal + // too. + if (modalWindowActive()) { + window.flags |= TWindow.MODAL; + window.flags |= TWindow.CENTERED; } for (TWindow w: windows) { if (w.isActive()) { @@ -1196,6 +1376,12 @@ public class TApplication implements Runnable { window.setZ(0); window.setActive(true); window.onFocus(); + + if (((window.flags & TWindow.CENTERED) == 0) + && smartWindowPlacement) { + + doSmartPlacement(window); + } } } @@ -1208,96 +1394,331 @@ public class TApplication implements Runnable { if (windows.size() == 0) { return false; } - return windows.get(windows.size() - 1).isModal(); - } - /** - * Check if a mouse event would hit either the active menu or any open - * sub-menus. - * - * @param mouse mouse event - * @return true if the mouse would hit the active menu or an open - * sub-menu - */ - private boolean mouseOnMenu(final TMouseEvent mouse) { - assert (activeMenu != null); - List menus = new LinkedList(subMenus); - Collections.reverse(menus); - for (TMenu menu: menus) { - if (menu.mouseWouldHit(mouse)) { + for (TWindow w: windows) { + if (w.isModal()) { return true; } } - return activeMenu.mouseWouldHit(mouse); + + return false; } /** - * See if we need to switch window or activate the menu based on - * a mouse click. - * - * @param mouse mouse event + * Close all open windows. */ - private void checkSwitchFocus(final TMouseEvent mouse) { - - if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) - && (activeMenu != null) - && (mouse.getAbsoluteY() != 0) - && (!mouseOnMenu(mouse)) - ) { - // They clicked outside the active menu, turn it off - activeMenu.setActive(false); - activeMenu = null; - for (TMenu menu: subMenus) { - menu.setActive(false); - } - subMenus.clear(); - // Continue checks + private void closeAllWindows() { + // Don't do anything if we are in the menu + if (activeMenu != null) { + return; } + while (windows.size() > 0) { + closeWindow(windows.get(0)); + } + } - // See if they hit the menu bar - if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) - && (mouse.isMouse1()) - && (!modalWindowActive()) - && (mouse.getAbsoluteY() == 0) - ) { - - for (TMenu menu: subMenus) { - menu.setActive(false); + /** + * Re-layout the open windows as non-overlapping tiles. This produces + * almost the same results as Turbo Pascal 7.0's IDE. + */ + private void tileWindows() { + synchronized (windows) { + // Don't do anything if we are in the menu + if (activeMenu != null) { + return; } - subMenus.clear(); + int z = windows.size(); + if (z == 0) { + return; + } + int a = 0; + int b = 0; + a = (int)(Math.sqrt(z)); + int c = 0; + while (c < a) { + b = (z - c) / a; + if (((a * b) + c) == z) { + break; + } + c++; + } + assert (a > 0); + assert (b > 0); + assert (c < a); + int newWidth = (getScreen().getWidth() / a); + int newHeight1 = ((getScreen().getHeight() - 1) / b); + int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); - // They selected the menu, go activate it - for (TMenu menu: menus) { - if ((mouse.getAbsoluteX() >= menu.getX()) - && (mouse.getAbsoluteX() < menu.getX() - + menu.getTitle().length() + 2) - ) { - menu.setActive(true); - activeMenu = menu; + List sorted = new LinkedList(windows); + Collections.sort(sorted); + Collections.reverse(sorted); + for (int i = 0; i < sorted.size(); i++) { + int logicalX = i / b; + int logicalY = i % b; + if (i >= ((a - 1) * b)) { + logicalX = a - 1; + logicalY = i - ((a - 1) * b); + } + + TWindow w = sorted.get(i); + w.setX(logicalX * newWidth); + w.setWidth(newWidth); + if (i >= ((a - 1) * b)) { + w.setY((logicalY * newHeight2) + 1); + w.setHeight(newHeight2); } else { - menu.setActive(false); + w.setY((logicalY * newHeight1) + 1); + w.setHeight(newHeight1); } } - return; } + } - // See if they hit the menu bar - if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) - && (mouse.isMouse1()) - && (activeMenu != null) - && (mouse.getAbsoluteY() == 0) - ) { - - TMenu oldMenu = activeMenu; - for (TMenu menu: subMenus) { - menu.setActive(false); + /** + * Re-layout the open windows as overlapping cascaded windows. + */ + private void cascadeWindows() { + synchronized (windows) { + // Don't do anything if we are in the menu + if (activeMenu != null) { + return; } - subMenus.clear(); - - // See if we should switch menus - for (TMenu menu: menus) { - if ((mouse.getAbsoluteX() >= menu.getX()) - && (mouse.getAbsoluteX() < menu.getX() + int x = 0; + int y = 1; + List sorted = new LinkedList(windows); + Collections.sort(sorted); + Collections.reverse(sorted); + for (TWindow window: sorted) { + window.setX(x); + window.setY(y); + x++; + y++; + if (x > getScreen().getWidth()) { + x = 0; + } + if (y >= getScreen().getHeight()) { + y = 1; + } + } + } + } + + /** + * Place a window to minimize its overlap with other windows. + * + * @param window the window to place + */ + public final void doSmartPlacement(final TWindow window) { + // This is a pretty dumb algorithm, but seems to work. The hardest + // part is computing these "overlap" values seeking a minimum average + // overlap. + int xMin = 0; + int yMin = desktopTop; + int xMax = getScreen().getWidth() - window.getWidth() + 1; + int yMax = desktopBottom - window.getHeight() + 1; + if (xMax < xMin) { + xMax = xMin; + } + if (yMax < yMin) { + yMax = yMin; + } + + if ((xMin == xMax) && (yMin == yMax)) { + // No work to do, bail out. + return; + } + + // Compute the overlap matrix without the new window. + int width = getScreen().getWidth(); + int height = getScreen().getHeight(); + int overlapMatrix[][] = new int[width][height]; + for (TWindow w: windows) { + if (window == w) { + continue; + } + for (int x = w.getX(); x < w.getX() + w.getWidth(); x++) { + if (x == width) { + continue; + } + for (int y = w.getY(); y < w.getY() + w.getHeight(); y++) { + if (y == height) { + continue; + } + overlapMatrix[x][y]++; + } + } + } + + long oldOverlapTotal = 0; + long oldOverlapN = 0; + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + oldOverlapTotal += overlapMatrix[x][y]; + if (overlapMatrix[x][y] > 0) { + oldOverlapN++; + } + } + } + + + double oldOverlapAvg = (double) oldOverlapTotal / (double) oldOverlapN; + boolean first = true; + int windowX = window.getX(); + int windowY = window.getY(); + + // For each possible (x, y) position for the new window, compute a + // new overlap matrix. + for (int x = xMin; x < xMax; x++) { + for (int y = yMin; y < yMax; y++) { + + // Start with the matrix minus this window. + int newMatrix[][] = new int[width][height]; + for (int mx = 0; mx < width; mx++) { + for (int my = 0; my < height; my++) { + newMatrix[mx][my] = overlapMatrix[mx][my]; + } + } + + // Add this window's values to the new overlap matrix. + long newOverlapTotal = 0; + long newOverlapN = 0; + // Start by adding each new cell. + for (int wx = x; wx < x + window.getWidth(); wx++) { + if (wx == width) { + continue; + } + for (int wy = y; wy < y + window.getHeight(); wy++) { + if (wy == height) { + continue; + } + newMatrix[wx][wy]++; + } + } + // Now figure out the new value for total coverage. + for (int mx = 0; mx < width; mx++) { + for (int my = 0; my < height; my++) { + newOverlapTotal += newMatrix[x][y]; + if (newMatrix[mx][my] > 0) { + newOverlapN++; + } + } + } + double newOverlapAvg = (double) newOverlapTotal / (double) newOverlapN; + + if (first) { + // First time: just record what we got. + oldOverlapAvg = newOverlapAvg; + first = false; + } else { + // All other times: pick a new best (x, y) and save the + // overlap value. + if (newOverlapAvg < oldOverlapAvg) { + windowX = x; + windowY = y; + oldOverlapAvg = newOverlapAvg; + } + } + + } // for (int x = xMin; x < xMax; x++) + + } // for (int y = yMin; y < yMax; y++) + + // Finally, set the window's new coordinates. + window.setX(windowX); + window.setY(windowY); + } + + // ------------------------------------------------------------------------ + // TMenu management ------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Check if a mouse event would hit either the active menu or any open + * sub-menus. + * + * @param mouse mouse event + * @return true if the mouse would hit the active menu or an open + * sub-menu + */ + private boolean mouseOnMenu(final TMouseEvent mouse) { + assert (activeMenu != null); + List menus = new LinkedList(subMenus); + Collections.reverse(menus); + for (TMenu menu: menus) { + if (menu.mouseWouldHit(mouse)) { + return true; + } + } + return activeMenu.mouseWouldHit(mouse); + } + + /** + * See if we need to switch window or activate the menu based on + * a mouse click. + * + * @param mouse mouse event + */ + private void checkSwitchFocus(final TMouseEvent mouse) { + + if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) + && (activeMenu != null) + && (mouse.getAbsoluteY() != 0) + && (!mouseOnMenu(mouse)) + ) { + // They clicked outside the active menu, turn it off + activeMenu.setActive(false); + activeMenu = null; + for (TMenu menu: subMenus) { + menu.setActive(false); + } + subMenus.clear(); + // Continue checks + } + + // See if they hit the menu bar + if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN) + && (mouse.isMouse1()) + && (!modalWindowActive()) + && (mouse.getAbsoluteY() == 0) + ) { + + for (TMenu menu: subMenus) { + menu.setActive(false); + } + subMenus.clear(); + + // They selected the menu, go activate it + for (TMenu menu: menus) { + if ((mouse.getAbsoluteX() >= menu.getX()) + && (mouse.getAbsoluteX() < menu.getX() + + menu.getTitle().length() + 2) + ) { + menu.setActive(true); + activeMenu = menu; + } else { + menu.setActive(false); + } + } + return; + } + + // See if they hit the menu bar + if ((mouse.getType() == TMouseEvent.Type.MOUSE_MOTION) + && (mouse.isMouse1()) + && (activeMenu != null) + && (mouse.getAbsoluteY() == 0) + ) { + + TMenu oldMenu = activeMenu; + for (TMenu menu: subMenus) { + menu.setActive(false); + } + subMenus.clear(); + + // See if we should switch menus + for (TMenu menu: menus) { + if ((mouse.getAbsoluteX() >= menu.getX()) + && (mouse.getAbsoluteX() < menu.getX() + menu.getTitle().length() + 2) ) { menu.setActive(true); @@ -1412,114 +1833,6 @@ public class TApplication implements Runnable { } } - /** - * Method that TApplication subclasses can override to handle menu or - * posted command events. - * - * @param command command event - * @return if true, this event was consumed - */ - protected boolean onCommand(final TCommandEvent command) { - // Default: handle cmExit - if (command.equals(cmExit)) { - if (messageBox("Confirmation", "Exit application?", - TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { - quit = true; - } - return true; - } - - if (command.equals(cmShell)) { - openTerminal(0, 0, TWindow.RESIZABLE); - return true; - } - - if (command.equals(cmTile)) { - tileWindows(); - return true; - } - if (command.equals(cmCascade)) { - cascadeWindows(); - return true; - } - if (command.equals(cmCloseAll)) { - closeAllWindows(); - return true; - } - - return false; - } - - /** - * Method that TApplication subclasses can override to handle menu - * events. - * - * @param menu menu event - * @return if true, this event was consumed - */ - protected boolean onMenu(final TMenuEvent menu) { - - // Default: handle MID_EXIT - if (menu.getId() == TMenu.MID_EXIT) { - if (messageBox("Confirmation", "Exit application?", - TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { - quit = true; - } - return true; - } - - if (menu.getId() == TMenu.MID_SHELL) { - openTerminal(0, 0, TWindow.RESIZABLE); - return true; - } - - if (menu.getId() == TMenu.MID_TILE) { - tileWindows(); - return true; - } - if (menu.getId() == TMenu.MID_CASCADE) { - cascadeWindows(); - return true; - } - if (menu.getId() == TMenu.MID_CLOSE_ALL) { - closeAllWindows(); - return true; - } - return false; - } - - /** - * Method that TApplication subclasses can override to handle keystrokes. - * - * @param keypress keystroke event - * @return if true, this event was consumed - */ - protected boolean onKeypress(final TKeypressEvent keypress) { - // Default: only menu shortcuts - - // Process Alt-F, Alt-E, etc. menu shortcut keys - if (!keypress.getKey().isFnKey() - && keypress.getKey().isAlt() - && !keypress.getKey().isCtrl() - && (activeMenu == null) - ) { - - assert (subMenus.size() == 0); - - for (TMenu menu: menus) { - if (Character.toLowerCase(menu.getMnemonic().getShortcut()) - == Character.toLowerCase(keypress.getKey().getChar()) - ) { - activeMenu = menu; - menu.setActive(true); - return true; - } - } - } - - return false; - } - /** * Add a menu item to the global list. If it has a keyboard accelerator, * that will be added the global hash. @@ -1610,7 +1923,7 @@ public class TApplication implements Runnable { * * @param event new event to add to the queue */ - public final void addMenuEvent(final TInputEvent event) { + public final void postMenuEvent(final TInputEvent event) { synchronized (fillEventQueue) { fillEventQueue.add(event); } @@ -1652,6 +1965,9 @@ public class TApplication implements Runnable { fileMenu.addSeparator(); fileMenu.addDefaultItem(TMenu.MID_SHELL); fileMenu.addDefaultItem(TMenu.MID_EXIT); + TStatusBar statusBar = fileMenu.newStatusBar("File-management " + + "commands (Open, Save, Print, etc.)"); + statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); return fileMenu; } @@ -1666,6 +1982,9 @@ public class TApplication implements Runnable { editMenu.addDefaultItem(TMenu.MID_COPY); editMenu.addDefaultItem(TMenu.MID_PASTE); editMenu.addDefaultItem(TMenu.MID_CLEAR); + TStatusBar statusBar = editMenu.newStatusBar("Editor operations, " + + "undo, and Clipboard access"); + statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); return editMenu; } @@ -1685,109 +2004,177 @@ public class TApplication implements Runnable { windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT); windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS); windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE); + TStatusBar statusBar = windowMenu.newStatusBar("Open, arrange, and " + + "list windows"); + statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); return windowMenu; } /** - * Close all open windows. + * Convenience function to add a default "Help" menu. + * + * @return the new menu */ - private void closeAllWindows() { - // Don't do anything if we are in the menu - if (activeMenu != null) { - return; - } + public final TMenu addHelpMenu() { + TMenu helpMenu = addMenu("&Help"); + helpMenu.addDefaultItem(TMenu.MID_HELP_CONTENTS); + helpMenu.addDefaultItem(TMenu.MID_HELP_INDEX); + helpMenu.addDefaultItem(TMenu.MID_HELP_SEARCH); + helpMenu.addDefaultItem(TMenu.MID_HELP_PREVIOUS); + helpMenu.addDefaultItem(TMenu.MID_HELP_HELP); + helpMenu.addDefaultItem(TMenu.MID_HELP_ACTIVE_FILE); + helpMenu.addSeparator(); + helpMenu.addDefaultItem(TMenu.MID_ABOUT); + TStatusBar statusBar = helpMenu.newStatusBar("Access online help"); + statusBar.addShortcutKeypress(kbF1, cmHelp, "Help"); + return helpMenu; + } - synchronized (windows) { - for (TWindow window: windows) { - closeWindow(window); + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Method that TApplication subclasses can override to handle menu or + * posted command events. + * + * @param command command event + * @return if true, this event was consumed + */ + protected boolean onCommand(final TCommandEvent command) { + // Default: handle cmExit + if (command.equals(cmExit)) { + if (messageBox("Confirmation", "Exit application?", + TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { + quit = true; } + return true; + } + + if (command.equals(cmShell)) { + openTerminal(0, 0, TWindow.RESIZABLE); + return true; } + + if (command.equals(cmTile)) { + tileWindows(); + return true; + } + if (command.equals(cmCascade)) { + cascadeWindows(); + return true; + } + if (command.equals(cmCloseAll)) { + closeAllWindows(); + return true; + } + + return false; } /** - * Re-layout the open windows as non-overlapping tiles. This produces - * almost the same results as Turbo Pascal 7.0's IDE. + * Method that TApplication subclasses can override to handle menu + * events. + * + * @param menu menu event + * @return if true, this event was consumed */ - private void tileWindows() { - synchronized (windows) { - // Don't do anything if we are in the menu - if (activeMenu != null) { - return; - } - int z = windows.size(); - if (z == 0) { - return; - } - int a = 0; - int b = 0; - a = (int)(Math.sqrt(z)); - int c = 0; - while (c < a) { - b = (z - c) / a; - if (((a * b) + c) == z) { - break; - } - c++; + protected boolean onMenu(final TMenuEvent menu) { + + // Default: handle MID_EXIT + if (menu.getId() == TMenu.MID_EXIT) { + if (messageBox("Confirmation", "Exit application?", + TMessageBox.Type.YESNO).getResult() == TMessageBox.Result.YES) { + quit = true; } - assert (a > 0); - assert (b > 0); - assert (c < a); - int newWidth = (getScreen().getWidth() / a); - int newHeight1 = ((getScreen().getHeight() - 1) / b); - int newHeight2 = ((getScreen().getHeight() - 1) / (b + c)); + return true; + } - List sorted = new LinkedList(windows); - Collections.sort(sorted); - Collections.reverse(sorted); - for (int i = 0; i < sorted.size(); i++) { - int logicalX = i / b; - int logicalY = i % b; - if (i >= ((a - 1) * b)) { - logicalX = a - 1; - logicalY = i - ((a - 1) * b); - } + if (menu.getId() == TMenu.MID_SHELL) { + openTerminal(0, 0, TWindow.RESIZABLE); + return true; + } - TWindow w = sorted.get(i); - w.setX(logicalX * newWidth); - w.setWidth(newWidth); - if (i >= ((a - 1) * b)) { - w.setY((logicalY * newHeight2) + 1); - w.setHeight(newHeight2); - } else { - w.setY((logicalY * newHeight1) + 1); - w.setHeight(newHeight1); + if (menu.getId() == TMenu.MID_TILE) { + tileWindows(); + return true; + } + if (menu.getId() == TMenu.MID_CASCADE) { + cascadeWindows(); + return true; + } + if (menu.getId() == TMenu.MID_CLOSE_ALL) { + closeAllWindows(); + return true; + } + if (menu.getId() == TMenu.MID_ABOUT) { + showAboutDialog(); + return true; + } + return false; + } + + /** + * Method that TApplication subclasses can override to handle keystrokes. + * + * @param keypress keystroke event + * @return if true, this event was consumed + */ + protected boolean onKeypress(final TKeypressEvent keypress) { + // Default: only menu shortcuts + + // Process Alt-F, Alt-E, etc. menu shortcut keys + if (!keypress.getKey().isFnKey() + && keypress.getKey().isAlt() + && !keypress.getKey().isCtrl() + && (activeMenu == null) + && !modalWindowActive() + ) { + + assert (subMenus.size() == 0); + + for (TMenu menu: menus) { + if (Character.toLowerCase(menu.getMnemonic().getShortcut()) + == Character.toLowerCase(keypress.getKey().getChar()) + ) { + activeMenu = menu; + menu.setActive(true); + return true; } } } + + return false; } + // ------------------------------------------------------------------------ + // TTimer management ------------------------------------------------------ + // ------------------------------------------------------------------------ + /** - * Re-layout the open windows as overlapping cascaded windows. + * Get the amount of time I can sleep before missing a Timer tick. + * + * @param timeout = initial (maximum) timeout in millis + * @return number of milliseconds between now and the next timer event */ - private void cascadeWindows() { - synchronized (windows) { - // Don't do anything if we are in the menu - if (activeMenu != null) { - return; + private long getSleepTime(final long timeout) { + Date now = new Date(); + long nowTime = now.getTime(); + long sleepTime = timeout; + for (TTimer timer: timers) { + long nextTickTime = timer.getNextTick().getTime(); + if (nextTickTime < nowTime) { + return 0; } - int x = 0; - int y = 1; - List sorted = new LinkedList(windows); - Collections.sort(sorted); - Collections.reverse(sorted); - for (TWindow window: sorted) { - window.setX(x); - window.setY(y); - x++; - y++; - if (x > getScreen().getWidth()) { - x = 0; - } - if (y >= getScreen().getHeight()) { - y = 1; - } + + long timeDifference = nextTickTime - nowTime; + if (timeDifference < sleepTime) { + sleepTime = timeDifference; } } + assert (sleepTime >= 0); + assert (sleepTime <= timeout); + return sleepTime; } /** @@ -1819,6 +2206,10 @@ public class TApplication implements Runnable { } } + // ------------------------------------------------------------------------ + // Other TWindow constructors --------------------------------------------- + // ------------------------------------------------------------------------ + /** * Convenience function to spawn a message box. *