X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTApplication.java;h=9d27c10f5420052103cee046baae697ca8c2bd6e;hb=12b90437b5f22c2ae6e9b9b14c3b62b60f6143e5;hp=264b8c8220e3fddb3b062e612a97ed74761f22c2;hpb=abb84744979f74e96ec604357895fc3130aec913;p=nikiroo-utils.git diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 264b8c8..9d27c10 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -48,6 +48,7 @@ import java.util.ResourceBundle; import jexer.bits.Cell; import jexer.bits.CellAttributes; import jexer.bits.ColorTheme; +import jexer.bits.StringUtils; import jexer.event.TCommandEvent; import jexer.event.TInputEvent; import jexer.event.TKeypressEvent; @@ -62,6 +63,7 @@ import jexer.backend.ECMA48Backend; import jexer.backend.TWindowBackend; import jexer.menu.TMenu; import jexer.menu.TMenuItem; +import jexer.menu.TSubMenu; import static jexer.TCommand.*; import static jexer.TKeypress.*; @@ -131,6 +133,11 @@ public class TApplication implements Runnable { */ private volatile WidgetEventHandler secondaryEventHandler; + /** + * The screen handler thread. + */ + private volatile ScreenHandler screenHandler; + /** * The widget receiving events from the secondary event handler thread. */ @@ -263,7 +270,7 @@ public class TApplication implements Runnable { * constant. Someday it would be nice to have a multi-line menu or * toolbars. */ - private static final int desktopTop = 1; + private int desktopTop = 1; /** * Y coordinate of the bottom edge of the desktop. @@ -283,15 +290,41 @@ public class TApplication implements Runnable { private boolean focusFollowsMouse = false; /** - * The images that might be displayed. Note package private access. + * If true, display a text-based mouse cursor. + */ + private boolean textMouse = true; + + /** + * If true, hide the mouse after typing a keystroke. + */ + private boolean hideMouseWhenTyping = false; + + /** + * If true, the mouse should not be displayed because a keystroke was + * typed. + */ + private boolean typingHidMouse = false; + + /** + * If true, hide the status bar. + */ + private boolean hideStatusBar = false; + + /** + * If true, hide the menu bar. */ - private List images; + private boolean hideMenuBar = false; /** * The list of commands to run before the next I/O check. */ private List invokeLaters = new LinkedList(); + /** + * The last time the screen was resized. + */ + private long screenResizeTime = 0; + /** * WidgetEventHandler is the main event consumer loop. There are at most * two such threads in existence: the primary for normal case and a @@ -432,9 +465,9 @@ public class TApplication implements Runnable { // resumes working on the primary. application.secondaryEventHandler = null; - // DO NOT UNLOCK. Primary thread just came back from - // primaryHandleEvent() and will unlock in the else - // block below. Just wake it up. + // We are ready to exit, wake up the primary thread. + // Remember that it is currently sleeping inside its + // primaryHandleEvent(). synchronized (application.primaryEventHandler) { application.primaryEventHandler.notify(); } @@ -454,6 +487,94 @@ public class TApplication implements Runnable { } } + /** + * ScreenHandler pushes screen updates to the physical device. + */ + private class ScreenHandler implements Runnable { + /** + * The main application. + */ + private TApplication application; + + /** + * The dirty flag. + */ + private boolean dirty = false; + + /** + * Public constructor. + * + * @param application the main application + */ + public ScreenHandler(final TApplication application) { + this.application = application; + } + + /** + * The screen update loop. + */ + public void run() { + // Wrap everything in a try, so that if we go belly up we can let + // the user have their terminal back. + try { + runImpl(); + } catch (Throwable t) { + this.application.restoreConsole(); + t.printStackTrace(); + this.application.exit(); + } + } + + /** + * The update loop. + */ + private void runImpl() { + + // Loop forever + while (!application.quit) { + + // Wait until application notifies me + while (!application.quit) { + try { + synchronized (this) { + if (dirty) { + dirty = false; + break; + } + + // Always check within 50 milliseconds. + this.wait(50); + } + } catch (InterruptedException e) { + // SQUASH + } + } // while (!application.quit) + + // Flush the screen contents + if (debugThreads) { + System.err.printf("%d %s backend.flushScreen()\n", + System.currentTimeMillis(), Thread.currentThread()); + } + synchronized (getScreen()) { + backend.flushScreen(); + } + } // while (true) (main runnable loop) + + // Shutdown the user I/O thread(s) + backend.shutdown(); + } + + /** + * Set the dirty flag. + */ + public void setDirty() { + synchronized (this) { + dirty = true; + } + } + + } + // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -594,8 +715,32 @@ public class TApplication implements Runnable { * Finish construction once the backend is set. */ private void TApplicationImpl() { + // Text block mouse option + if (System.getProperty("jexer.textMouse", "true").equals("false")) { + textMouse = false; + } + + // Hide mouse when typing option + if (System.getProperty("jexer.hideMouseWhenTyping", + "false").equals("true")) { + + hideMouseWhenTyping = true; + } + + // Hide status bar option + if (System.getProperty("jexer.hideStatusBar", + "false").equals("true")) { + hideStatusBar = true; + } + + // Hide menu bar option + if (System.getProperty("jexer.hideMenuBar", "false").equals("true")) { + hideMenuBar = true; + } + theme = new ColorTheme(); - desktopBottom = getScreen().getHeight() - 1; + desktopTop = (hideMenuBar ? 0 : 1); + desktopBottom = getScreen().getHeight() - 1 + (hideStatusBar ? 1 : 0); fillEventQueue = new LinkedList(); drainEventQueue = new LinkedList(); windows = new LinkedList(); @@ -605,7 +750,6 @@ public class TApplication implements Runnable { accelerators = new HashMap(); menuItems = new LinkedList(); desktop = new TDesktop(this); - images = new LinkedList(); // Special case: the Swing backend needs to have a timer to drive its // blink state. @@ -628,6 +772,7 @@ public class TApplication implements Runnable { ); } } + } // ------------------------------------------------------------------------ @@ -640,6 +785,10 @@ public class TApplication implements Runnable { public void run() { // System.err.println("*** TApplication.run() begins ***"); + // Start the screen updater thread + screenHandler = new ScreenHandler(this); + (new Thread(screenHandler)).start(); + // Start the main consumer thread primaryEventHandler = new WidgetEventHandler(this, true); (new Thread(primaryEventHandler)).start(); @@ -716,13 +865,15 @@ public class TApplication implements Runnable { } } - // Shutdown the user I/O thread(s) - backend.shutdown(); - // Close all the windows. This gives them an opportunity to release // resources. closeAllWindows(); + // Close the desktop. + if (desktop != null) { + setDesktop(null); + } + // Give the overarching application an opportunity to release // resources. onExit(); @@ -771,7 +922,7 @@ public class TApplication implements Runnable { return true; } - if (command.equals(cmMenu)) { + if (command.equals(cmMenu) && (hideMenuBar == false)) { if (!modalWindowActive() && (activeMenu == null)) { if (menus.size() > 0) { menus.get(0).setActive(true); @@ -834,7 +985,7 @@ public class TApplication implements Runnable { openImage(); return true; } - if (menu.getId() == TMenu.MID_CHANGE_FONT) { + if (menu.getId() == TMenu.MID_SCREEN_OPTIONS) { new TFontChooserWindow(this); return true; } @@ -856,6 +1007,7 @@ public class TApplication implements Runnable { && !keypress.getKey().isCtrl() && (activeMenu == null) && !modalWindowActive() + && (hideMenuBar == false) ) { assert (subMenus.size() == 0); @@ -891,6 +1043,9 @@ public class TApplication implements Runnable { drawAll(); } + // Wake up the screen repainter + wakeScreenHandler(); + if (debugThreads) { System.err.printf(System.currentTimeMillis() + " " + Thread.currentThread() + " finishEventProcessing() END\n"); @@ -932,17 +1087,26 @@ public class TApplication implements Runnable { if (event instanceof TResizeEvent) { TResizeEvent resize = (TResizeEvent) event; synchronized (getScreen()) { - getScreen().setDimensions(resize.getWidth(), - resize.getHeight()); + if ((System.currentTimeMillis() - screenResizeTime >= 15) + || (resize.getWidth() < getScreen().getWidth()) + || (resize.getHeight() < getScreen().getHeight()) + ) { + getScreen().setDimensions(resize.getWidth(), + resize.getHeight()); + screenResizeTime = System.currentTimeMillis(); + } desktopBottom = getScreen().getHeight() - 1; + if (hideStatusBar) { + desktopBottom++; + } mouseX = 0; mouseY = 0; oldMouseX = 0; oldMouseY = 0; } if (desktop != null) { - desktop.setDimensions(0, 0, resize.getWidth(), - resize.getHeight() - 1); + desktop.setDimensions(0, desktopTop, resize.getWidth(), + (desktopBottom - desktopTop)); desktop.onResize(resize); } @@ -982,8 +1146,16 @@ public class TApplication implements Runnable { // Special application-wide events ----------------------------------- + if (event instanceof TKeypressEvent) { + if (hideMouseWhenTyping) { + typingHidMouse = true; + } + } + // Peek at the mouse position if (event instanceof TMouseEvent) { + typingHidMouse = false; + TMouseEvent mouse = (TMouseEvent) event; if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { oldMouseX = mouseX; @@ -1129,6 +1301,8 @@ public class TApplication implements Runnable { } } else if (event instanceof TKeypressEvent) { dispatchToDesktop = false; + } else if (event instanceof TMenuEvent) { + dispatchToDesktop = false; } if (debugEvents) { @@ -1169,6 +1343,8 @@ public class TApplication implements Runnable { // Peek at the mouse position if (event instanceof TMouseEvent) { + typingHidMouse = false; + TMouseEvent mouse = (TMouseEvent) event; if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { oldMouseX = mouseX; @@ -1328,6 +1504,19 @@ public class TApplication implements Runnable { } } + /** + * Wake the sleeping screen handler. + */ + private void wakeScreenHandler() { + if (!started) { + return; + } + + synchronized (screenHandler) { + screenHandler.notify(); + } + } + // ------------------------------------------------------------------------ // TApplication ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -1427,6 +1616,8 @@ public class TApplication implements Runnable { */ public final void setDesktop(final TDesktop desktop) { if (this.desktop != null) { + this.desktop.onPreClose(); + this.desktop.onUnfocus(); this.desktop.onClose(); } this.desktop = desktop; @@ -1488,7 +1679,7 @@ public class TApplication implements Runnable { String version = getClass().getPackage().getImplementationVersion(); if (version == null) { // This is Java 9+, use a hardcoded string here. - version = "0.3.1"; + version = "0.3.2"; } messageBox(i18n.getString("aboutDialogTitle"), MessageFormat.format(i18n.getString("aboutDialogText"), version), @@ -1539,6 +1730,19 @@ public class TApplication implements Runnable { * @param y row position */ private void invertCell(final int x, final int y) { + invertCell(x, y, false); + } + + /** + * Invert the cell color at a position. This is used to track the mouse. + * + * @param x column position + * @param y row position + * @param onlyThisCell if true, only invert this cell + */ + private void invertCell(final int x, final int y, + final boolean onlyThisCell) { + if (debugThreads) { System.err.printf("%d %s invertCell() %d %d\n", System.currentTimeMillis(), Thread.currentThread(), x, y); @@ -1562,22 +1766,57 @@ public class TApplication implements Runnable { } } + // If this cell is on top of the desktop, and the desktop has + // requested a hidden mouse, bail out. + if ((desktop != null) && (activeWindow == null) && (activeMenu == null)) { + if ((desktop.hasHiddenMouse() == true) + && (x > desktop.getX()) + && (x < desktop.getX() + desktop.getWidth() - 1) + && (y > desktop.getY()) + && (y < desktop.getY() + desktop.getHeight() - 1) + ) { + return; + } + } + Cell cell = getScreen().getCharXY(x, y); if (cell.isImage()) { cell.invertImage(); + } + if (cell.getForeColorRGB() < 0) { + cell.setForeColor(cell.getForeColor().invert()); } else { - if (cell.getForeColorRGB() < 0) { - cell.setForeColor(cell.getForeColor().invert()); - } else { - cell.setForeColorRGB(cell.getForeColorRGB() ^ 0x00ffffff); + cell.setForeColorRGB(cell.getForeColorRGB() ^ 0x00ffffff); + } + if (cell.getBackColorRGB() < 0) { + cell.setBackColor(cell.getBackColor().invert()); + } else { + cell.setBackColorRGB(cell.getBackColorRGB() ^ 0x00ffffff); + } + getScreen().putCharXY(x, y, cell); + if ((onlyThisCell == true) || (cell.getWidth() == Cell.Width.SINGLE)) { + return; + } + + // This cell is one half of a fullwidth glyph. Invert the other + // half. + if (cell.getWidth() == Cell.Width.LEFT) { + if (x < getScreen().getWidth() - 1) { + Cell rightHalf = getScreen().getCharXY(x + 1, y); + if (rightHalf.getWidth() == Cell.Width.RIGHT) { + invertCell(x + 1, y, true); + return; + } } - if (cell.getBackColorRGB() < 0) { - cell.setBackColor(cell.getBackColor().invert()); - } else { - cell.setBackColorRGB(cell.getBackColorRGB() ^ 0x00ffffff); + } + if (cell.getWidth() == Cell.Width.RIGHT) { + if (x > 0) { + Cell leftHalf = getScreen().getCharXY(x - 1, y); + if (leftHalf.getWidth() == Cell.Width.LEFT) { + invertCell(x - 1, y, true); + } } } - getScreen().putCharXY(x, y, cell); } /** @@ -1616,7 +1855,7 @@ public class TApplication implements Runnable { getScreen().putCharXY(oldDrawnMouseX, oldDrawnMouseY, oldDrawnMouseCell); oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY); - if ((images.size() > 0) && (backend instanceof ECMA48Backend)) { + if (backend instanceof ECMA48Backend) { // Special case: the entire row containing the mouse has // to be re-drawn if it has any image data, AND any rows // in between. @@ -1637,14 +1876,16 @@ public class TApplication implements Runnable { } } - // Draw mouse at the new position. - invertCell(mouseX, mouseY); + if ((textMouse == true) && (typingHidMouse == false)) { + // Draw mouse at the new position. + invertCell(mouseX, mouseY); + } oldDrawnMouseX = mouseX; oldDrawnMouseY = mouseY; } - if ((images.size() > 0) || getScreen().isDirty()) { - backend.flushScreen(); + if (getScreen().isDirty()) { + screenHandler.setDirty(); } return; } @@ -1679,63 +1920,71 @@ public class TApplication implements Runnable { } } - // Draw the blank menubar line - reset the screen clipping first so - // it won't trim it out. - getScreen().resetClipping(); - getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ', - theme.getColor("tmenu")); - // Now draw the menus. - int x = 1; - for (TMenu menu: menus) { - CellAttributes menuColor; - CellAttributes menuMnemonicColor; - if (menu.isActive()) { - menuIsActive = true; - menuColor = theme.getColor("tmenu.highlighted"); - menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); - topLevel = menu; - } else { - menuColor = theme.getColor("tmenu"); - menuMnemonicColor = theme.getColor("tmenu.mnemonic"); - } - // Draw the menu title - getScreen().hLineXY(x, 0, menu.getTitle().length() + 2, ' ', - menuColor); - getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); - // Draw the highlight character - getScreen().putCharXY(x + 1 + menu.getMnemonic().getShortcutIdx(), - 0, menu.getMnemonic().getShortcut(), menuMnemonicColor); - - if (menu.isActive()) { - ((TWindow) menu).drawChildren(); - // Reset the screen clipping so we can draw the next title. - getScreen().resetClipping(); - } - x += menu.getTitle().length() + 2; - } + if (hideMenuBar == false) { - for (TMenu menu: subMenus) { - // Reset the screen clipping so we can draw the next sub-menu. + // Draw the blank menubar line - reset the screen clipping first + // so it won't trim it out. getScreen().resetClipping(); - ((TWindow) menu).drawChildren(); + getScreen().hLineXY(0, 0, getScreen().getWidth(), ' ', + theme.getColor("tmenu")); + // Now draw the menus. + int x = 1; + for (TMenu menu: menus) { + CellAttributes menuColor; + CellAttributes menuMnemonicColor; + if (menu.isActive()) { + menuIsActive = true; + menuColor = theme.getColor("tmenu.highlighted"); + menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); + topLevel = menu; + } else { + menuColor = theme.getColor("tmenu"); + menuMnemonicColor = theme.getColor("tmenu.mnemonic"); + } + // Draw the menu title + getScreen().hLineXY(x, 0, + StringUtils.width(menu.getTitle()) + 2, ' ', menuColor); + getScreen().putStringXY(x + 1, 0, menu.getTitle(), menuColor); + // Draw the highlight character + getScreen().putCharXY(x + 1 + + menu.getMnemonic().getScreenShortcutIdx(), + 0, menu.getMnemonic().getShortcut(), menuMnemonicColor); + + if (menu.isActive()) { + ((TWindow) menu).drawChildren(); + // Reset the screen clipping so we can draw the next + // title. + getScreen().resetClipping(); + } + x += StringUtils.width(menu.getTitle()) + 2; + } + + for (TMenu menu: subMenus) { + // Reset the screen clipping so we can draw the next + // sub-menu. + getScreen().resetClipping(); + ((TWindow) menu).drawChildren(); + } } getScreen().resetClipping(); - // Draw the status bar of the top-level window - TStatusBar statusBar = null; - if (topLevel != null) { - statusBar = topLevel.getStatusBar(); - } - if (statusBar != null) { - getScreen().resetClipping(); - statusBar.setWidth(getScreen().getWidth()); - statusBar.setY(getScreen().getHeight() - topLevel.getY()); - statusBar.draw(); - } else { - CellAttributes barColor = new CellAttributes(); - barColor.setTo(getTheme().getColor("tstatusbar.text")); - getScreen().hLineXY(0, desktopBottom, getScreen().getWidth(), ' ', - barColor); + if (hideStatusBar == false) { + // Draw the status bar of the top-level window + TStatusBar statusBar = null; + if (topLevel != null) { + statusBar = topLevel.getStatusBar(); + } + if (statusBar != null) { + getScreen().resetClipping(); + statusBar.setWidth(getScreen().getWidth()); + statusBar.setY(getScreen().getHeight() - topLevel.getY()); + statusBar.draw(); + } else { + CellAttributes barColor = new CellAttributes(); + barColor.setTo(getTheme().getColor("tstatusbar.text")); + getScreen().hLineXY(0, desktopBottom, getScreen().getWidth(), + ' ', barColor); + } } // Draw the mouse pointer @@ -1745,7 +1994,7 @@ public class TApplication implements Runnable { oldDrawnMouseX, oldDrawnMouseY); } oldDrawnMouseCell = getScreen().getCharXY(mouseX, mouseY); - if ((images.size() > 0) && (backend instanceof ECMA48Backend)) { + if (backend instanceof ECMA48Backend) { // Special case: the entire row containing the mouse has to be // re-drawn if it has any image data, AND any rows in between. if (oldDrawnMouseY != mouseY) { @@ -1764,18 +2013,37 @@ public class TApplication implements Runnable { getScreen().unsetImageRow(mouseY); } } - invertCell(mouseX, mouseY); + if ((textMouse == true) && (typingHidMouse == false)) { + invertCell(mouseX, mouseY); + } oldDrawnMouseX = mouseX; oldDrawnMouseY = mouseY; // Place the cursor if it is visible if (!menuIsActive) { + + int visibleWindowCount = 0; + for (TWindow window: sorted) { + if (window.isShown()) { + visibleWindowCount++; + } + } + if (visibleWindowCount == 0) { + // No windows are visible, only the desktop. Allow it to + // have the cursor. + if (desktop != null) { + sorted.add(desktop); + } + } + TWidget activeWidget = null; if (sorted.size() > 0) { activeWidget = sorted.get(sorted.size() - 1).getActiveChild(); + int cursorClipTop = desktopTop; + int cursorClipBottom = desktopBottom; if (activeWidget.isCursorVisible()) { - if ((activeWidget.getCursorAbsoluteY() < desktopBottom) - && (activeWidget.getCursorAbsoluteY() > desktopTop) + if ((activeWidget.getCursorAbsoluteY() <= cursorClipBottom) + && (activeWidget.getCursorAbsoluteY() >= cursorClipTop) ) { getScreen().putCursor(true, activeWidget.getCursorAbsoluteX(), @@ -1795,15 +2063,9 @@ public class TApplication implements Runnable { getScreen().hideCursor(); } - // Flush the screen contents - if ((images.size() > 0) || getScreen().isDirty()) { - if (debugThreads) { - System.err.printf("%d %s backend.flushScreen()\n", - System.currentTimeMillis(), Thread.currentThread()); - } - backend.flushScreen(); + if (getScreen().isDirty()) { + screenHandler.setDirty(); } - repaint = false; } @@ -1940,9 +2202,6 @@ public class TApplication implements Runnable { assert (!window.isActive()); if (activeWindow != null) { - // TODO: see if this assertion is really necessary. - // assert (activeWindow.getZ() == 0); - activeWindow.setActive(false); // Increment every window Z that is on top of window @@ -2243,6 +2502,7 @@ public class TApplication implements Runnable { if (((window.flags & TWindow.CENTERED) == 0) && ((window.flags & TWindow.ABSOLUTEXY) == 0) && (smartWindowPlacement == true) + && (!(window instanceof TDesktop)) ) { doSmartPlacement(window); @@ -2528,46 +2788,6 @@ public class TApplication implements Runnable { window.setY(windowY); } - // ------------------------------------------------------------------------ - // TImage management ------------------------------------------------------ - // ------------------------------------------------------------------------ - - /** - * Add an image to the list. Note package private access. - * - * @param image the image to add - * @throws IllegalArgumentException if the image is already used in - * another TApplication - */ - final void addImage(final TImage image) { - if ((image.getApplication() != null) - && (image.getApplication() != this) - ) { - throw new IllegalArgumentException("Image " + image + - " is already " + "part of application " + - image.getApplication()); - } - images.add(image); - } - - /** - * Remove an image from the list. Note package private access. - * - * @param image the image to remove - * @throws IllegalArgumentException if the image is already used in - * another TApplication - */ - final void removeImage(final TImage image) { - if ((image.getApplication() != null) - && (image.getApplication() != this) - ) { - throw new IllegalArgumentException("Image " + image + - " is already " + "part of application " + - image.getApplication()); - } - images.remove(image); - } - // ------------------------------------------------------------------------ // TMenu management ------------------------------------------------------- // ------------------------------------------------------------------------ @@ -2621,6 +2841,7 @@ public class TApplication implements Runnable { && (!modalWindowActive()) && (!overrideMenuWindowActive()) && (mouse.getAbsoluteY() == 0) + && (hideMenuBar == false) ) { for (TMenu menu: subMenus) { @@ -2632,7 +2853,7 @@ public class TApplication implements Runnable { for (TMenu menu: menus) { if ((mouse.getAbsoluteX() >= menu.getTitleX()) && (mouse.getAbsoluteX() < menu.getTitleX() - + menu.getTitle().length() + 2) + + StringUtils.width(menu.getTitle()) + 2) ) { menu.setActive(true); activeMenu = menu; @@ -2648,6 +2869,7 @@ public class TApplication implements Runnable { && (mouse.isMouse1()) && (activeMenu != null) && (mouse.getAbsoluteY() == 0) + && (hideMenuBar == false) ) { TMenu oldMenu = activeMenu; @@ -2660,7 +2882,7 @@ public class TApplication implements Runnable { for (TMenu menu: menus) { if ((mouse.getAbsoluteX() >= menu.getTitleX()) && (mouse.getAbsoluteX() < menu.getTitleX() - + menu.getTitle().length() + 2) + + StringUtils.width(menu.getTitle()) + 2) ) { menu.setActive(true); activeMenu = menu; @@ -2816,6 +3038,7 @@ public class TApplication implements Runnable { */ public final void switchMenu(final boolean forward) { assert (activeMenu != null); + assert (hideMenuBar == false); for (TMenu menu: subMenus) { menu.setActive(false); @@ -2922,6 +3145,21 @@ public class TApplication implements Runnable { } } + /** + * Get the menu item associated with this ID. + * + * @param id the menu item ID + * @return the menu item, or null if not found + */ + public final TMenuItem getMenuItem(final int id) { + for (TMenuItem item: menuItems) { + if (item.getId() == id) { + return item; + } + } + return null; + } + /** * Recompute menu x positions based on their title length. */ @@ -2930,7 +3168,7 @@ public class TApplication implements Runnable { for (TMenu menu: menus) { menu.setX(x); menu.setTitleX(x); - x += menu.getTitle().length() + 2; + x += StringUtils.width(menu.getTitle()) + 2; // Don't let the menu window exceed the screen width int rightEdge = menu.getX() + menu.getWidth(); @@ -3010,7 +3248,7 @@ public class TApplication implements Runnable { TMenu toolMenu = addMenu(i18n.getString("toolMenuTitle")); toolMenu.addDefaultItem(TMenu.MID_REPAINT); toolMenu.addDefaultItem(TMenu.MID_VIEW_IMAGE); - toolMenu.addDefaultItem(TMenu.MID_CHANGE_FONT); + toolMenu.addDefaultItem(TMenu.MID_SCREEN_OPTIONS); TStatusBar toolStatusBar = toolMenu.newStatusBar(i18n. getString("toolMenuStatus")); toolStatusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); @@ -3024,9 +3262,8 @@ public class TApplication implements Runnable { */ public final TMenu addFileMenu() { TMenu fileMenu = addMenu(i18n.getString("fileMenuTitle")); - fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE); - fileMenu.addSeparator(); fileMenu.addDefaultItem(TMenu.MID_SHELL); + fileMenu.addSeparator(); fileMenu.addDefaultItem(TMenu.MID_EXIT); TStatusBar statusBar = fileMenu.newStatusBar(i18n. getString("fileMenuStatus")); @@ -3094,6 +3331,64 @@ public class TApplication implements Runnable { return helpMenu; } + /** + * Convenience function to add a default "Table" menu. + * + * @return the new menu + */ + public final TMenu addTableMenu() { + TMenu tableMenu = addMenu(i18n.getString("tableMenuTitle")); + tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_COLUMN, false); + tableMenu.addDefaultItem(TMenu.MID_TABLE_RENAME_ROW, false); + tableMenu.addSeparator(); + + TSubMenu viewMenu = tableMenu.addSubMenu(i18n. + getString("tableSubMenuView")); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_ROW_LABELS, false); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS, false); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW, false); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN, false); + + TSubMenu borderMenu = tableMenu.addSubMenu(i18n. + getString("tableSubMenuBorders")); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_NONE, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_ALL, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_NONE, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_CELL_ALL, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_RIGHT, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_LEFT, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_TOP, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_BOTTOM, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM, false); + borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_THICK_BOTTOM, false); + TSubMenu deleteMenu = tableMenu.addSubMenu(i18n. + getString("tableSubMenuDelete")); + deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_LEFT, false); + deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_UP, false); + deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_ROW, false); + deleteMenu.addDefaultItem(TMenu.MID_TABLE_DELETE_COLUMN, false); + TSubMenu insertMenu = tableMenu.addSubMenu(i18n. + getString("tableSubMenuInsert")); + insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_LEFT, false); + insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_RIGHT, false); + insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_ABOVE, false); + insertMenu.addDefaultItem(TMenu.MID_TABLE_INSERT_BELOW, false); + TSubMenu columnMenu = tableMenu.addSubMenu(i18n. + getString("tableSubMenuColumn")); + columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_NARROW, false); + columnMenu.addDefaultItem(TMenu.MID_TABLE_COLUMN_WIDEN, false); + TSubMenu fileMenu = tableMenu.addSubMenu(i18n. + getString("tableSubMenuFile")); + fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_OPEN_CSV, false); + fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_CSV, false); + fileMenu.addDefaultItem(TMenu.MID_TABLE_FILE_SAVE_TEXT, false); + + TStatusBar statusBar = tableMenu.newStatusBar(i18n. + getString("tableMenuStatus")); + statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); + return tableMenu; + } + // ------------------------------------------------------------------------ // TTimer management ------------------------------------------------------ // ------------------------------------------------------------------------