Merge branch 'subtree'
[fanfix.git] / src / jexer / TEditorWindow.java
index 495131106e7b9fad3fa38a1f3ddadf159238d776..a28376ba3a18e8fcdfd75a300a32c180c2946385 100644 (file)
@@ -44,8 +44,10 @@ import jexer.bits.CellAttributes;
 import jexer.bits.GraphicsChars;
 import jexer.event.TCommandEvent;
 import jexer.event.TKeypressEvent;
+import jexer.event.TMenuEvent;
 import jexer.event.TMouseEvent;
 import jexer.event.TResizeEvent;
+import jexer.menu.TMenu;
 import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
@@ -73,6 +75,17 @@ public class TEditorWindow extends TScrollableWindow {
      */
     private String filename = "";
 
+    /**
+     * If true, hide the mouse after typing a keystroke.
+     */
+    private boolean hideMouseWhenTyping = true;
+
+    /**
+     * If true, the mouse should not be displayed because a keystroke was
+     * typed.
+     */
+    private boolean typingHidMouse = false;
+
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -86,7 +99,7 @@ public class TEditorWindow extends TScrollableWindow {
     public TEditorWindow(final TApplication parent, final String title) {
 
         super(parent, title, 0, 0, parent.getScreen().getWidth(),
-            parent.getScreen().getHeight() - 2, RESIZABLE);
+            parent.getDesktopBottom() - parent.getDesktopTop(), RESIZABLE);
 
         editField = addEditor("", 0, 0, getWidth() - 2, getHeight() - 2);
         setupAfterEditor();
@@ -103,7 +116,7 @@ public class TEditorWindow extends TScrollableWindow {
         final String contents) {
 
         super(parent, title, 0, 0, parent.getScreen().getWidth(),
-            parent.getScreen().getHeight() - 2, RESIZABLE);
+            parent.getDesktopBottom() - parent.getDesktopTop(), RESIZABLE);
 
         filename = title;
         editField = addEditor(contents, 0, 0, getWidth() - 2, getHeight() - 2);
@@ -121,7 +134,7 @@ public class TEditorWindow extends TScrollableWindow {
         final File file) throws IOException {
 
         super(parent, file.getName(), 0, 0, parent.getScreen().getWidth(),
-            parent.getScreen().getHeight() - 2, RESIZABLE);
+            parent.getDesktopBottom() - parent.getDesktopTop(), RESIZABLE);
 
         filename = file.getName();
         String contents = readFileData(file);
@@ -139,28 +152,27 @@ public class TEditorWindow extends TScrollableWindow {
     }
 
     // ------------------------------------------------------------------------
-    // TWindow ----------------------------------------------------------------
+    // Event handlers ---------------------------------------------------------
     // ------------------------------------------------------------------------
 
     /**
-     * Draw the window.
+     * Called by application.switchWindow() when this window gets the
+     * focus, and also by application.addWindow().
      */
-    @Override
-    public void draw() {
-        // Draw as normal.
-        super.draw();
-
-        // Add the row:col on the bottom row
-        CellAttributes borderColor = getBorder();
-        String location = String.format(" %d:%d ",
-            editField.getEditingRowNumber(),
-            editField.getEditingColumnNumber());
-        int colon = location.indexOf(':');
-        putStringXY(10 - colon, getHeight() - 1, location, borderColor);
+    public void onFocus() {
+        super.onFocus();
+        getApplication().enableMenuItem(TMenu.MID_UNDO);
+        getApplication().enableMenuItem(TMenu.MID_REDO);
+    }
 
-        if (editField.isDirty()) {
-            putCharXY(2, getHeight() - 1, GraphicsChars.OCTOSTAR, borderColor);
-        }
+    /**
+     * Called by application.switchWindow() when another window gets the
+     * focus.
+     */
+    public void onUnfocus() {
+        super.onUnfocus();
+        getApplication().disableMenuItem(TMenu.MID_UNDO);
+        getApplication().disableMenuItem(TMenu.MID_REDO);
     }
 
     /**
@@ -173,6 +185,10 @@ public class TEditorWindow extends TScrollableWindow {
         // Use TWidget's code to pass the event to the children.
         super.onMouseDown(mouse);
 
+        if (hideMouseWhenTyping) {
+            typingHidMouse = false;
+        }
+
         if (mouseOnEditor(mouse)) {
             // The editor might have changed, update the scollbars.
             setBottomValue(editField.getMaximumRowNumber());
@@ -197,12 +213,19 @@ public class TEditorWindow extends TScrollableWindow {
         // Use TWidget's code to pass the event to the children.
         super.onMouseUp(mouse);
 
+        if (hideMouseWhenTyping) {
+            typingHidMouse = false;
+        }
+
         if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
             // Clicked on vertical scrollbar
             editField.setVisibleRowNumber(getVerticalValue());
         }
-
-        // TODO: horizontal scrolling
+        if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
+            // Clicked on horizontal scrollbar
+            editField.setVisibleColumnNumber(getHorizontalValue());
+            setHorizontalValue(editField.getVisibleColumnNumber());
+        }
     }
 
     /**
@@ -215,6 +238,10 @@ public class TEditorWindow extends TScrollableWindow {
         // Use TWidget's code to pass the event to the children.
         super.onMouseMotion(mouse);
 
+        if (hideMouseWhenTyping) {
+            typingHidMouse = false;
+        }
+
         if (mouseOnEditor(mouse) && mouse.isMouse1()) {
             // The editor might have changed, update the scollbars.
             setBottomValue(editField.getMaximumRowNumber());
@@ -226,8 +253,11 @@ public class TEditorWindow extends TScrollableWindow {
                 // Clicked/dragged on vertical scrollbar
                 editField.setVisibleRowNumber(getVerticalValue());
             }
-
-            // TODO: horizontal scrolling
+            if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
+                // Clicked/dragged on horizontal scrollbar
+                editField.setVisibleColumnNumber(getHorizontalValue());
+                setHorizontalValue(editField.getVisibleColumnNumber());
+            }
         }
 
     }
@@ -239,6 +269,10 @@ public class TEditorWindow extends TScrollableWindow {
      */
     @Override
     public void onKeypress(final TKeypressEvent keypress) {
+        if (hideMouseWhenTyping) {
+            typingHidMouse = true;
+        }
+
         // Use TWidget's code to pass the event to the children.
         super.onKeypress(keypress);
 
@@ -318,6 +352,60 @@ public class TEditorWindow extends TScrollableWindow {
         super.onCommand(command);
     }
 
+    /**
+     * Handle posted menu events.
+     *
+     * @param menu menu event
+     */
+    @Override
+    public void onMenu(final TMenuEvent menu) {
+        switch (menu.getId()) {
+        case TMenu.MID_UNDO:
+            editField.undo();
+            break;
+        case TMenu.MID_REDO:
+            editField.redo();
+            break;
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // TWindow ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Draw the window.
+     */
+    @Override
+    public void draw() {
+        // Draw as normal.
+        super.draw();
+
+        // Add the row:col on the bottom row
+        CellAttributes borderColor = getBorder();
+        String location = String.format(" %d:%d ",
+            editField.getEditingRowNumber(),
+            editField.getEditingColumnNumber());
+        int colon = location.indexOf(':');
+        putStringXY(10 - colon, getHeight() - 1, location, borderColor);
+
+        if (editField.isDirty()) {
+            putCharXY(2, getHeight() - 1, GraphicsChars.OCTOSTAR, borderColor);
+        }
+    }
+
+    /**
+     * Returns true if this window does not want the application-wide mouse
+     * cursor drawn over it.
+     *
+     * @return true if this window does not want the application-wide mouse
+     * cursor drawn over it
+     */
+    @Override
+    public boolean hasHiddenMouse() {
+        return (super.hasHiddenMouse() || typingHidMouse);
+    }
+
     // ------------------------------------------------------------------------
     // TEditorWindow ----------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -344,6 +432,13 @@ public class TEditorWindow extends TScrollableWindow {
             i18n.getString("statusBarOpen"));
         statusBar.addShortcutKeypress(kbF10, cmMenu,
             i18n.getString("statusBarMenu"));
+
+        // Hide mouse when typing option
+        if (System.getProperty("jexer.TEditor.hideMouseWhenTyping",
+                "true").equals("false")) {
+
+            hideMouseWhenTyping = false;
+        }
     }
 
     /**
@@ -386,7 +481,7 @@ public class TEditorWindow extends TScrollableWindow {
      * @param mouse a mouse-based event
      * @return whether or not the mouse is on the editor
      */
-    private final boolean mouseOnEditor(final TMouseEvent mouse) {
+    private boolean mouseOnEditor(final TMouseEvent mouse) {
         if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
             && (mouse.getAbsoluteX() <  getAbsoluteX() + getWidth() - 1)
             && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)