+ public void invokeLater(final Runnable command) {
+ synchronized (invokeLaters) {
+ invokeLaters.add(command);
+ }
+ doRepaint();
+ }
+
+ /**
+ * Restore the console to sane defaults. This is meant to be used for
+ * improper exits (e.g. a caught exception in main()), and should not be
+ * necessary for normal program termination.
+ */
+ public void restoreConsole() {
+ if (backend != null) {
+ if (backend instanceof ECMA48Backend) {
+ backend.shutdown();
+ }
+ }
+ }
+
+ /**
+ * Get the Backend.
+ *
+ * @return the Backend
+ */
+ public final Backend getBackend() {
+ return backend;
+ }
+
+ /**
+ * Get the Screen.
+ *
+ * @return the Screen
+ */
+ public final Screen getScreen() {
+ if (backend instanceof TWindowBackend) {
+ // We are being rendered to a TWindow. We can't use its
+ // getScreen() method because that is how it is rendering to a
+ // hardware backend somewhere. Instead use its getOtherScreen()
+ // method.
+ return ((TWindowBackend) backend).getOtherScreen();
+ } else {
+ return backend.getScreen();
+ }
+ }
+
+ /**
+ * Get the color theme.
+ *
+ * @return the theme
+ */
+ public final ColorTheme getTheme() {
+ return theme;
+ }
+
+ /**
+ * Repaint the screen on the next update.
+ */
+ public void doRepaint() {
+ repaint = true;
+ wakeEventHandler();
+ }
+
+ /**
+ * Get Y coordinate of the top edge of the desktop.
+ *
+ * @return Y coordinate of the top edge of the desktop
+ */
+ public final int getDesktopTop() {
+ return desktopTop;
+ }
+
+ /**
+ * Get Y coordinate of the bottom edge of the desktop.
+ *
+ * @return Y coordinate of the bottom edge of the desktop
+ */
+ public final int getDesktopBottom() {
+ return desktopBottom;
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * Get the current active window.
+ *
+ * @return the active window, or null if it is not set
+ */
+ public final TWindow getActiveWindow() {
+ return activeWindow;
+ }
+
+ /**
+ * Get a (shallow) copy of the window list.
+ *
+ * @return a copy of the list of windows for this application
+ */
+ public final List<TWindow> getAllWindows() {
+ List<TWindow> result = new ArrayList<TWindow>();
+ result.addAll(windows);
+ return result;
+ }
+
+ /**
+ * Get focusFollowsMouse flag.
+ *
+ * @return true if focus follows mouse: windows automatically raised if
+ * the mouse passes over them
+ */
+ public boolean getFocusFollowsMouse() {
+ return focusFollowsMouse;
+ }
+
+ /**
+ * Set focusFollowsMouse flag.
+ *
+ * @param focusFollowsMouse if true, focus follows mouse: windows
+ * automatically raised if the mouse passes over them
+ */
+ public void setFocusFollowsMouse(final boolean focusFollowsMouse) {
+ this.focusFollowsMouse = focusFollowsMouse;
+ }
+
+ /**
+ * Display the about dialog.
+ */
+ protected void showAboutDialog() {
+ String version = getClass().getPackage().getImplementationVersion();
+ if (version == null) {
+ // This is Java 9+, use a hardcoded string here.
+ version = "0.3.1";
+ }
+ messageBox(i18n.getString("aboutDialogTitle"),
+ MessageFormat.format(i18n.getString("aboutDialogText"), version),
+ TMessageBox.Type.OK);
+ }
+
+ /**
+ * Handle the Tool | Open image menu item.
+ */
+ private void openImage() {
+ try {
+ List<String> filters = new ArrayList<String>();
+ filters.add("^.*\\.[Jj][Pp][Gg]$");
+ filters.add("^.*\\.[Jj][Pp][Ee][Gg]$");
+ filters.add("^.*\\.[Pp][Nn][Gg]$");
+ filters.add("^.*\\.[Gg][Ii][Ff]$");
+ filters.add("^.*\\.[Bb][Mm][Pp]$");
+ String filename = fileOpenBox(".", TFileOpenBox.Type.OPEN, filters);
+ if (filename != null) {
+ new TImageWindow(this, new File(filename));
+ }
+ } catch (IOException e) {
+ // Show this exception to the user.
+ new TExceptionDialog(this, e);
+ }
+ }
+
+ /**
+ * Check if application is still running.
+ *
+ * @return true if the application is running
+ */
+ public final boolean isRunning() {
+ if (quit == true) {
+ return false;
+ }
+ return true;
+ }
+
+ // ------------------------------------------------------------------------
+ // Screen refresh loop ----------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Invert the cell color at a position. This is used to track the mouse.
+ *
+ * @param x column position
+ * @param y row position
+ */
+ private void invertCell(final int x, final int y) {
+ if (debugThreads) {
+ System.err.printf("%d %s invertCell() %d %d\n",
+ System.currentTimeMillis(), Thread.currentThread(), x, y);
+
+ if (activeWindow != null) {
+ System.err.println("activeWindow.hasHiddenMouse() " +
+ activeWindow.hasHiddenMouse());
+ }
+ }
+
+ // If this cell is on top of a visible window that has requested a
+ // hidden mouse, bail out.
+ if ((activeWindow != null) && (activeMenu == null)) {
+ if ((activeWindow.hasHiddenMouse() == true)
+ && (x > activeWindow.getX())
+ && (x < activeWindow.getX() + activeWindow.getWidth() - 1)
+ && (y > activeWindow.getY())
+ && (y < activeWindow.getY() + activeWindow.getHeight() - 1)
+ ) {
+ return;
+ }
+ }
+
+ Cell cell = getScreen().getCharXY(x, y);
+ if (cell.isImage()) {
+ cell.invertImage();
+ } else {
+ if (cell.getForeColorRGB() < 0) {
+ cell.setForeColor(cell.getForeColor().invert());
+ } else {
+ cell.setForeColorRGB(cell.getForeColorRGB() ^ 0x00ffffff);
+ }
+ if (cell.getBackColorRGB() < 0) {
+ cell.setBackColor(cell.getBackColor().invert());
+ } else {
+ cell.setBackColorRGB(cell.getBackColorRGB() ^ 0x00ffffff);
+ }
+ }
+ getScreen().putCharXY(x, y, cell);
+ }
+
+ /**
+ * Draw everything.
+ */
+ private void drawAll() {
+ boolean menuIsActive = false;
+
+ if (debugThreads) {
+ System.err.printf("%d %s drawAll() enter\n",
+ System.currentTimeMillis(), Thread.currentThread());
+ }
+
+ // I don't think this does anything useful anymore...
+ if (!repaint) {
+ if (debugThreads) {
+ System.err.printf("%d %s drawAll() !repaint\n",
+ System.currentTimeMillis(), Thread.currentThread());
+ }
+ if ((oldDrawnMouseX != mouseX) || (oldDrawnMouseY != mouseY)) {
+ if (debugThreads) {
+ System.err.printf("%d %s drawAll() !repaint MOUSE\n",
+ System.currentTimeMillis(), Thread.currentThread());
+ }
+
+ // The only thing that has happened is the mouse moved.
+
+ // Redraw the old cell at that position, and save the cell at
+ // the new mouse position.
+ if (debugThreads) {
+ System.err.printf("%d %s restoreImage() %d %d\n",
+ System.currentTimeMillis(), Thread.currentThread(),
+ oldDrawnMouseX, oldDrawnMouseY);
+ }
+ oldDrawnMouseCell.restoreImage();
+ getScreen().putCharXY(oldDrawnMouseX, oldDrawnMouseY,
+ oldDrawnMouseCell);
+ oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY);
+ if ((images.size() > 0) && (backend instanceof ECMA48Backend)) {
+ // Special case: the entire row containing the mouse has
+ // to be re-drawn if it has any image data, AND any rows
+ // in between.
+ if (oldDrawnMouseY != mouseY) {
+ for (int i = oldDrawnMouseY; ;) {
+ getScreen().unsetImageRow(i);
+ if (i == mouseY) {
+ break;
+ }
+ if (oldDrawnMouseY < mouseY) {
+ i++;
+ } else {
+ i--;
+ }
+ }
+ } else {
+ getScreen().unsetImageRow(mouseY);
+ }
+ }
+
+ // Draw mouse at the new position.
+ invertCell(mouseX, mouseY);
+
+ oldDrawnMouseX = mouseX;
+ oldDrawnMouseY = mouseY;
+ }
+ if ((images.size() > 0) || getScreen().isDirty()) {
+ backend.flushScreen();
+ }
+ return;
+ }
+
+ if (debugThreads) {
+ System.err.printf("%d %s drawAll() REDRAW\n",
+ System.currentTimeMillis(), Thread.currentThread());
+ }
+
+ // If true, the cursor is not visible
+ boolean cursor = false;
+
+ // Start with a clean screen
+ getScreen().clear();
+
+ // Draw the desktop
+ if (desktop != null) {
+ desktop.drawChildren();
+ }
+
+ // Draw each window in reverse Z order
+ List<TWindow> sorted = new ArrayList<TWindow>(windows);
+ Collections.sort(sorted);
+ TWindow topLevel = null;
+ if (sorted.size() > 0) {
+ topLevel = sorted.get(0);
+ }
+ Collections.reverse(sorted);
+ for (TWindow window: sorted) {
+ if (window.isShown()) {
+ window.drawChildren();
+ }
+ }
+
+ // Draw the blank menubar line - reset the screen clipping first so
+ // it won't trim it out.
+ getScreen().resetClipping();
+ getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ',
+ theme.getColor("tmenu"));
+ // Now draw the menus.
+ int x = 1;
+ for (TMenu menu: menus) {
+ CellAttributes menuColor;
+ CellAttributes menuMnemonicColor;
+ if (menu.isActive()) {
+ menuIsActive = true;
+ menuColor = theme.getColor("tmenu.highlighted");
+ menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted");
+ topLevel = menu;
+ } else {
+ menuColor = theme.getColor("tmenu");
+ menuMnemonicColor = theme.getColor("tmenu.mnemonic");
+ }
+ // Draw the menu title
+ getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ',
+ menuColor);
+ getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor);
+ // Draw the highlight character
+ getScreen().putCharXY(x + 1 + menu.getMnemonic().getShortcutIdx(),
+ 0, menu.getMnemonic().getShortcut(), menuMnemonicColor);
+
+ if (menu.isActive()) {
+ ((TWindow) menu).drawChildren();
+ // Reset the screen clipping so we can draw the next title.
+ getScreen().resetClipping();
+ }
+ x += menu.getTitle().length() + 2;
+ }
+
+ for (TMenu menu: subMenus) {
+ // Reset the screen clipping so we can draw the next sub-menu.
+ getScreen().resetClipping();
+ ((TWindow) menu).drawChildren();
+ }
+ getScreen().resetClipping();
+
+ // 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
+ if (debugThreads) {
+ System.err.printf("%d %s restoreImage() %d %d\n",
+ System.currentTimeMillis(), Thread.currentThread(),
+ oldDrawnMouseX, oldDrawnMouseY);
+ }
+ oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY);
+ if ((images.size() > 0) && (backend instanceof ECMA48Backend)) {
+ // Special case: the entire row containing the mouse has to be
+ // re-drawn if it has any image data, AND any rows in between.
+ if (oldDrawnMouseY != mouseY) {
+ for (int i = oldDrawnMouseY; ;) {
+ getScreen().unsetImageRow(i);
+ if (i == mouseY) {
+ break;
+ }
+ if (oldDrawnMouseY < mouseY) {
+ i++;
+ } else {
+ i--;
+ }
+ }
+ } else {
+ getScreen().unsetImageRow(mouseY);
+ }
+ }
+ invertCell(mouseX, mouseY);
+ oldDrawnMouseX = mouseX;
+ oldDrawnMouseY = mouseY;
+
+ // Place the cursor if it is visible
+ if (!menuIsActive) {
+ TWidget activeWidget = null;
+ if (sorted.size() > 0) {
+ activeWidget = sorted.get(sorted.size() - 1).getActiveChild();
+ if (activeWidget.isCursorVisible()) {
+ if ((activeWidget.getCursorAbsoluteY() < desktopBottom)
+ && (activeWidget.getCursorAbsoluteY() > desktopTop)
+ ) {
+ getScreen().putCursor(true,
+ activeWidget.getCursorAbsoluteX(),
+ activeWidget.getCursorAbsoluteY());
+ cursor = true;
+ } else {
+ // Turn off the cursor. Also place it at 0,0.
+ getScreen().putCursor(false, 0, 0);
+ cursor = false;
+ }
+ }
+ }
+ }
+
+ // Kill the cursor
+ if (!cursor) {
+ getScreen().hideCursor();
+ }
+
+ // Flush the screen contents
+ if ((images.size() > 0) || getScreen().isDirty()) {
+ if (debugThreads) {
+ System.err.printf("%d %s backend.flushScreen()\n",
+ System.currentTimeMillis(), Thread.currentThread());
+ }
+ backend.flushScreen();
+ }
+
+ repaint = false;
+ }
+
+ /**
+ * Force this application to exit.
+ */
+ public void exit() {
+ quit = true;
+ synchronized (this) {
+ this.notify();
+ }
+ }
+
+ /**
+ * Subclasses can use this hook to cleanup resources. Called as the last
+ * step of TApplication.run().
+ */
+ public void onExit() {
+ // Default does nothing.
+ }
+
+ // ------------------------------------------------------------------------
+ // TWindow management -----------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Return the total number of windows.
+ *
+ * @return the total number of windows
+ */
+ public final int windowCount() {
+ return windows.size();
+ }
+
+ /**
+ * Return the number of windows that are showing.
+ *
+ * @return the number of windows that are showing on screen
+ */
+ public final int shownWindowCount() {
+ int n = 0;
+ for (TWindow w: windows) {
+ if (w.isShown()) {
+ n++;
+ }
+ }
+ return n;
+ }
+
+ /**
+ * Return the number of windows that are hidden.
+ *
+ * @return the number of windows that are hidden
+ */
+ public final int hiddenWindowCount() {
+ int n = 0;
+ for (TWindow w: windows) {
+ if (w.isHidden()) {
+ n++;
+ }
+ }
+ return n;
+ }
+
+ /**
+ * Check if a window instance is in this application's window list.
+ *
+ * @param window window to look for
+ * @return true if this window is in the list
+ */
+ public final boolean hasWindow(final TWindow window) {
+ if (windows.size() == 0) {
+ return false;
+ }
+ for (TWindow w: windows) {
+ if (w == window) {
+ assert (window.getApplication() == this);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Activate a window: bring it to the top and have it receive events.
+ *
+ * @param window the window to become the new active window
+ */
+ public void activateWindow(final TWindow window) {
+ if (hasWindow(window) == false) {
+ /*
+ * Someone has a handle to a window I don't have. Ignore this
+ * request.
+ */
+ return;
+ }
+
+ // Whatever window might be moving/dragging, stop it now.
+ for (TWindow w: windows) {
+ if (w.inMovements()) {
+ w.stopMovements();
+ }
+ }
+
+ assert (windows.size() > 0);
+
+ if (window.isHidden()) {
+ // Unhiding will also activate.
+ showWindow(window);
+ return;
+ }
+ assert (window.isShown());
+
+ if (windows.size() == 1) {
+ assert (window == windows.get(0));
+ if (activeWindow == null) {
+ activeWindow = window;
+ window.setZ(0);
+ activeWindow.setActive(true);
+ activeWindow.onFocus();
+ }
+
+ assert (window.isActive());
+ assert (activeWindow == window);
+ return;
+ }
+
+ if (activeWindow == window) {
+ assert (window.isActive());
+
+ // Window is already active, do nothing.
+ return;
+ }
+
+ assert (!window.isActive());
+ if (activeWindow != null) {
+ // TODO: see if this assertion is really necessary.
+ // assert (activeWindow.getZ() == 0);
+
+ activeWindow.setActive(false);
+
+ // Increment every window Z that is on top of window
+ for (TWindow w: windows) {
+ if (w == window) {
+ continue;
+ }
+ if (w.getZ() < window.getZ()) {
+ w.setZ(w.getZ() + 1);
+ }
+ }
+
+ // Unset activeWindow now before unfocus, so that a window
+ // lifecycle change inside onUnfocus() doesn't call
+ // switchWindow() and lead to a stack overflow.
+ TWindow oldActiveWindow = activeWindow;
+ activeWindow = null;
+ oldActiveWindow.onUnfocus();
+ }
+ activeWindow = window;
+ activeWindow.setZ(0);
+ activeWindow.setActive(true);
+ activeWindow.onFocus();
+ return;
+ }
+
+ /**
+ * Hide a window.
+ *
+ * @param window the window to hide
+ */
+ public void hideWindow(final TWindow window) {
+ if (hasWindow(window) == false) {
+ /*
+ * Someone has a handle to a window I don't have. Ignore this
+ * request.
+ */
+ return;
+ }
+
+ // Whatever window might be moving/dragging, stop it now.
+ for (TWindow w: windows) {
+ if (w.inMovements()) {
+ w.stopMovements();
+ }
+ }
+
+ assert (windows.size() > 0);
+
+ if (!window.hidden) {
+ if (window == activeWindow) {
+ if (shownWindowCount() > 1) {
+ switchWindow(true);