+ /**
+ * Get the color theme.
+ *
+ * @return the theme
+ */
+ public final ColorTheme getTheme() {
+ return theme;
+ }
+
+ /**
+ * The top-level windows (but not menus).
+ */
+ List<TWindow> windows;
+
+ /**
+ * When true, exit the application.
+ */
+ private boolean quit = false;
+
+ /**
+ * When true, repaint the entire screen.
+ */
+ private boolean repaint = true;
+
+ /**
+ * Request full repaint on next screen refresh.
+ */
+ public void setRepaint() {
+ repaint = true;
+ }
+
+ /**
+ * When true, just flush updates from the screen.
+ */
+ private boolean flush = false;
+
+ /**
+ * Y coordinate of the top edge of the desktop. For now this is a
+ * constant. Someday it would be nice to have a multi-line menu or
+ * toolbars.
+ */
+ private static final int desktopTop = 1;
+
+ /**
+ * 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;
+ }
+
+ /**
+ * Y coordinate of the bottom edge of the desktop.
+ */
+ private int desktopBottom;
+
+ /**
+ * 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;
+ }
+
+ /**
+ * Public constructor.
+ *
+ * @param input an InputStream connected to the remote user, or null for
+ * System.in. If System.in is used, then on non-Windows systems it will
+ * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
+ * mode. input is always converted to a Reader with UTF-8 encoding.
+ * @param output an OutputStream connected to the remote user, or null
+ * for System.out. output is always converted to a Writer with UTF-8
+ * encoding.
+ * @throws UnsupportedEncodingException if an exception is thrown when
+ * creating the InputStreamReader
+ */
+ public TApplication(final InputStream input,
+ final OutputStream output) throws UnsupportedEncodingException {
+
+ backend = new ECMA48Backend(input, output);
+ theme = new ColorTheme();
+ desktopBottom = getScreen().getHeight() - 1;
+ eventQueue = new LinkedList<TInputEvent>();
+ windows = new LinkedList<TWindow>();
+ }
+
+ /**
+ * Invert the cell at the mouse pointer position.
+ */
+ private void drawMouse() {
+ CellAttributes attr = getScreen().getAttrXY(mouseX, mouseY);
+ attr.setForeColor(attr.getForeColor().invert());
+ attr.setBackColor(attr.getBackColor().invert());
+ getScreen().putAttrXY(mouseX, mouseY, attr, false);
+ flush = true;
+
+ if (windows.size() == 0) {
+ repaint = true;
+ }
+ }
+
+ /**
+ * Draw everything.
+ */
+ public final void drawAll() {
+ if ((flush) && (!repaint)) {
+ backend.flushScreen();
+ flush = false;
+ return;
+ }
+
+ if (!repaint) {
+ return;
+ }
+
+ // If true, the cursor is not visible
+ boolean cursor = false;
+
+ // Start with a clean screen
+ getScreen().clear();
+
+ // Draw the background
+ CellAttributes background = theme.getColor("tapplication.background");
+ getScreen().putAll(GraphicsChars.HATCH, background);
+
+ // Draw each window in reverse Z order
+ List<TWindow> sorted = new LinkedList<TWindow>(windows);
+ Collections.sort(sorted);
+ Collections.reverse(sorted);
+ for (TWindow window: sorted) {
+ 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 m: menus) {
+ CellAttributes menuColor;
+ CellAttributes menuMnemonicColor;
+ if (menu.active) {
+ menuColor = theme.getColor("tmenu.highlighted");
+ menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted");
+ } else {
+ menuColor = theme.getColor("tmenu");
+ menuMnemonicColor = theme.getColor("tmenu.mnemonic");
+ }
+ // Draw the menu title
+ getScreen().hLineXY(x, 0, menu.title.length() + 2, ' ',
+ menuColor);
+ getScreen().putStrXY(x + 1, 0, menu.title, menuColor);
+ // Draw the highlight character
+ getScreen().putCharXY(x + 1 + m.mnemonic.shortcutIdx, 0,
+ m.mnemonic.shortcut, menuMnemonicColor);
+
+ if (menu.active) {
+ menu.drawChildren();
+ // Reset the screen clipping so we can draw the next title.
+ getScreen().resetClipping();
+ }
+ x += menu.title.length + 2;
+ }
+
+ for (TMenu menu: subMenus) {
+ // Reset the screen clipping so we can draw the next sub-menu.
+ getScreen().resetClipping();
+ menu.drawChildren();
+ }
+ */
+
+ // Draw the mouse pointer
+ drawMouse();
+
+ // Place the cursor if it is visible
+ TWidget activeWidget = null;
+ if (sorted.size() > 0) {
+ activeWidget = sorted.get(sorted.size() - 1).getActiveChild();
+ if (activeWidget.visibleCursor()) {
+ getScreen().putCursor(true, activeWidget.getCursorAbsoluteX(),
+ activeWidget.getCursorAbsoluteY());
+ cursor = true;
+ }
+ }
+
+ // Kill the cursor
+ if (cursor == false) {
+ getScreen().hideCursor();
+ }
+
+ // Flush the screen contents
+ backend.flushScreen();
+
+ repaint = false;
+ flush = false;
+ }