X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fmenu%2FTMenu.java;h=dbbea82bd1a9509dcd2bdd6a4e576cec84d57176;hb=e23ea53820244957b17a7000c6d3e1ff586f1ef0;hp=6d89647a6f459c9eb35df1badd79ee14ceccdb7e;hpb=a7986f7b289a17ca812a2f0cf04e48071accd636;p=fanfix.git diff --git a/src/jexer/menu/TMenu.java b/src/jexer/menu/TMenu.java index 6d89647..dbbea82 100644 --- a/src/jexer/menu/TMenu.java +++ b/src/jexer/menu/TMenu.java @@ -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"), @@ -28,6 +28,8 @@ */ package jexer.menu; +import java.util.ResourceBundle; + import jexer.TApplication; import jexer.TKeypress; import jexer.TWidget; @@ -42,60 +44,85 @@ import static jexer.TKeypress.*; /** * TMenu is a top-level collection of TMenuItems. */ -public final class TMenu extends TWindow { - - /** - * If true, this is a sub-menu. Note package private access. - */ - boolean isSubMenu = false; +public class TMenu extends TWindow { /** - * The shortcut and title. + * Translated strings. */ - private MnemonicString mnemonic; + private static final ResourceBundle i18n = ResourceBundle.getBundle(TMenu.class.getName()); - /** - * 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; + // Tools menu + public static final int MID_REPAINT = 1; + public static final int MID_VIEW_IMAGE = 2; + public static final int MID_CHANGE_FONT = 3; + // File menu - public static final int MID_EXIT = 1; + public static final int MID_NEW = 10; + public static final int MID_EXIT = 11; public static final int MID_QUIT = MID_EXIT; - public static final int MID_OPEN_FILE = 2; - public static final int MID_SHELL = 3; + public static final int MID_OPEN_FILE = 12; + public static final int MID_SHELL = 13; // Edit menu - public static final int MID_CUT = 10; - public static final int MID_COPY = 11; - public static final int MID_PASTE = 12; - public static final int MID_CLEAR = 13; + public static final int MID_CUT = 20; + public static final int MID_COPY = 21; + public static final int MID_PASTE = 22; + public static final int MID_CLEAR = 23; + + // Search menu + public static final int MID_FIND = 30; + public static final int MID_REPLACE = 31; + public static final int MID_SEARCH_AGAIN = 32; + public static final int MID_GOTO_LINE = 33; // 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 = 40; + public static final int MID_CASCADE = 41; + public static final int MID_CLOSE_ALL = 42; + public static final int MID_WINDOW_MOVE = 43; + public static final int MID_WINDOW_ZOOM = 44; + public static final int MID_WINDOW_NEXT = 45; + public static final int MID_WINDOW_PREVIOUS = 46; + public static final int MID_WINDOW_CLOSE = 47; // Help menu - public static final int MID_HELP_CONTENTS = 40; - public static final int MID_HELP_INDEX = 41; - public static final int MID_HELP_SEARCH = 42; - public static final int MID_HELP_PREVIOUS = 43; - public static final int MID_HELP_HELP = 44; - public static final int MID_HELP_ACTIVE_FILE = 45; - public static final int MID_ABOUT = 46; + public static final int MID_HELP_CONTENTS = 50; + public static final int MID_HELP_INDEX = 51; + public static final int MID_HELP_SEARCH = 52; + public static final int MID_HELP_PREVIOUS = 53; + public static final int MID_HELP_HELP = 54; + public static final int MID_HELP_ACTIVE_FILE = 55; + public static final int MID_ABOUT = 56; + + // ------------------------------------------------------------------------ + // 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. @@ -104,8 +131,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) { @@ -125,46 +152,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. @@ -174,6 +164,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()) { @@ -312,6 +303,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. * @@ -324,6 +391,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. * @@ -339,6 +421,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. * @@ -350,11 +449,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); @@ -376,6 +491,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); @@ -384,97 +511,127 @@ public final class TMenu extends TWindow { switch (id) { + case MID_REPAINT: + label = i18n.getString("menuRepaintDesktop"); + break; + + case MID_VIEW_IMAGE: + label = i18n.getString("menuViewImage"); + break; + + case MID_CHANGE_FONT: + label = i18n.getString("menuChangeFont"); + break; + + case MID_NEW: + label = i18n.getString("menuNew"); + break; + 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; default: throw new IllegalArgumentException("Invalid menu ID: " + id); } - return addItemInternal(id, label, key); + return addItemInternal(id, label, key, enabled); } /** @@ -494,7 +651,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 "&", e.g. "&File" * @return the new sub-menu */ public TSubMenu addSubMenu(final String title) {