Merge branch 'subtree'
[fanfix.git] / src / jexer / TTerminalWindow.java
index 34e7dc411d298af3612af465b641db1e95547e83..754b7a512d6f7581216a78b76c2a8a7be3838dcb 100644 (file)
  */
 package jexer;
 
-import java.awt.Font;
-import java.awt.FontMetrics;
-import java.awt.Graphics2D;
-import java.awt.image.BufferedImage;
-
-import java.io.InputStream;
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 import java.util.ResourceBundle;
 
-import jexer.backend.ECMA48Terminal;
-import jexer.backend.GlyphMaker;
-import jexer.backend.MultiScreen;
-import jexer.backend.SwingTerminal;
-import jexer.bits.Cell;
-import jexer.bits.CellAttributes;
+import jexer.menu.TMenu;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMenuEvent;
 import jexer.event.TMouseEvent;
 import jexer.event.TResizeEvent;
-import jexer.menu.TMenu;
-import jexer.tterminal.DisplayLine;
-import jexer.tterminal.DisplayListener;
-import jexer.tterminal.ECMA48;
+import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
 /**
@@ -153,6 +132,9 @@ public class TTerminalWindow extends TScrollableWindow {
         super(application, i18n.getString("windowTitle"), x, y,
             80 + 2, 24 + 2, flags);
 
+        // Require at least one line for the display.
+        setMinimumWindowHeight(3);
+
         this.closeOnExit = closeOnExit;
         vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
 
@@ -160,10 +142,14 @@ public class TTerminalWindow extends TScrollableWindow {
         addShortcutKeys();
 
         // Add shortcut text
-        newStatusBar(i18n.getString("statusBarRunning"));
+        TStatusBar statusBar = newStatusBar(i18n.getString("statusBarRunning"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp,
+            i18n.getString("statusBarHelp"));
+        statusBar.addShortcutKeypress(kbF10, cmMenu,
+            i18n.getString("statusBarMenu"));
 
         // Spin it up
-        terminal = new TTerminalWidget(this, 0, 0, new TAction() {
+        terminal = new TTerminalWidget(this, 0, 0, command, new TAction() {
             public void DO() {
                 onShellExit();
             }
@@ -202,6 +188,9 @@ public class TTerminalWindow extends TScrollableWindow {
         super(application, i18n.getString("windowTitle"), x, y,
             80 + 2, 24 + 2, flags);
 
+        // Require at least one line for the display.
+        setMinimumWindowHeight(3);
+
         this.closeOnExit = closeOnExit;
         vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
 
@@ -209,7 +198,11 @@ public class TTerminalWindow extends TScrollableWindow {
         addShortcutKeys();
 
         // Add shortcut text
-        newStatusBar(i18n.getString("statusBarRunning"));
+        TStatusBar statusBar = newStatusBar(i18n.getString("statusBarRunning"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp,
+            i18n.getString("statusBarHelp"));
+        statusBar.addShortcutKeypress(kbF10, cmMenu,
+            i18n.getString("statusBarMenu"));
 
         // Spin it up
         terminal = new TTerminalWidget(this, 0, 0, new TAction() {
@@ -228,7 +221,9 @@ public class TTerminalWindow extends TScrollableWindow {
      */
     @Override
     public void draw() {
-        setTitle(terminal.getTitle());
+        if (terminal != null) {
+            setTitle(terminal.getTitle());
+        }
         reflowData();
         super.draw();
     }
@@ -241,8 +236,10 @@ public class TTerminalWindow extends TScrollableWindow {
     @Override
     public void onResize(final TResizeEvent resize) {
         if (resize.getType() == TResizeEvent.Type.WIDGET) {
-            terminal.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
-                    getWidth() - 2, getHeight() - 2));
+            if (terminal != null) {
+                terminal.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+                        getWidth() - 2, getHeight() - 2));
+            }
 
             // Resize the scroll bars
             reflowData();
@@ -257,11 +254,13 @@ public class TTerminalWindow extends TScrollableWindow {
     @Override
     public void reflowData() {
         // Vertical scrollbar
-        terminal.reflowData();
-        setTopValue(terminal.getTopValue());
-        setBottomValue(terminal.getBottomValue());
-        setVerticalBigChange(terminal.getVerticalBigChange());
-        setVerticalValue(terminal.getVerticalValue());
+        if (terminal != null) {
+            terminal.reflowData();
+            setTopValue(terminal.getTopValue());
+            setBottomValue(terminal.getBottomValue());
+            setVerticalBigChange(terminal.getVerticalBigChange());
+            setVerticalValue(terminal.getVerticalValue());
+        }
     }
 
     /**
@@ -271,7 +270,10 @@ public class TTerminalWindow extends TScrollableWindow {
      */
     @Override
     public void onKeypress(final TKeypressEvent keypress) {
-        if (terminal.isReading()) {
+        if ((terminal != null)
+            && (terminal.isReading())
+            && (!inKeyboardResize)
+        ) {
             terminal.onKeypress(keypress);
         } else {
             super.onKeypress(keypress);
@@ -308,6 +310,13 @@ public class TTerminalWindow extends TScrollableWindow {
         }
 
         super.onMouseUp(mouse);
+
+        if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
+            // Clicked on vertical scrollbar
+            if (terminal != null) {
+                terminal.setVerticalValue(getVerticalValue());
+            }
+        }
     }
 
     /**
@@ -324,6 +333,23 @@ public class TTerminalWindow extends TScrollableWindow {
         }
 
         super.onMouseMotion(mouse);
+
+        if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
+            // Clicked/dragged on vertical scrollbar
+            if (terminal != null) {
+                terminal.setVerticalValue(getVerticalValue());
+            }
+        }
+    }
+
+    /**
+     * Get this window's help topic to load.
+     *
+     * @return the topic name
+     */
+    @Override
+    public String getHelpTopic() {
+        return "Terminal Window";
     }
 
     // ------------------------------------------------------------------------
@@ -339,7 +365,10 @@ public class TTerminalWindow extends TScrollableWindow {
      */
     @Override
     public boolean hasHiddenMouse() {
-        return terminal.hasHiddenMouse();
+        if (terminal != null) {
+            return terminal.hasHiddenMouse();
+        }
+        return false;
     }
 
     /**
@@ -423,4 +452,29 @@ public class TTerminalWindow extends TScrollableWindow {
         getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
     }
 
+    /**
+     * Wait for a period of time to get output from the launched process.
+     *
+     * @param millis millis to wait for, or 0 to wait forever
+     * @return true if the launched process has emitted something
+     */
+    public boolean waitForOutput(final int millis) {
+        if (terminal == null) {
+            return false;
+        }
+        return terminal.waitForOutput(millis);
+    }
+
+    /**
+     * Get the exit value for the emulator.
+     *
+     * @return exit value
+     */
+    public int getExitValue() {
+        if (terminal == null) {
+            return -1;
+        }
+        return terminal.getExitValue();
+    }
+
 }