X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTApplication.java;h=8a338076d290fbaba784b7857864acfb6c4ccca4;hb=bff0df27562e1cca8e6be47e7ace9bd5bb1adbfa;hp=9d27c10f5420052103cee046baae697ca8c2bd6e;hpb=1bc08f18257ad47aea41f2c62e7da5d05eb3cc23;p=fanfix.git diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 9d27c10..8a33807 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -47,6 +47,7 @@ import java.util.ResourceBundle; import jexer.bits.Cell; import jexer.bits.CellAttributes; +import jexer.bits.Clipboard; import jexer.bits.ColorTheme; import jexer.bits.StringUtils; import jexer.event.TCommandEvent; @@ -148,6 +149,11 @@ public class TApplication implements Runnable { */ private Backend backend; + /** + * The clipboard for copy and paste. + */ + private Clipboard clipboard = new Clipboard(); + /** * Actual mouse coordinate X. */ @@ -158,16 +164,6 @@ public class TApplication implements Runnable { */ private int mouseY; - /** - * Old version of mouse coordinate X. - */ - private int oldMouseX; - - /** - * Old version mouse coordinate Y. - */ - private int oldMouseY; - /** * Old drawn version of mouse coordinate X. */ @@ -325,6 +321,36 @@ public class TApplication implements Runnable { */ private long screenResizeTime = 0; + /** + * If true, screen selection is a rectangle. + */ + private boolean screenSelectionRectangle = false; + + /** + * If true, the mouse is dragging a screen selection. + */ + private boolean inScreenSelection = false; + + /** + * Screen selection starting X. + */ + private int screenSelectionX0; + + /** + * Screen selection starting Y. + */ + private int screenSelectionY0; + + /** + * Screen selection ending X. + */ + private int screenSelectionX1; + + /** + * Screen selection ending Y. + */ + private int screenSelectionY1; + /** * WidgetEventHandler is the main event consumer loop. There are at most * two such threads in existence: the primary for normal case and a @@ -989,6 +1015,24 @@ public class TApplication implements Runnable { new TFontChooserWindow(this); return true; } + + if (menu.getId() == TMenu.MID_CUT) { + postMenuEvent(new TCommandEvent(cmCut)); + return true; + } + if (menu.getId() == TMenu.MID_COPY) { + postMenuEvent(new TCommandEvent(cmCopy)); + return true; + } + if (menu.getId() == TMenu.MID_PASTE) { + postMenuEvent(new TCommandEvent(cmPaste)); + return true; + } + if (menu.getId() == TMenu.MID_CLEAR) { + postMenuEvent(new TCommandEvent(cmClear)); + return true; + } + return false; } @@ -1035,6 +1079,47 @@ public class TApplication implements Runnable { Thread.currentThread() + " finishEventProcessing()\n"); } + // See if we need to enable/disable the edit menu. + EditMenuUser widget = null; + if (activeMenu == null) { + if (activeWindow != null) { + if (activeWindow.getActiveChild() instanceof EditMenuUser) { + widget = (EditMenuUser) activeWindow.getActiveChild(); + } + } else if (desktop != null) { + if (desktop.getActiveChild() instanceof EditMenuUser) { + widget = (EditMenuUser) desktop.getActiveChild(); + } + } + if (widget == null) { + disableMenuItem(TMenu.MID_CUT); + disableMenuItem(TMenu.MID_COPY); + disableMenuItem(TMenu.MID_PASTE); + disableMenuItem(TMenu.MID_CLEAR); + } else { + if (widget.isEditMenuCut()) { + enableMenuItem(TMenu.MID_CUT); + } else { + disableMenuItem(TMenu.MID_CUT); + } + if (widget.isEditMenuCopy()) { + enableMenuItem(TMenu.MID_COPY); + } else { + disableMenuItem(TMenu.MID_COPY); + } + if (widget.isEditMenuPaste()) { + enableMenuItem(TMenu.MID_PASTE); + } else { + disableMenuItem(TMenu.MID_PASTE); + } + if (widget.isEditMenuClear()) { + enableMenuItem(TMenu.MID_CLEAR); + } else { + disableMenuItem(TMenu.MID_CLEAR); + } + } + } + // Process timers and call doIdle()'s doIdle(); @@ -1101,8 +1186,6 @@ public class TApplication implements Runnable { } mouseX = 0; mouseY = 0; - oldMouseX = 0; - oldMouseY = 0; } if (desktop != null) { desktop.setDimensions(0, desktopTop, resize.getWidth(), @@ -1157,9 +1240,29 @@ public class TApplication implements Runnable { typingHidMouse = false; TMouseEvent mouse = (TMouseEvent) event; + if (mouse.isMouse1() && (mouse.isShift() || mouse.isCtrl())) { + // Screen selection. + if (inScreenSelection) { + screenSelectionX1 = mouse.getX(); + screenSelectionY1 = mouse.getY(); + } else { + inScreenSelection = true; + screenSelectionX0 = mouse.getX(); + screenSelectionY0 = mouse.getY(); + screenSelectionX1 = mouse.getX(); + screenSelectionY1 = mouse.getY(); + screenSelectionRectangle = mouse.isCtrl(); + } + } else { + if (inScreenSelection) { + getScreen().copySelection(clipboard, screenSelectionX0, + screenSelectionY0, screenSelectionX1, screenSelectionY1, + screenSelectionRectangle); + } + inScreenSelection = false; + } + if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { - oldMouseX = mouseX; - oldMouseY = mouseY; mouseX = mouse.getX(); mouseY = mouse.getY(); } else { @@ -1177,7 +1280,8 @@ public class TApplication implements Runnable { mouse.getAbsoluteX(), mouse.getAbsoluteY(), mouse.isMouse1(), mouse.isMouse2(), mouse.isMouse3(), - mouse.isMouseWheelUp(), mouse.isMouseWheelDown()); + mouse.isMouseWheelUp(), mouse.isMouseWheelDown(), + mouse.isAlt(), mouse.isCtrl(), mouse.isShift()); } else { // The first click of a potential double-click. @@ -1347,8 +1451,6 @@ public class TApplication implements Runnable { TMouseEvent mouse = (TMouseEvent) event; if ((mouseX != mouse.getX()) || (mouseY != mouse.getY())) { - oldMouseX = mouseX; - oldMouseY = mouseY; mouseX = mouse.getX(); mouseY = mouse.getY(); } else { @@ -1366,7 +1468,8 @@ public class TApplication implements Runnable { mouse.getAbsoluteX(), mouse.getAbsoluteY(), mouse.isMouse1(), mouse.isMouse2(), mouse.isMouse3(), - mouse.isMouseWheelUp(), mouse.isMouseWheelDown()); + mouse.isMouseWheelUp(), mouse.isMouseWheelDown(), + mouse.isAlt(), mouse.isCtrl(), mouse.isShift()); } else { // The first click of a potential double-click. @@ -1474,13 +1577,17 @@ public class TApplication implements Runnable { desktop.onIdle(); } - // Run any invokeLaters + // Run any invokeLaters. We make a copy, and run that, because one + // of these Runnables might add call TApplication.invokeLater(). + List invokes = new ArrayList(); synchronized (invokeLaters) { - for (Runnable invoke: invokeLaters) { - invoke.run(); - } + invokes.addAll(invokeLaters); invokeLaters.clear(); } + for (Runnable invoke: invokes) { + invoke.run(); + } + doRepaint(); } @@ -1582,6 +1689,15 @@ public class TApplication implements Runnable { return theme; } + /** + * Get the clipboard. + * + * @return the clipboard + */ + public final Clipboard getClipboard() { + return clipboard; + } + /** * Repaint the screen on the next update. */ @@ -1679,7 +1795,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.2"; + version = "1.0.0"; } messageBox(i18n.getString("aboutDialogTitle"), MessageFormat.format(i18n.getString("aboutDialogText"), version), @@ -1724,27 +1840,15 @@ public class TApplication implements Runnable { // ------------------------------------------------------------------------ /** - * Invert the cell color at a position. This is used to track the mouse. + * Draw the text mouse at position. * * @param x column position * @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) { + private void drawTextMouse(final int x, final int y) { if (debugThreads) { - System.err.printf("%d %s invertCell() %d %d\n", + System.err.printf("%d %s drawTextMouse() %d %d\n", System.currentTimeMillis(), Thread.currentThread(), x, y); if (activeWindow != null) { @@ -1779,44 +1883,7 @@ public class TApplication implements Runnable { } } - Cell cell = getScreen().getCharXY(x, y); - if (cell.isImage()) { - cell.invertImage(); - } - if (cell.getForeColorRGB() < 0) { - cell.setForeColor(cell.getForeColor().invert()); - } else { - 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.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().invertCell(x, y); } /** @@ -1876,9 +1943,15 @@ public class TApplication implements Runnable { } } + if (inScreenSelection) { + getScreen().setSelection(screenSelectionX0, + screenSelectionY0, screenSelectionX1, screenSelectionY1, + screenSelectionRectangle); + } + if ((textMouse == true) && (typingHidMouse == false)) { // Draw mouse at the new position. - invertCell(mouseX, mouseY); + drawTextMouse(mouseX, mouseY); } oldDrawnMouseX = mouseX; @@ -1972,7 +2045,9 @@ public class TApplication implements Runnable { // Draw the status bar of the top-level window TStatusBar statusBar = null; if (topLevel != null) { - statusBar = topLevel.getStatusBar(); + if (topLevel.isShown()) { + statusBar = topLevel.getStatusBar(); + } } if (statusBar != null) { getScreen().resetClipping(); @@ -2013,8 +2088,14 @@ public class TApplication implements Runnable { getScreen().unsetImageRow(mouseY); } } + + if (inScreenSelection) { + getScreen().setSelection(screenSelectionX0, screenSelectionY0, + screenSelectionX1, screenSelectionY1, screenSelectionRectangle); + } + if ((textMouse == true) && (typingHidMouse == false)) { - invertCell(mouseX, mouseY); + drawTextMouse(mouseX, mouseY); } oldDrawnMouseX = mouseX; oldDrawnMouseY = mouseY; @@ -3278,10 +3359,10 @@ public class TApplication implements Runnable { */ public final TMenu addEditMenu() { TMenu editMenu = addMenu(i18n.getString("editMenuTitle")); - editMenu.addDefaultItem(TMenu.MID_CUT); - editMenu.addDefaultItem(TMenu.MID_COPY); - editMenu.addDefaultItem(TMenu.MID_PASTE); - editMenu.addDefaultItem(TMenu.MID_CLEAR); + editMenu.addDefaultItem(TMenu.MID_CUT, false); + editMenu.addDefaultItem(TMenu.MID_COPY, false); + editMenu.addDefaultItem(TMenu.MID_PASTE, false); + editMenu.addDefaultItem(TMenu.MID_CLEAR, false); TStatusBar statusBar = editMenu.newStatusBar(i18n. getString("editMenuStatus")); statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help"));