- if (debugThreads) {
- System.err.printf(System.currentTimeMillis() + " " +
- Thread.currentThread() + " finishEventProcessing() END\n");
- }
- }
-
- /**
- * 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);
- }
- CellAttributes attr = getScreen().getAttrXY(x, y);
- attr.setForeColor(attr.getForeColor().invert());
- attr.setBackColor(attr.getBackColor().invert());
- getScreen().putAttrXY(x, y, attr, false);
- }
-
- /**
- * Draw everything.
- */
- private void drawAll() {
- if (debugThreads) {
- System.err.printf("%d %s drawAll() enter\n",
- System.currentTimeMillis(), Thread.currentThread());
- }
-
- if (!repaint) {
- if (debugThreads) {
- System.err.printf("%d %s drawAll() !repaint\n",
- System.currentTimeMillis(), Thread.currentThread());
- }
- 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;
- }
- }
-
- 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 LinkedList<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()) {
- 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()) {
- 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();
- 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;
- 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 {
- getScreen().putCursor(false,
- activeWidget.getCursorAbsoluteX(),
- activeWidget.getCursorAbsoluteY());
- cursor = false;
- }
- }
- }
-
- // Kill the cursor
- if (!cursor) {
- getScreen().hideCursor();
- }
-
- // Flush the screen contents
- if (getScreen().isDirty()) {
- backend.flushScreen();
- }
-
- repaint = false;
- }
-
- // ------------------------------------------------------------------------
- // Main loop --------------------------------------------------------------
- // ------------------------------------------------------------------------
-
- /**
- * Force this application to exit.
- */
- public void exit() {
- quit = true;
- synchronized (this) {
- this.notify();
- }
- }
-
- /**
- * Run this application until it exits.
- */
- public void run() {
- // Start the main consumer thread
- primaryEventHandler = new WidgetEventHandler(this, true);
- (new Thread(primaryEventHandler)).start();
-
- while (!quit) {
- synchronized (this) {
- boolean doWait = false;
-
- synchronized (fillEventQueue) {
- if (fillEventQueue.size() == 0) {
- doWait = true;
- }
- }