*
* 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"),
*/
package jexer.menu;
+import java.util.ResourceBundle;
+
import jexer.TApplication;
import jexer.TKeypress;
import jexer.TWidget;
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;
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;
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.
*
* @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) {
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.
}
}
+ // ------------------------------------------------------------------------
+ // 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.
*
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.
*
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);
* @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);
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);
}
/**
* 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) {