package NET.worlds.console; import NET.worlds.core.IniFile; import java.awt.Canvas; import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Scrollbar; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; class StyledTextCanvas extends Canvas { private static final long serialVersionUID = -2680423246934436331L; private MouseEvent delayedMouseEvent = null; protected int mouseX = -1; protected int mouseY = -1; protected int startX = -1; protected int startY = -1; protected int endX = -1; protected int endY = -1; protected long mouseWhen = 0L; protected boolean mouseActive = false; protected boolean mouseDoubleClick = false; protected static long doubleClickSpeed = IniFile.override().getIniInt("DoubleClickSpeed", 300); public StyledTextCanvas() { this.enableEvents(61L); this.setEnabled(true); } public void sendDelayedMouseEvent(MouseEvent e) { synchronized (this.getParent()) { this.delayedMouseEvent = e; } } @Override public void setBounds(int x, int y, int w, int h) { if (this.getParent() != null && w >= 0 && h >= 0) { synchronized (this.getParent()) { if (this.getParent() instanceof GammaTextArea) { GammaTextArea gta = (GammaTextArea)this.getParent(); gta.setWidth(w); gta.setHeight(h); gta.rewrap(); } super.setBounds(x, y, w, h); } } } @Override protected void processMouseMotionEvent(MouseEvent e) { if (e.getID() == 506 && this.mouseActive) { this.processMouseEvent(e); } super.processMouseMotionEvent(e); } @Override protected void processMouseEvent(MouseEvent e) { synchronized (this.getParent()) { if (e.getID() == 500) { if (this.mouseWhen > 0L && e.getWhen() - this.mouseWhen <= doubleClickSpeed) { this.mouseDoubleClick = true; this.mouseWhen = 0L; this.mouseX = this.startX = this.endX = e.getX(); this.mouseY = this.startY = this.endY = e.getY(); ((GammaTextArea)this.getParent()).selectionConversion = true; this.getParent().repaint(); } else { this.mouseDoubleClick = false; this.mouseWhen = e.getWhen(); if (e.getButton() == 1 && !this.mouseActive && (e.getModifiers() & 1) == 0) { ((GammaTextArea)this.getParent()).selectionStart = -1; ((GammaTextArea)this.getParent()).selectionEnd = -1; } } this.getParent().requestFocus(); } else if (e.getID() == 501) { if (e.getButton() == 1) { if ((e.getModifiers() & 1) == 0) { if (((GammaTextArea)this.getParent()).selectionStart >= 0 && ((GammaTextArea)this.getParent()).selectionEnd > ((GammaTextArea)this.getParent()).selectionStart) { ((GammaTextArea)this.getParent()).selectionStart = -1; ((GammaTextArea)this.getParent()).selectionEnd = -1; } else { this.mouseX = this.startX = this.endX = e.getX(); this.mouseY = this.startY = this.endY = e.getY(); this.mouseActive = true; ((GammaTextArea)this.getParent()).selectionConversion = true; } } else { this.mouseX = this.startX = this.endX = e.getX(); this.mouseY = this.startY = this.endY = e.getY(); this.mouseActive = true; ((GammaTextArea)this.getParent()).selectionConversion = true; } this.getParent().repaint(); } this.getParent().requestFocus(); } else if (this.mouseActive && (e.getID() == 506 || e.getID() == 507 || e.getID() == 507)) { if ((e.getModifiers() & 16) != 0) { this.mouseX = this.endX = e.getX(); this.mouseY = this.endY = e.getY(); ((GammaTextArea)this.getParent()).selectionConversion = true; this.getParent().repaint(); } this.getParent().requestFocus(); } else if (e.getID() == 502) { if (e.getButton() == 1 && this.mouseActive) { this.mouseX = this.endX = e.getX(); this.mouseY = this.endY = e.getY(); this.mouseActive = false; ((GammaTextArea)this.getParent()).selectionConversion = false; this.getParent().repaint(); } this.getParent().requestFocus(); } ((GammaTextArea)this.getParent()).processMouseEvent(e); super.processMouseEvent(e); } } @Override public void update(Graphics g) { this.paint(g); } @Override protected void processKeyEvent(KeyEvent e) { synchronized (this.getParent()) { if (e.getID() == 401) { Scrollbar s = null; if (this.getParent() instanceof GammaTextArea) { GammaTextArea gta = (GammaTextArea)this.getParent(); s = gta.getVertScrollbar(); } if (e.getKeyCode() == 33) { if (s != null && s.isEnabled()) { s.dispatchEvent(e); } } else if (e.getKeyCode() == 34 && s != null && s.isEnabled()) { s.dispatchEvent(e); } } super.processKeyEvent(e); } } @Override public void paint(Graphics g) { synchronized (this.getParent()) { if (this.getParent().isEnabled()) { GammaTextArea gta = (GammaTextArea)this.getParent(); Rectangle r = this.getBounds(); if (r.width <= 0 || r.height <= 0) { return; } Image offImage = this.createImage(r.width, r.height); Graphics offGraphic = offImage.getGraphics(); offGraphic.setColor(GammaTextArea.getBackgroundColor()); offGraphic.fillRect(r.x, r.y, r.width, r.height); offGraphic.setColor(Color.black); if (gta.getHasFocus()) { offGraphic.setColor(Color.blue); offGraphic.drawRect(r.x, r.y, r.width - 1, r.height - 1); offGraphic.drawRect(r.x + 1, r.y + 1, r.width - 2, r.height - 2); offGraphic.setColor(Color.black); } offGraphic.setFont(gta.getFont()); FontMetrics fm = offGraphic.getFontMetrics(gta.getFont()); gta._curpos = 0; if (gta.getNumLines() <= gta.getCanvasLines()) { for (int i = 0; i < gta.getNumLines(); i++) { gta.drawLine(offGraphic, i, (i + 1) * fm.getHeight() - fm.getDescent()); } } else { int i = gta.getScrollLine(); for (int j = 1; i < gta.getNumLines(); j++) { gta.drawLine(offGraphic, i, j * fm.getHeight() - fm.getDescent()); i++; } } g.drawImage(offImage, 0, 0, this); if (this.delayedMouseEvent != null) { this.processMouseEvent(this.delayedMouseEvent); this.delayedMouseEvent = null; } } } } }