Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / jexer / menu / TSubMenu.java
index ed141b45393b53f9f44427fab5742cc8d21f8605..be281b52d97d58741072632e9009e5dc1c344904 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 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"),
@@ -38,19 +38,27 @@ import static jexer.TKeypress.*;
 /**
  * TSubMenu is a special case menu item that wraps another TMenu.
  */
-public final class TSubMenu extends TMenuItem {
+public class TSubMenu extends TMenuItem {
+
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * The menu window.  Note package private access.
      */
     TMenu menu;
 
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Package private constructor.
      *
      * @param parent parent widget
      * @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 "&", e.g. "&File"
      * @param x column relative to parent
      * @param y row relative to parent
      */
@@ -67,28 +75,9 @@ public final class TSubMenu extends TMenuItem {
         this.menu.isSubMenu = true;
     }
 
-    /**
-     * Draw the menu title.
-     */
-    @Override
-    public void draw() {
-        super.draw();
-
-        CellAttributes menuColor;
-        if (isAbsoluteActive()) {
-            menuColor = getTheme().getColor("tmenu.highlighted");
-        } else {
-            if (isEnabled()) {
-                menuColor = getTheme().getColor("tmenu");
-            } else {
-                menuColor = getTheme().getColor("tmenu.disabled");
-            }
-        }
-
-        // Add the arrow
-        getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.CP437[0x10],
-            menuColor);
-    }
+    // ------------------------------------------------------------------------
+    // Event handlers ---------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Handle keystrokes.
@@ -151,6 +140,32 @@ public final class TSubMenu extends TMenuItem {
         }
     }
 
+    // ------------------------------------------------------------------------
+    // TMenuItem --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Draw the menu title.
+     */
+    @Override
+    public void draw() {
+        super.draw();
+
+        CellAttributes menuColor;
+        if (isAbsoluteActive()) {
+            menuColor = getTheme().getColor("tmenu.highlighted");
+        } else {
+            if (isEnabled()) {
+                menuColor = getTheme().getColor("tmenu");
+            } else {
+                menuColor = getTheme().getColor("tmenu.disabled");
+            }
+        }
+
+        // Add the arrow
+        putCharXY(getWidth() - 2, 0, GraphicsChars.CP437[0x10], menuColor);
+    }
+
     /**
      * Override dispatch() to do nothing.
      */
@@ -179,6 +194,10 @@ public final class TSubMenu extends TMenuItem {
         return this;
     }
 
+    // ------------------------------------------------------------------------
+    // TSubMenu ---------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Convenience function to add a custom menu item.
      *
@@ -193,6 +212,21 @@ public final class TSubMenu extends TMenuItem {
         return menu.addItem(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) {
+
+        return menu.addItem(id, label, key, enabled);
+    }
+
     /**
      * Convenience function to add a menu item.
      *
@@ -204,6 +238,20 @@ public final class TSubMenu extends TMenuItem {
         return menu.addItem(id, label);
     }
 
+    /**
+     * 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) {
+
+        return menu.addItem(id, label, enabled);
+    }
+
     /**
      * Convenience function to add one of the default menu items.
      *
@@ -215,6 +263,18 @@ public final class TSubMenu extends TMenuItem {
         return menu.addDefaultItem(id);
     }
 
+    /**
+     * 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) {
+        return menu.addDefaultItem(id, enabled);
+    }
+
     /**
      * Convenience function to add a menu separator.
      */
@@ -226,12 +286,11 @@ public final class TSubMenu extends TMenuItem {
      * 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 "&", e.g. "&File"
      * @return the new sub-menu
      */
     public TSubMenu addSubMenu(final String title) {
         return menu.addSubMenu(title);
     }
 
-
 }