X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbackend%2FSwingTerminal.java;h=c2b335f43380e2b1e22ae4ee7b0d5556baad2ab0;hb=aac4f7d63b892dde46508318910cc173369dd111;hp=134eceda63772cec5c776b9d0dedf323f4559bf3;hpb=bfa37f3b2ef87d39c15fad7d565c00cbabd92acf;p=fanfix.git diff --git a/src/jexer/backend/SwingTerminal.java b/src/jexer/backend/SwingTerminal.java index 134eced..c2b335f 100644 --- a/src/jexer/backend/SwingTerminal.java +++ b/src/jexer/backend/SwingTerminal.java @@ -117,7 +117,12 @@ public class SwingTerminal extends LogicalScreen /** * Use an outlined block for the cursor. */ - OUTLINE + OUTLINE, + + /** + * Use a vertical bar for the cursor. + */ + VERTICAL_BAR, } // ------------------------------------------------------------------------ @@ -648,6 +653,8 @@ public class SwingTerminal extends LogicalScreen cursorStyle = CursorStyle.OUTLINE; } else if (cursorStyleString.equals("block")) { cursorStyle = CursorStyle.BLOCK; + } else if (cursorStyleString.equals("verticalbar")) { + cursorStyle = CursorStyle.VERTICAL_BAR; } // Pull the system property for triple buffering. @@ -658,6 +665,9 @@ public class SwingTerminal extends LogicalScreen } else { SwingComponent.tripleBuffer = false; } + + // Set custom colors + setCustomSystemColors(); } // ------------------------------------------------------------------------ @@ -709,6 +719,62 @@ public class SwingTerminal extends LogicalScreen dosColors = true; } + /** + * Setup Swing colors to match those provided in system properties. + */ + private static void setCustomSystemColors() { + synchronized (SwingTerminal.class) { + MYBLACK = getCustomColor("jexer.Swing.color0", MYBLACK); + MYRED = getCustomColor("jexer.Swing.color1", MYRED); + MYGREEN = getCustomColor("jexer.Swing.color2", MYGREEN); + MYYELLOW = getCustomColor("jexer.Swing.color3", MYYELLOW); + MYBLUE = getCustomColor("jexer.Swing.color4", MYBLUE); + MYMAGENTA = getCustomColor("jexer.Swing.color5", MYMAGENTA); + MYCYAN = getCustomColor("jexer.Swing.color6", MYCYAN); + MYWHITE = getCustomColor("jexer.Swing.color7", MYWHITE); + MYBOLD_BLACK = getCustomColor("jexer.Swing.color8", MYBOLD_BLACK); + MYBOLD_RED = getCustomColor("jexer.Swing.color9", MYBOLD_RED); + MYBOLD_GREEN = getCustomColor("jexer.Swing.color10", MYBOLD_GREEN); + MYBOLD_YELLOW = getCustomColor("jexer.Swing.color11", MYBOLD_YELLOW); + MYBOLD_BLUE = getCustomColor("jexer.Swing.color12", MYBOLD_BLUE); + MYBOLD_MAGENTA = getCustomColor("jexer.Swing.color13", MYBOLD_MAGENTA); + MYBOLD_CYAN = getCustomColor("jexer.Swing.color14", MYBOLD_CYAN); + MYBOLD_WHITE = getCustomColor("jexer.Swing.color15", MYBOLD_WHITE); + } + } + + /** + * Setup one Swing color to match the RGB value provided in system + * properties. + * + * @param key the system property key + * @param defaultColor the default color to return if key is not set, or + * incorrect + * @return a color from the RGB string, or defaultColor + */ + private static Color getCustomColor(final String key, + final Color defaultColor) { + + String rgb = System.getProperty(key); + if (rgb == null) { + return defaultColor; + } + if (rgb.startsWith("#")) { + rgb = rgb.substring(1); + } + int rgbInt = 0; + try { + rgbInt = Integer.parseInt(rgb, 16); + } catch (NumberFormatException e) { + return defaultColor; + } + Color color = new Color((rgbInt & 0xFF0000) >>> 16, + (rgbInt & 0x00FF00) >>> 8, + (rgbInt & 0x0000FF)); + + return color; + } + /** * Get the number of millis to wait before switching the blink from * visible to invisible. @@ -755,13 +821,35 @@ public class SwingTerminal extends LogicalScreen * @param font the new font */ public void setFont(final Font font) { - synchronized (this) { - this.font = font; - getFontDimensions(); - swing.setFont(font); - glyphCacheBlink = new HashMap(); - glyphCache = new HashMap(); - resizeToScreen(true); + if (!SwingUtilities.isEventDispatchThread()) { + // Not in the Swing thread: force this inside the Swing thread. + try { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + synchronized (this) { + SwingTerminal.this.font = font; + getFontDimensions(); + swing.setFont(font); + glyphCacheBlink = new HashMap(); + glyphCache = new HashMap(); + resizeToScreen(true); + } + } + }); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (java.lang.reflect.InvocationTargetException e) { + e.printStackTrace(); + } + } else { + synchronized (this) { + SwingTerminal.this.font = font; + getFontDimensions(); + swing.setFont(font); + glyphCacheBlink = new HashMap(); + glyphCache = new HashMap(); + resizeToScreen(true); + } } } @@ -1180,7 +1268,7 @@ public class SwingTerminal extends LogicalScreen /* System.err.println("drawGlyph(): " + xPixel + " " + yPixel + " " + cell); - */ + */ BufferedImage image = null; if (cell.isBlink() && !cursorBlinkVisible) { @@ -1229,9 +1317,8 @@ public class SwingTerminal extends LogicalScreen || (cell.isBlink() && cursorBlinkVisible) ) { gr2.setColor(attrToForegroundColor(cellColor)); - char [] chars = new char[1]; - chars[0] = cell.getChar(); - gr2.drawChars(chars, 0, 1, gr2x + textAdjustX, + char [] chars = Character.toChars(cell.getChar()); + gr2.drawChars(chars, 0, chars.length, gr2x + textAdjustX, gr2y + textHeight - maxDescent + textAdjustY); if (cell.isUnderline()) { @@ -1303,6 +1390,9 @@ public class SwingTerminal extends LogicalScreen case OUTLINE: gr.drawRect(xPixel, yPixel, cursorWidth - 1, textHeight - 1); break; + case VERTICAL_BAR: + gr.fillRect(xPixel, yPixel, 2, textHeight); + break; } } } @@ -1349,7 +1439,7 @@ public class SwingTerminal extends LogicalScreen if (bounds != null) { // Only update what is in the bounds xCellMin = textColumn(bounds.x); - xCellMax = textColumn(bounds.x + bounds.width); + xCellMax = textColumn(bounds.x + bounds.width) + 1; if (xCellMax > width) { xCellMax = width; } @@ -1360,7 +1450,7 @@ public class SwingTerminal extends LogicalScreen xCellMin = 0; } yCellMin = textRow(bounds.y); - yCellMax = textRow(bounds.y + bounds.height); + yCellMax = textRow(bounds.y + bounds.height) + 1; if (yCellMax > height) { yCellMax = height; } @@ -1382,7 +1472,7 @@ public class SwingTerminal extends LogicalScreen /* System.err.printf("bounds %s X %d %d Y %d %d\n", bounds, xCellMin, xCellMax, yCellMin, yCellMax); - */ + */ for (int y = yCellMin; y < yCellMax; y++) { for (int x = xCellMin; x < xCellMax; x++) { @@ -1829,6 +1919,7 @@ public class SwingTerminal extends LogicalScreen break; default: if (!alt && ctrl && !shift) { + // Control character, replace ch with 'A', 'B', etc. ch = KeyEvent.getKeyText(key.getKeyCode()).charAt(0); } // Not a special key, put it together @@ -2010,6 +2101,10 @@ public class SwingTerminal extends LogicalScreen boolean eventMouse1 = false; boolean eventMouse2 = false; boolean eventMouse3 = false; + boolean eventAlt = false; + boolean eventCtrl = false; + boolean eventShift = false; + if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) { eventMouse1 = true; } @@ -2019,6 +2114,16 @@ public class SwingTerminal extends LogicalScreen if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) { eventMouse3 = true; } + if ((modifiers & MouseEvent.ALT_DOWN_MASK) != 0) { + eventAlt = true; + } + if ((modifiers & MouseEvent.CTRL_DOWN_MASK) != 0) { + eventCtrl = true; + } + if ((modifiers & MouseEvent.SHIFT_DOWN_MASK) != 0) { + eventShift = true; + } + mouse1 = eventMouse1; mouse2 = eventMouse2; mouse3 = eventMouse3; @@ -2026,7 +2131,8 @@ public class SwingTerminal extends LogicalScreen int y = textRow(mouse.getY()); TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_MOTION, - x, y, x, y, mouse1, mouse2, mouse3, false, false); + x, y, x, y, mouse1, mouse2, mouse3, false, false, + eventAlt, eventCtrl, eventShift); synchronized (eventQueue) { eventQueue.add(mouseEvent); @@ -2054,8 +2160,24 @@ public class SwingTerminal extends LogicalScreen oldMouseX = x; oldMouseY = y; + boolean eventAlt = false; + boolean eventCtrl = false; + boolean eventShift = false; + + int modifiers = mouse.getModifiersEx(); + if ((modifiers & MouseEvent.ALT_DOWN_MASK) != 0) { + eventAlt = true; + } + if ((modifiers & MouseEvent.CTRL_DOWN_MASK) != 0) { + eventCtrl = true; + } + if ((modifiers & MouseEvent.SHIFT_DOWN_MASK) != 0) { + eventShift = true; + } + TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_MOTION, - x, y, x, y, mouse1, mouse2, mouse3, false, false); + x, y, x, y, mouse1, mouse2, mouse3, false, false, + eventAlt, eventCtrl, eventShift); synchronized (eventQueue) { eventQueue.add(mouseEvent); @@ -2087,7 +2209,7 @@ public class SwingTerminal extends LogicalScreen * @param mouse mouse event received */ public void mouseEntered(final MouseEvent mouse) { - // Ignore + swing.requestFocusInWindow(); } /** @@ -2109,6 +2231,10 @@ public class SwingTerminal extends LogicalScreen boolean eventMouse1 = false; boolean eventMouse2 = false; boolean eventMouse3 = false; + boolean eventAlt = false; + boolean eventCtrl = false; + boolean eventShift = false; + if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) { eventMouse1 = true; } @@ -2118,6 +2244,16 @@ public class SwingTerminal extends LogicalScreen if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) { eventMouse3 = true; } + if ((modifiers & MouseEvent.ALT_DOWN_MASK) != 0) { + eventAlt = true; + } + if ((modifiers & MouseEvent.CTRL_DOWN_MASK) != 0) { + eventCtrl = true; + } + if ((modifiers & MouseEvent.SHIFT_DOWN_MASK) != 0) { + eventShift = true; + } + mouse1 = eventMouse1; mouse2 = eventMouse2; mouse3 = eventMouse3; @@ -2125,7 +2261,8 @@ public class SwingTerminal extends LogicalScreen int y = textRow(mouse.getY()); TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_DOWN, - x, y, x, y, mouse1, mouse2, mouse3, false, false); + x, y, x, y, mouse1, mouse2, mouse3, false, false, + eventAlt, eventCtrl, eventShift); synchronized (eventQueue) { eventQueue.add(mouseEvent); @@ -2148,6 +2285,10 @@ public class SwingTerminal extends LogicalScreen boolean eventMouse1 = false; boolean eventMouse2 = false; boolean eventMouse3 = false; + boolean eventAlt = false; + boolean eventCtrl = false; + boolean eventShift = false; + if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) { eventMouse1 = true; } @@ -2157,6 +2298,16 @@ public class SwingTerminal extends LogicalScreen if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) { eventMouse3 = true; } + if ((modifiers & MouseEvent.ALT_DOWN_MASK) != 0) { + eventAlt = true; + } + if ((modifiers & MouseEvent.CTRL_DOWN_MASK) != 0) { + eventCtrl = true; + } + if ((modifiers & MouseEvent.SHIFT_DOWN_MASK) != 0) { + eventShift = true; + } + if (mouse1) { mouse1 = false; eventMouse1 = true; @@ -2173,7 +2324,8 @@ public class SwingTerminal extends LogicalScreen int y = textRow(mouse.getY()); TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_UP, - x, y, x, y, eventMouse1, eventMouse2, eventMouse3, false, false); + x, y, x, y, eventMouse1, eventMouse2, eventMouse3, false, false, + eventAlt, eventCtrl, eventShift); synchronized (eventQueue) { eventQueue.add(mouseEvent); @@ -2202,6 +2354,10 @@ public class SwingTerminal extends LogicalScreen boolean eventMouse3 = false; boolean mouseWheelUp = false; boolean mouseWheelDown = false; + boolean eventAlt = false; + boolean eventCtrl = false; + boolean eventShift = false; + if ((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) { eventMouse1 = true; } @@ -2211,6 +2367,16 @@ public class SwingTerminal extends LogicalScreen if ((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) { eventMouse3 = true; } + if ((modifiers & MouseEvent.ALT_DOWN_MASK) != 0) { + eventAlt = true; + } + if ((modifiers & MouseEvent.CTRL_DOWN_MASK) != 0) { + eventCtrl = true; + } + if ((modifiers & MouseEvent.SHIFT_DOWN_MASK) != 0) { + eventShift = true; + } + mouse1 = eventMouse1; mouse2 = eventMouse2; mouse3 = eventMouse3; @@ -2224,7 +2390,8 @@ public class SwingTerminal extends LogicalScreen } TMouseEvent mouseEvent = new TMouseEvent(TMouseEvent.Type.MOUSE_DOWN, - x, y, x, y, mouse1, mouse2, mouse3, mouseWheelUp, mouseWheelDown); + x, y, x, y, mouse1, mouse2, mouse3, mouseWheelUp, mouseWheelDown, + eventAlt, eventCtrl, eventShift); synchronized (eventQueue) { eventQueue.add(mouseEvent);