Merge branch 'master' of https://github.com/klamonte/jexer
[fanfix.git] / src / jexer / menu / TMenu.java
index a0a978651511e2a93d04151870c4cf236c278d6e..be4cf5dd12646bd107d7841a3dc49edd15996cd9 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -28,6 +28,8 @@
  */
 package jexer.menu;
 
+import java.util.ResourceBundle;
+
 import jexer.TApplication;
 import jexer.TKeypress;
 import jexer.TWidget;
@@ -45,23 +47,13 @@ import static jexer.TKeypress.*;
 public final class TMenu extends TWindow {
 
     /**
-     * If true, this is a sub-menu.  Note package private access.
+     * Translated strings.
      */
-    boolean isSubMenu = false;
+    private static final ResourceBundle i18n = ResourceBundle.getBundle(TMenu.class.getName());
 
-    /**
-     * 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;
@@ -78,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;
@@ -97,6 +95,32 @@ public final class TMenu extends TWindow {
     public static final int MID_HELP_ACTIVE_FILE        = 45;
     public static final int MID_ABOUT                   = 46;
 
+    // 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.
      *
@@ -113,9 +137,6 @@ public final class TMenu extends TWindow {
         super(parent, label, x, y, parent.getScreen().getWidth(),
             parent.getScreen().getHeight());
 
-        // My parent constructor added me as a window, get rid of that
-        parent.closeWindow(this);
-
         // Setup the menu shortcut
         mnemonic = new MnemonicString(label);
         setTitle(mnemonic.getRawLabel());
@@ -128,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.
@@ -315,6 +299,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
+        getScreen().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.
      *
@@ -342,6 +402,23 @@ public final class TMenu extends TWindow {
         return addItemInternal(id, label, key);
     }
 
+    /**
+     * 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
+     */
+    public TMenuItem addItem(final int id, final String label,
+        final TKeypress key, final boolean enabled) {
+
+        TMenuItem item = addItem(id, label, key);
+        item.setEnabled(enabled);
+        return item;
+    }
+
     /**
      * Convenience function to add a custom menu item.
      *
@@ -353,11 +430,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);
@@ -379,6 +472,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);
 
@@ -388,96 +493,114 @@ public final class TMenu extends TWindow {
         switch (id) {
 
         case MID_EXIT:
-            label = "E&xit";
+            label = i18n.getString("menuExit");
             key = kbAltX;
             break;
 
         case MID_SHELL:
-            label = "O&S Shell";
+            label = i18n.getString("menuShell");
             break;
 
         case MID_OPEN_FILE:
-            label = "&Open";
-            key = kbAltO;
+            label = i18n.getString("menuOpen");
+            key = kbF3;
             break;
 
         case MID_CUT:
-            label = "Cu&t";
+            label = i18n.getString("menuCut");
             key = kbCtrlX;
             break;
         case MID_COPY:
-            label = "&Copy";
+            label = i18n.getString("menuCopy");
             key = kbCtrlC;
             break;
         case MID_PASTE:
-            label = "&Paste";
+            label = i18n.getString("menuPaste");
             key = kbCtrlV;
             break;
         case MID_CLEAR:
-            label = "C&lear";
+            label = i18n.getString("menuClear");
             // 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 = "&Tile";
+            label = i18n.getString("menuWindowTile");
             break;
         case MID_CASCADE:
-            label = "C&ascade";
+            label = i18n.getString("menuWindowCascade");
             break;
         case MID_CLOSE_ALL:
-            label = "Cl&ose All";
+            label = i18n.getString("menuWindowCloseAll");
             break;
         case MID_WINDOW_MOVE:
-            label = "&Size/Move";
+            label = i18n.getString("menuWindowMove");
             key = kbCtrlF5;
             break;
         case MID_WINDOW_ZOOM:
-            label = "&Zoom";
+            label = i18n.getString("menuWindowZoom");
             key = kbF5;
             break;
         case MID_WINDOW_NEXT:
-            label = "&Next";
+            label = i18n.getString("menuWindowNext");
             key = kbF6;
             break;
         case MID_WINDOW_PREVIOUS:
-            label = "&Previous";
+            label = i18n.getString("menuWindowPrevious");
             key = kbShiftF6;
             break;
         case MID_WINDOW_CLOSE:
-            label = "&Close";
+            label = i18n.getString("menuWindowClose");
             key = kbCtrlW;
             break;
 
         case MID_HELP_CONTENTS:
-            label = "&Contents";
+            label = i18n.getString("menuHelpContents");
             break;
         case MID_HELP_INDEX:
-            label = "&Index";
+            label = i18n.getString("menuHelpIndex");
             key = kbShiftF1;
             break;
         case MID_HELP_SEARCH:
-            label = "&Topic search";
+            label = i18n.getString("menuHelpSearch");
             key = kbCtrlF1;
             break;
         case MID_HELP_PREVIOUS:
-            label = "&Previous topic";
+            label = i18n.getString("menuHelpPrevious");
             key = kbAltF1;
             break;
         case MID_HELP_HELP:
-            label = "&Help on help";
+            label = i18n.getString("menuHelpHelp");
             break;
         case MID_HELP_ACTIVE_FILE:
-            label = "Active &file...";
+            label = i18n.getString("menuHelpActive");
             break;
         case MID_ABOUT:
-            label = "&About...";
+            label = i18n.getString("menuHelpAbout");
+            break;
+
+        case MID_REPAINT:
+            label = i18n.getString("menuRepaintDesktop");
             break;
 
         default:
             throw new IllegalArgumentException("Invalid menu ID: " + id);
         }
 
-        return addItemInternal(id, label, key);
+        return addItemInternal(id, label, key, enabled);
     }
 
     /**