retrofit from GJexer
[nikiroo-utils.git] / src / jexer / TApplication.java
index 96f91f2943696d38a7f6e4d72b8fab2036da3482..20406b1f8e82aa654efa86081c085d5f3883af03 100644 (file)
  */
 package jexer;
 
+import java.io.File;
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -812,11 +814,23 @@ public class TApplication implements Runnable {
             closeAllWindows();
             return true;
         }
+        if (menu.getId() == TMenu.MID_ABOUT) {
+            showAboutDialog();
+            return true;
+        }
         if (menu.getId() == TMenu.MID_REPAINT) {
             getScreen().clearPhysical();
             doRepaint();
             return true;
         }
+        if (menu.getId() == TMenu.MID_VIEW_IMAGE) {
+            openImage();
+            return true;
+        }
+        if (menu.getId() == TMenu.MID_CHANGE_FONT) {
+            new TFontChooserWindow(this);
+            return true;
+        }
         return false;
     }
 
@@ -969,7 +983,10 @@ public class TApplication implements Runnable {
                 mouseX = mouse.getX();
                 mouseY = mouse.getY();
             } else {
-                if (mouse.getType() == TMouseEvent.Type.MOUSE_UP) {
+                if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
+                    && (!mouse.isMouseWheelUp())
+                    && (!mouse.isMouseWheelDown())
+                ) {
                     if ((mouse.getTime().getTime() - lastMouseUpTime) <
                         doubleClickTime) {
 
@@ -1151,7 +1168,10 @@ public class TApplication implements Runnable {
                 mouseX = mouse.getX();
                 mouseY = mouse.getY();
             } else {
-                if (mouse.getType() == TMouseEvent.Type.MOUSE_UP) {
+                if ((mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
+                    && (!mouse.isMouseWheelUp())
+                    && (!mouse.isMouseWheelDown())
+                ) {
                     if ((mouse.getTime().getTime() - lastMouseUpTime) <
                         doubleClickTime) {
 
@@ -1314,6 +1334,7 @@ public class TApplication implements Runnable {
         synchronized (invokeLaters) {
             invokeLaters.add(command);
         }
+        doRepaint();
     }
 
     /**
@@ -1452,6 +1473,41 @@ public class TApplication implements Runnable {
         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.0";
+        }
+        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);
+        }
+    }
+
     // ------------------------------------------------------------------------
     // Screen refresh loop ----------------------------------------------------
     // ------------------------------------------------------------------------
@@ -1466,7 +1522,26 @@ public class TApplication implements Runnable {
         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();
@@ -1959,29 +2034,33 @@ public class TApplication implements Runnable {
             int z = window.getZ();
             window.setZ(-1);
             window.onUnfocus();
+            windows.remove(window);
             Collections.sort(windows);
-            windows.remove(0);
             activeWindow = null;
+            int newZ = 0;
+            boolean foundNextWindow = false;
+
             for (TWindow w: windows) {
+                w.setZ(newZ);
+                newZ++;
 
                 // Do not activate a hidden window.
                 if (w.isHidden()) {
                     continue;
                 }
 
-                if (w.getZ() > z) {
-                    w.setZ(w.getZ() - 1);
-                    if (w.getZ() == 0) {
-                        w.setActive(true);
-                        w.onFocus();
-                        assert (activeWindow == null);
-                        activeWindow = w;
-                    } else {
-                        if (w.isActive()) {
-                            w.setActive(false);
-                            w.onUnfocus();
-                        }
-                    }
+                if (foundNextWindow == false) {
+                    foundNextWindow = true;
+                    w.setActive(true);
+                    w.onFocus();
+                    assert (activeWindow == null);
+                    activeWindow = w;
+                    continue;
+                }
+
+                if (w.isActive()) {
+                    w.setActive(false);
+                    w.onUnfocus();
                 }
             }
         }
@@ -2557,7 +2636,7 @@ public class TApplication implements Runnable {
 
         if (((focusFollowsMouse == true)
                 && (mouse.getType() == TMouseEvent.Type.MOUSE_MOTION))
-            || (mouse.getType() == TMouseEvent.Type.MOUSE_UP)
+            || (mouse.getType() == TMouseEvent.Type.MOUSE_DOWN)
         ) {
             synchronized (windows) {
                 Collections.sort(windows);
@@ -2873,6 +2952,22 @@ public class TApplication implements Runnable {
         return menu;
     }
 
+    /**
+     * Convenience function to add a default tools (hamburger) menu.
+     *
+     * @return the new menu
+     */
+    public final TMenu addToolMenu() {
+        TMenu toolMenu = addMenu(i18n.getString("toolMenuTitle"));
+        toolMenu.addDefaultItem(TMenu.MID_REPAINT);
+        toolMenu.addDefaultItem(TMenu.MID_VIEW_IMAGE);
+        toolMenu.addDefaultItem(TMenu.MID_CHANGE_FONT);
+        TStatusBar toolStatusBar = toolMenu.newStatusBar(i18n.
+            getString("toolMenuStatus"));
+        toolStatusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));
+        return toolMenu;
+    }
+
     /**
      * Convenience function to add a default "File" menu.
      *
@@ -3222,7 +3317,7 @@ public class TApplication implements Runnable {
     public final TTerminalWindow openTerminal(final int x, final int y,
         final int flags, final String commandLine) {
 
-        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s"));
+        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+"));
     }
 
     /**
@@ -3239,7 +3334,7 @@ public class TApplication implements Runnable {
     public final TTerminalWindow openTerminal(final int x, final int y,
         final int flags, final String commandLine, final boolean closeOnExit) {
 
-        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s"),
+        return new TTerminalWindow(this, x, y, flags, commandLine.split("\\s+"),
             closeOnExit);
     }