X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fmenu%2FTMenu.java;h=6d746df0c42ebc3f6da6ede018a33ff439233235;hb=12b90437b5f22c2ae6e9b9b14c3b62b60f6143e5;hp=3120635996e503fd43feb4bc24d3c031c43911bf;hpb=051e29138b18fb4b731a72f8727475b10e4c74e4;p=fanfix.git diff --git a/src/jexer/menu/TMenu.java b/src/jexer/menu/TMenu.java index 3120635..6d746df 100644 --- a/src/jexer/menu/TMenu.java +++ b/src/jexer/menu/TMenu.java @@ -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"), @@ -37,6 +37,7 @@ import jexer.TWindow; import jexer.bits.CellAttributes; import jexer.bits.GraphicsChars; import jexer.bits.MnemonicString; +import jexer.bits.StringUtils; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; import static jexer.TKeypress.*; @@ -58,45 +59,79 @@ public class TMenu extends TWindow { // 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_SCREEN_OPTIONS = 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 = 20; - public static final int MID_REPLACE = 21; - public static final int MID_SEARCH_AGAIN = 22; - public static final int MID_GOTO_LINE = 23; + 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 = 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; + 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; - - // Other - public static final int MID_REPAINT = 50; + 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; + + // Table menu + public static final int MID_TABLE_RENAME_ROW = 60; + public static final int MID_TABLE_RENAME_COLUMN = 61; + public static final int MID_TABLE_VIEW_ROW_LABELS = 70; + public static final int MID_TABLE_VIEW_COLUMN_LABELS = 71; + public static final int MID_TABLE_VIEW_HIGHLIGHT_ROW = 72; + public static final int MID_TABLE_VIEW_HIGHLIGHT_COLUMN = 73; + public static final int MID_TABLE_BORDER_NONE = 80; + public static final int MID_TABLE_BORDER_ALL = 81; + public static final int MID_TABLE_BORDER_CELL_NONE = 82; + public static final int MID_TABLE_BORDER_CELL_ALL = 83; + public static final int MID_TABLE_BORDER_RIGHT = 84; + public static final int MID_TABLE_BORDER_LEFT = 85; + public static final int MID_TABLE_BORDER_TOP = 86; + public static final int MID_TABLE_BORDER_BOTTOM = 87; + public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM = 88; + public static final int MID_TABLE_BORDER_THICK_BOTTOM = 89; + public static final int MID_TABLE_DELETE_LEFT = 100; + public static final int MID_TABLE_DELETE_UP = 101; + public static final int MID_TABLE_DELETE_ROW = 102; + public static final int MID_TABLE_DELETE_COLUMN = 103; + public static final int MID_TABLE_INSERT_LEFT = 104; + public static final int MID_TABLE_INSERT_RIGHT = 105; + public static final int MID_TABLE_INSERT_ABOVE = 106; + public static final int MID_TABLE_INSERT_BELOW = 107; + public static final int MID_TABLE_COLUMN_NARROW = 110; + public static final int MID_TABLE_COLUMN_WIDEN = 111; + public static final int MID_TABLE_FILE_OPEN_CSV = 115; + public static final int MID_TABLE_FILE_SAVE_CSV = 116; + public static final int MID_TABLE_FILE_SAVE_TEXT = 117; // ------------------------------------------------------------------------ // Variables -------------------------------------------------------------- @@ -143,7 +178,7 @@ public class TMenu extends TWindow { assert (mnemonic.getShortcutIdx() >= 0); // Recompute width and height to reflect an empty menu - setWidth(getTitle().length() + 4); + setWidth(StringUtils.width(getTitle()) + 4); setHeight(2); setActive(false); @@ -161,6 +196,7 @@ public class TMenu extends TWindow { @Override public void onMouseDown(final TMouseEvent mouse) { this.mouse = mouse; + super.onMouseDown(mouse); // Pass to children for (TWidget widget: getChildren()) { @@ -236,15 +272,21 @@ public class TMenu extends TWindow { /* System.err.printf("keypress: %s active child: %s\n", keypress, getActiveChild()); - */ + */ if (getActiveChild() != this) { - if ((getActiveChild() instanceof TSubMenu) - || (getActiveChild() instanceof TMenu) - ) { + if (getActiveChild() instanceof TMenu) { getActiveChild().onKeypress(keypress); return; } + + if (getActiveChild() instanceof TSubMenu) { + TSubMenu subMenu = (TSubMenu) getActiveChild(); + if (subMenu.menu.isActive()) { + subMenu.onKeypress(keypress); + return; + } + } } if (keypress.equals(kbEsc)) { @@ -276,12 +318,18 @@ public class TMenu extends TWindow { if (!keypress.getKey().isFnKey() && !keypress.getKey().isAlt() && !keypress.getKey().isCtrl()) { + + // System.err.println("Checking children for mnemonic..."); + for (TWidget widget: getChildren()) { TMenuItem item = (TMenuItem) widget; - if ((item.getMnemonic() != null) + if ((item.isEnabled() == true) + && (item.getMnemonic() != null) && (Character.toLowerCase(item.getMnemonic().getShortcut()) == Character.toLowerCase(keypress.getKey().getChar())) ) { + // System.err.println("activate: " + item); + // Send an enter keystroke to it activate(item); item.handleEvent(new TKeypressEvent(kbEnter)); @@ -341,7 +389,7 @@ public class TMenu extends TWindow { hLineXY(1 + 1, getHeight() - 1, getWidth() - 4, cHSide, background); // Draw a shadow - getScreen().drawBoxShadow(0, 0, getWidth(), getHeight()); + drawBoxShadow(0, 0, getWidth(), getHeight()); } // ------------------------------------------------------------------------ @@ -383,10 +431,24 @@ public class TMenu extends TWindow { * @return the new menu item */ public TMenuItem addItem(final int id, final String label) { - assert (id >= 1024); 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. * @@ -489,9 +551,27 @@ public class TMenu extends TWindow { String label; TKeypress key = null; + boolean checkable = false; + boolean checked = false; switch (id) { + case MID_REPAINT: + label = i18n.getString("menuRepaintDesktop"); + break; + + case MID_VIEW_IMAGE: + label = i18n.getString("menuViewImage"); + break; + + case MID_SCREEN_OPTIONS: + label = i18n.getString("menuScreenOptions"); + break; + + case MID_NEW: + label = i18n.getString("menuNew"); + break; + case MID_EXIT: label = i18n.getString("menuExit"); key = kbAltX; @@ -531,10 +611,10 @@ public class TMenu extends TWindow { break; case MID_SEARCH_AGAIN: label = i18n.getString("menuSearchAgain"); + key = kbCtrlL; break; case MID_GOTO_LINE: label = i18n.getString("menuGotoLine"); - key = kbCtrlL; break; case MID_TILE: @@ -592,15 +672,112 @@ public class TMenu extends TWindow { label = i18n.getString("menuHelpAbout"); break; - case MID_REPAINT: - label = i18n.getString("menuRepaintDesktop"); + case MID_TABLE_RENAME_COLUMN: + label = i18n.getString("menuTableRenameColumn"); + break; + case MID_TABLE_RENAME_ROW: + label = i18n.getString("menuTableRenameRow"); + break; + case MID_TABLE_VIEW_ROW_LABELS: + label = i18n.getString("menuTableViewRowLabels"); + checkable = true; + checked = true; + break; + case MID_TABLE_VIEW_COLUMN_LABELS: + label = i18n.getString("menuTableViewColumnLabels"); + checkable = true; + checked = true; + break; + case MID_TABLE_VIEW_HIGHLIGHT_ROW: + label = i18n.getString("menuTableViewHighlightRow"); + checkable = true; + checked = true; + break; + case MID_TABLE_VIEW_HIGHLIGHT_COLUMN: + label = i18n.getString("menuTableViewHighlightColumn"); + checkable = true; + checked = true; + break; + + case MID_TABLE_BORDER_NONE: + label = i18n.getString("menuTableBorderNone"); + break; + case MID_TABLE_BORDER_ALL: + label = i18n.getString("menuTableBorderAll"); + break; + case MID_TABLE_BORDER_CELL_NONE: + label = i18n.getString("menuTableBorderCellNone"); + break; + case MID_TABLE_BORDER_CELL_ALL: + label = i18n.getString("menuTableBorderCellAll"); + break; + case MID_TABLE_BORDER_RIGHT: + label = i18n.getString("menuTableBorderRight"); + break; + case MID_TABLE_BORDER_LEFT: + label = i18n.getString("menuTableBorderLeft"); + break; + case MID_TABLE_BORDER_TOP: + label = i18n.getString("menuTableBorderTop"); + break; + case MID_TABLE_BORDER_BOTTOM: + label = i18n.getString("menuTableBorderBottom"); + break; + case MID_TABLE_BORDER_DOUBLE_BOTTOM: + label = i18n.getString("menuTableBorderDoubleBottom"); + break; + case MID_TABLE_BORDER_THICK_BOTTOM: + label = i18n.getString("menuTableBorderThickBottom"); + break; + case MID_TABLE_DELETE_LEFT: + label = i18n.getString("menuTableDeleteLeft"); + break; + case MID_TABLE_DELETE_UP: + label = i18n.getString("menuTableDeleteUp"); + break; + case MID_TABLE_DELETE_ROW: + label = i18n.getString("menuTableDeleteRow"); + break; + case MID_TABLE_DELETE_COLUMN: + label = i18n.getString("menuTableDeleteColumn"); + break; + case MID_TABLE_INSERT_LEFT: + label = i18n.getString("menuTableInsertLeft"); + break; + case MID_TABLE_INSERT_RIGHT: + label = i18n.getString("menuTableInsertRight"); + break; + case MID_TABLE_INSERT_ABOVE: + label = i18n.getString("menuTableInsertAbove"); + break; + case MID_TABLE_INSERT_BELOW: + label = i18n.getString("menuTableInsertBelow"); + break; + case MID_TABLE_COLUMN_NARROW: + label = i18n.getString("menuTableColumnNarrow"); + key = kbShiftLeft; + break; + case MID_TABLE_COLUMN_WIDEN: + label = i18n.getString("menuTableColumnWiden"); + key = kbShiftRight; + break; + case MID_TABLE_FILE_OPEN_CSV: + label = i18n.getString("menuTableFileOpenCsv"); + break; + case MID_TABLE_FILE_SAVE_CSV: + label = i18n.getString("menuTableFileSaveCsv"); + break; + case MID_TABLE_FILE_SAVE_TEXT: + label = i18n.getString("menuTableFileSaveText"); break; default: throw new IllegalArgumentException("Invalid menu ID: " + id); } - return addItemInternal(id, label, key, enabled); + TMenuItem item = addItemInternal(id, label, key, enabled); + item.setCheckable(checkable); + return item; } /**