Prep for 2019 release
[fanfix.git] / src / jexer / menu / TMenu.java
index b9641e1eab4a31fd0179ffed4cb53e4e040d66e8..bfda60247a2f81ffe20afcd8599a94a06181cfe7 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -44,54 +44,16 @@ import static jexer.TKeypress.*;
 /**
  * TMenu is a top-level collection of TMenuItems.
  */
-public final class TMenu extends TWindow {
+public class TMenu extends TWindow {
 
     /**
      * Translated strings.
      */
     private static final ResourceBundle i18n = ResourceBundle.getBundle(TMenu.class.getName());
 
-    /**
-     * If true, this is a sub-menu.  Note package private access.
-     */
-    boolean isSubMenu = false;
-
-    /**
-     * The X position of the menu's title.
-     */
-    private int titleX;
-
-    /**
-     * Set the menu title X position.
-     *
-     * @param titleX the position
-     */
-    public void setTitleX(final int titleX) {
-        this.titleX = titleX;
-    }
-
-    /**
-     * Get the menu title X position.
-     *
-     * @return the position
-     */
-    public int getTitleX() {
-        return titleX;
-    }
-
-    /**
-     * The shortcut and title.
-     */
-    private MnemonicString mnemonic;
-
-    /**
-     * Get the mnemonic string.
-     *
-     * @return the full mnemonic string
-     */
-    public MnemonicString getMnemonic() {
-        return mnemonic;
-    }
+    // ------------------------------------------------------------------------
+    // Constants --------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     // Reserved menu item IDs
     public static final int MID_UNUSED          = -1;
@@ -108,15 +70,21 @@ public final class TMenu extends TWindow {
     public static final int MID_PASTE           = 12;
     public static final int MID_CLEAR           = 13;
 
+    // Search menu
+    public static final int MID_FIND            = 20;
+    public static final int MID_REPLACE         = 21;
+    public static final int MID_SEARCH_AGAIN    = 22;
+    public static final int MID_GOTO_LINE       = 23;
+
     // Window menu
-    public static final int MID_TILE            = 20;
-    public static final int MID_CASCADE         = 21;
-    public static final int MID_CLOSE_ALL       = 22;
-    public static final int MID_WINDOW_MOVE     = 23;
-    public static final int MID_WINDOW_ZOOM     = 24;
-    public static final int MID_WINDOW_NEXT     = 25;
-    public static final int MID_WINDOW_PREVIOUS = 26;
-    public static final int MID_WINDOW_CLOSE    = 27;
+    public static final int MID_TILE            = 30;
+    public static final int MID_CASCADE         = 31;
+    public static final int MID_CLOSE_ALL       = 32;
+    public static final int MID_WINDOW_MOVE     = 33;
+    public static final int MID_WINDOW_ZOOM     = 34;
+    public static final int MID_WINDOW_NEXT     = 35;
+    public static final int MID_WINDOW_PREVIOUS = 36;
+    public static final int MID_WINDOW_CLOSE    = 37;
 
     // Help menu
     public static final int MID_HELP_CONTENTS           = 40;
@@ -130,6 +98,29 @@ public final class TMenu extends TWindow {
     // Other
     public static final int MID_REPAINT         = 50;
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * If true, this is a sub-menu.  Note package private access.
+     */
+    boolean isSubMenu = false;
+
+    /**
+     * The X position of the menu's title.
+     */
+    private int titleX;
+
+    /**
+     * The shortcut and title.
+     */
+    private MnemonicString mnemonic;
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Public constructor.
      *
@@ -137,8 +128,8 @@ public final class TMenu extends TWindow {
      * @param x column relative to parent
      * @param y row relative to parent
      * @param label mnemonic menu title.  Label must contain a keyboard
-     * shortcut (mnemonic), denoted by prefixing a letter with "&",
-     * e.g. "&File"
+     * shortcut (mnemonic), denoted by prefixing a letter with "&",
+     * e.g. "&File"
      */
     public TMenu(final TApplication parent, final int x, final int y,
         final String label) {
@@ -158,46 +149,9 @@ public final class TMenu extends TWindow {
         setActive(false);
     }
 
-    /**
-     * Draw a top-level menu with title and menu items.
-     */
-    @Override
-    public void draw() {
-        CellAttributes background = getTheme().getColor("tmenu");
-
-        assert (isAbsoluteActive());
-
-        // Fill in the interior background
-        for (int i = 0; i < getHeight(); i++) {
-            hLineXY(0, i, getWidth(), ' ', background);
-        }
-
-        // Draw the box
-        char cTopLeft;
-        char cTopRight;
-        char cBottomLeft;
-        char cBottomRight;
-        char cHSide;
-
-        cTopLeft = GraphicsChars.ULCORNER;
-        cTopRight = GraphicsChars.URCORNER;
-        cBottomLeft = GraphicsChars.LLCORNER;
-        cBottomRight = GraphicsChars.LRCORNER;
-        cHSide = GraphicsChars.SINGLE_BAR;
-
-        // Place the corner characters
-        putCharXY(1, 0, cTopLeft, background);
-        putCharXY(getWidth() - 2, 0, cTopRight, background);
-        putCharXY(1, getHeight() - 1, cBottomLeft, background);
-        putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight, background);
-
-        // Draw the box lines
-        hLineXY(1 + 1, 0, getWidth() - 4, cHSide, background);
-        hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide, background);
-
-        // Draw a shadow
-        getScreen().drawBoxShadow(0, 0, getWidth(), getHeight());
-    }
+    // ------------------------------------------------------------------------
+    // Event handlers ---------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Handle mouse button presses.
@@ -207,6 +161,7 @@ public final class TMenu extends TWindow {
     @Override
     public void onMouseDown(final TMouseEvent mouse) {
         this.mouse = mouse;
+        super.onMouseDown(mouse);
 
         // Pass to children
         for (TWidget widget: getChildren()) {
@@ -345,6 +300,82 @@ public final class TMenu extends TWindow {
         }
     }
 
+    // ------------------------------------------------------------------------
+    // TWindow ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Draw a top-level menu with title and menu items.
+     */
+    @Override
+    public void draw() {
+        CellAttributes background = getTheme().getColor("tmenu");
+
+        assert (isAbsoluteActive());
+
+        // Fill in the interior background
+        for (int i = 0; i < getHeight(); i++) {
+            hLineXY(0, i, getWidth(), ' ', background);
+        }
+
+        // Draw the box
+        char cTopLeft;
+        char cTopRight;
+        char cBottomLeft;
+        char cBottomRight;
+        char cHSide;
+
+        cTopLeft = GraphicsChars.ULCORNER;
+        cTopRight = GraphicsChars.URCORNER;
+        cBottomLeft = GraphicsChars.LLCORNER;
+        cBottomRight = GraphicsChars.LRCORNER;
+        cHSide = GraphicsChars.SINGLE_BAR;
+
+        // Place the corner characters
+        putCharXY(1, 0, cTopLeft, background);
+        putCharXY(getWidth() - 2, 0, cTopRight, background);
+        putCharXY(1, getHeight() - 1, cBottomLeft, background);
+        putCharXY(getWidth() - 2, getHeight() - 1, cBottomRight, background);
+
+        // Draw the box lines
+        hLineXY(1 + 1, 0, getWidth() - 4, cHSide, background);
+        hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide, background);
+
+        // Draw a shadow
+        drawBoxShadow(0, 0, getWidth(), getHeight());
+    }
+
+    // ------------------------------------------------------------------------
+    // TMenu ------------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Set the menu title X position.
+     *
+     * @param titleX the position
+     */
+    public void setTitleX(final int titleX) {
+        this.titleX = titleX;
+    }
+
+    /**
+     * Get the menu title X position.
+     *
+     * @return the position
+     */
+    public int getTitleX() {
+        return titleX;
+    }
+
+    /**
+     * Get the mnemonic string.
+     *
+     * @return the full mnemonic string
+     */
+    public MnemonicString getMnemonic() {
+        return mnemonic;
+    }
+
     /**
      * Convenience function to add a menu item.
      *
@@ -357,6 +388,21 @@ public final class TMenu extends TWindow {
         return addItemInternal(id, label, null);
     }
 
+    /**
+     * Convenience function to add a menu item.
+     *
+     * @param id menu item ID.  Must be greater than 1024.
+     * @param label menu item label
+     * @param enabled default state for enabled
+     * @return the new menu item
+     */
+    public TMenuItem addItem(final int id, final String label,
+        final boolean enabled) {
+
+        assert (id >= 1024);
+        return addItemInternal(id, label, null, enabled);
+    }
+
     /**
      * Convenience function to add a custom menu item.
      *
@@ -400,11 +446,27 @@ public final class TMenu extends TWindow {
     private TMenuItem addItemInternal(final int id, final String label,
         final TKeypress key) {
 
+        return addItemInternal(id, label, key, true);
+    }
+
+    /**
+     * Convenience function to add a custom menu item.
+     *
+     * @param id menu item ID.  Must be greater than 1024.
+     * @param label menu item label
+     * @param key global keyboard accelerator
+     * @param enabled default state for enabled
+     * @return the new menu item
+     */
+    private TMenuItem addItemInternal(final int id, final String label,
+        final TKeypress key, final boolean enabled) {
+
         int newY = getChildren().size() + 1;
         assert (newY < getHeight());
 
         TMenuItem menuItem = new TMenuItem(this, id, 1, newY, label);
         menuItem.setKey(key);
+        menuItem.setEnabled(enabled);
         setHeight(getHeight() + 1);
         if (menuItem.getWidth() + 2 > getWidth()) {
             setWidth(menuItem.getWidth() + 2);
@@ -426,6 +488,18 @@ public final class TMenu extends TWindow {
      * @return the new menu item
      */
     public TMenuItem addDefaultItem(final int id) {
+        return addDefaultItem(id, true);
+    }
+
+    /**
+     * Convenience function to add one of the default menu items.
+     *
+     * @param id menu item ID.  Must be between 0 (inclusive) and 1023
+     * (inclusive).
+     * @param enabled default state for enabled
+     * @return the new menu item
+     */
+    public TMenuItem addDefaultItem(final int id, final boolean enabled) {
         assert (id >= 0);
         assert (id < 1024);
 
@@ -445,7 +519,7 @@ public final class TMenu extends TWindow {
 
         case MID_OPEN_FILE:
             label = i18n.getString("menuOpen");
-            key = kbAltO;
+            key = kbF3;
             break;
 
         case MID_CUT:
@@ -465,6 +539,20 @@ public final class TMenu extends TWindow {
             // key = kbDel;
             break;
 
+        case MID_FIND:
+            label = i18n.getString("menuFind");
+            break;
+        case MID_REPLACE:
+            label = i18n.getString("menuReplace");
+            break;
+        case MID_SEARCH_AGAIN:
+            label = i18n.getString("menuSearchAgain");
+            break;
+        case MID_GOTO_LINE:
+            label = i18n.getString("menuGotoLine");
+            key = kbCtrlL;
+            break;
+
         case MID_TILE:
             label = i18n.getString("menuWindowTile");
             break;
@@ -528,7 +616,7 @@ public final class TMenu extends TWindow {
             throw new IllegalArgumentException("Invalid menu ID: " + id);
         }
 
-        return addItemInternal(id, label, key);
+        return addItemInternal(id, label, key, enabled);
     }
 
     /**
@@ -548,7 +636,7 @@ public final class TMenu extends TWindow {
      * Convenience function to add a sub-menu.
      *
      * @param title menu title.  Title must contain a keyboard shortcut,
-     * denoted by prefixing a letter with "&", e.g. "&File"
+     * denoted by prefixing a letter with "&amp;", e.g. "&amp;File"
      * @return the new sub-menu
      */
     public TSubMenu addSubMenu(final String title) {