// Copyright 2000-2005 the Contributors, as shown in the revision logs. // Licensed under the GNU General Public License version 2 ("the License"). // You may not use this file except in compliance with the License. package org.ibex.plat; import org.ibex.util.*; import java.io.*; import org.ibex.js.*; import java.awt.*; import java.awt.datatransfer.*; import java.awt.image.*; import java.awt.event.*; import org.ibex.graphics.*; import org.ibex.core.*; import org.ibex.net.*; /** Platform subclass for all VM's providing AWT 1.1 functionality */ public class AWT extends JVM { protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; } protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new AWTPixelBuffer(w, h); } protected Picture _createPicture(JS r) { return new AWTPicture(r); } protected int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; } protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; } protected Surface _createSurface(Box b, boolean framed) { return new AWTSurface(b, framed); } protected void postInit() { if (Log.on) Log.diag(Platform.class, " color depth = " + Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp"); } protected void _criticalAbort(String message) { if (Log.on) Log.info(this, message); final Dialog d = new Dialog(new Frame(), "Ibex Cannot Continue"); d.setLayout(new BorderLayout()); TextArea ta = new TextArea("Ibex cannot continue because:\n\n" + message, 10, 80); ta.setEditable(false); d.add(ta, "Center"); Button b = new Button("OK"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { d.dispose(); } }); d.add(b, "South"); d.setModal(true); d.pack(); d.show(); new Semaphore().block(); } protected String _getClipBoard() { Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); if (cb == null) return null; Transferable clipdata = cb.getContents(null); try { return (String)clipdata.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex) { return null; } } protected void _setClipBoard(String s) { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); if (clipboard == null) return; StringSelection clipString = new StringSelection(s); clipboard.setContents(clipString, clipString); } /** some platforms (cough, cough, NetscapeVM) have totally broken modifier masks; they will need to override this */ protected static int modifiersToButtonNumber(int modifiers) { if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1; if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) { // ugh, MacOSX reports the right mouse button as BUTTON2_MASK... if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 2; return 3; } if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { // ugh, MacOSX reports the right mouse button as BUTTON2_MASK... if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 3; return 2; } return 0; } static class FileDialogHelper extends FileDialog implements WindowListener, ComponentListener { Semaphore s; public FileDialogHelper(String suggestedFileName, Semaphore s, boolean write) { super(new Frame(), write ? "Save" : "Open", write ? FileDialog.SAVE : FileDialog.LOAD); this.s = s; addWindowListener(this); addComponentListener(this); if (suggestedFileName.indexOf(File.separatorChar) == -1) { setFile(suggestedFileName); } else { setDirectory(suggestedFileName.substring(0, suggestedFileName.lastIndexOf(File.separatorChar))); setFile(suggestedFileName.substring(suggestedFileName.lastIndexOf(File.separatorChar) + 1)); } show(); } public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { s.release(); } public void windowClosing(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void componentHidden(ComponentEvent e) { s.release(); } public void componentMoved(ComponentEvent e) { } public void componentResized(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } }; protected String _fileDialog(String suggestedFileName, boolean write) { final Semaphore s = new Semaphore(); FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write); s.block(); return fd.getDirectory() == null ? null : (fd.getDirectory() + File.separatorChar + fd.getFile()); } // Inner Classes ///////////////////////////////////////////////////////////////////////////////////// protected org.ibex.graphics.Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new AWTGlyph(f, c); } protected static class AWTGlyph extends org.ibex.graphics.Font.Glyph { Image i = null; Image i2 = null; private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); // this doesn't work on Win32 because the JVM is broken /* static final ColorModel cmodel = new ColorModel(8) { public int getRed(int p) { return 0; } public int getGreen(int p) { return 0; } public int getBlue(int p) { return 0; } public int getAlpha(int p) { return p & 0xFF; } }; */ public AWTGlyph(org.ibex.graphics.Font f, char c) { super(f, c); } Image getImage() { if (i == null && isLoaded) { int[] data2 = new int[data.length]; for(int i=0; i> 16, (argb & 0x0000ff00) >> 8, argb & 0x000000ff/*, (argb & 0xff000000) >> 24*/)); g.fillPolygon(xpoints, ypoints, 3); } public void drawPicture(Picture p, Affine a, Mesh h) { throw new Error("drawPicture() not implemented"); } protected Image i = null; protected Graphics g = null; protected AWTSurface surface = null; /** JDK1.1 platforms require that a component be associated with each off-screen buffer */ static Component component = null; public AWTPixelBuffer(Image i) { this.i = i; } public AWTPixelBuffer(AWTSurface s) { this.surface = s; } public AWTPixelBuffer(int w, int h) { synchronized(AWTPixelBuffer.class) { if (component == null) { component = new Frame(); component.setVisible(false); component.addNotify(); } } i = component.createImage(w, h); } Graphics getGraphics() { if (surface != null) return surface.getGraphics(); if (g != null) return g; if (i != null) { g = i.getGraphics(); return g; } return null; } public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) { ((AWTPicture)source).init(); Graphics g = getGraphics(); g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1); g.drawImage(((AWTPicture)source).i, dx, dy, null); if (i != null) g.setClip(0, 0, i.getWidth(null), i.getHeight(null)); } public void drawLine(int x1, int y1, int x2, int y2, int rgb) { Graphics g = getGraphics(); ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff)); g.drawLine(x1, y1, x2, y2); } //public void stroke(org.ibex.graphics.Mesh p, int color) { /*p.stroke(this, color);*/ } //public void fill(org.ibex.graphics.Mesh p, org.ibex.graphics.Paint paint) { /*p.fill(this, paint);*/ } private static int[] xa = new int[4]; private static int[] ya = new int[4]; public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) { Graphics g = getGraphics(); ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF))); xa[0]=x1; xa[1]=x2; xa[2]=x4; xa[3]=x3; ya[0]=y1; ya[1]=y1; ya[2]=y2; ya[3]=y2; g.fillPolygon(xa, ya, 4); } // this doens't seem to work on Windows public void drawGlyph(org.ibex.graphics.Font.Glyph source, Affine a, Mesh h, int argb, int bg) { //throw new Error("drawGlyph() not implemented"); /* Image i = ((AWTGlyph)source).getImage(); if (((AWTGlyph)source).i2 == null) ((AWTGlyph)source).i2 = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB); Image i2 = ((AWTGlyph)source).i2; Graphics g2 = i2.getGraphics(); g2.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF))); g2.fillRect(0, 0, i2.getWidth(null), i2.getHeight(null)); g2.drawImage(i, 0, 0, null); Graphics g = getGraphics(); g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1); g.drawImage(i2, dx, dy, i2.getWidth(null), i2.getHeight(null), null); g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF))); g.fillRect(cx1, cy1, dx - cx1, cy2 - cy1); g.fillRect(cx1, cy1, cx2 - cx1, dy - cy1); g.fillRect(dx+i2.getWidth(null), cy1, cx2 - (dx+i2.getWidth(null)), cy2 - cy1); g.fillRect(cx1, dy+i2.getHeight(null), cx2 - cx1, cy2 - (dy+i2.getHeight(null))); g.setClip(0, 0, 1000, 1000); */ } } protected static class AWTSurface extends Surface // extends Surface.DoubleBufferedSurface implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener { protected AWTPixelBuffer pb = null; private Graphics g = null; Frame frame = null; Window window = null; public PixelBuffer getPixelBuffer() { return pb==null?(pb=new AWTPixelBuffer(this)):pb; } public void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2) { getGraphics().drawImage(((AWTPixelBuffer)source).i, sx, sy, sx+(dx2-dx), sy+(dy2-dy), dx, dy, dx2, dy2, null); } /** our component's insets */ protected Insets insets = new Insets(0, 0, 0, 0); Graphics getGraphics() { if (g==null) g=window==null ? frame.getGraphics() : window.getGraphics(); return g; } public void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) { if (pb ==null) pb = new AWTPixelBuffer(this); pb.fillTrapezoid(x1, x2, y1, x3, x4, y2, color); } //public PixelBuffer getPixelBuffer() { return pb != null ? pb : (pb = new AWTPixelBuffer(this)); } public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); } public void toBack() { if (window != null) window.toBack(); } public void toFront() { if (window != null) window.toFront(); } public void setLocation() { /*window.setLocation(root.x, root.y); FIXME */ } public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); } public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); } public void _setSize(int width, int height) { g = null; window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); } public void setInvisible(boolean b) { window.setVisible(!b); } protected void _setMinimized(boolean b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); } protected void _setMaximized(boolean b) { if (!b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot unmaximize windows"); return; } window.setLocation(new Point(0, 0)); window.setSize(Toolkit.getDefaultToolkit().getScreenSize()); g = null; } class InnerFrame extends Frame { public InnerFrame() throws java.lang.UnsupportedOperationException { } public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); } public void update(Graphics gr) { Rectangle r = gr.getClipBounds(); super.update(gr); dirtify(r); } public void paint(Graphics gr) { // Mac OS X Jdk1.4 Bug: after a componentResized(), you must wait for the paint() before you redraw Rectangle r = gr.getClipBounds(); super.paint(gr); dirtify(r); } private void dirtify(java.awt.Rectangle r) { if (r != null) { Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height); } else { Dirty(0, 0, Math.max(getWidth() - insets.left - insets.right, root.getRootWidth()), Math.min(getHeight() - insets.top - insets.bottom, root.getRootHeight())); } // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize int newwidth = Math.max(getWidth() - insets.left - insets.right, root.getRootWidth()); int newheight = Math.max(getHeight() - insets.top - insets.bottom, root.getRootHeight()); if (newwidth != root.getRootWidth() || newheight != root.getRootHeight()) componentResized(newwidth, newheight); } } class InnerWindow extends Window { public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); } public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); } public void update(Graphics gr) { paint(gr); } public void paint(Graphics gr) { g = null; Rectangle r = gr.getClipBounds(); Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height); } } private int oldfill = 0x0; public void render() { // useful optimizatin; /* if (oldfill != root.fillcolor) { oldfill = root.fillcolor; window.setBackground((root.fillcolor & 0xFF000000) == 0 ? java.awt.Color.white : new java.awt.Color((root.fillcolor >> 16) & 0xff, (root.fillcolor >> 8) & 0xff, (root.fillcolor) & 0xff)); } */ super.render(); } Insets getInsets() { Insets ret = window.getInsets(); if (System.getProperty("os.name", "").equals("Mac OS X")) ret.bottom = -1 * ret.top; return ret; } AWTSurface(Box root, boolean framed) { super(root); try { if (framed) window = frame = new InnerFrame(); else window = new InnerWindow(); // this is here to catch HeadlessException on jdk1.4 } catch (java.lang.UnsupportedOperationException e) { if (Log.on) Log.info(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen"); if (Log.on) Log.info(this, e); } insets = getInsets(); window.addMouseListener(this); window.addKeyListener(this); window.addComponentListener(this); window.addMouseMotionListener(this); window.addWindowListener(this); // IMPORTANT: this must be called before render() to ensure // that our peer has been created makeVisible(); } protected void makeVisible() { window.setVisible(true); } public void _dispose() { window.removeMouseListener(this); // removed to work around a jdk1.3 bug /* window.removeKeyListener(this); */ window.removeComponentListener(this); window.removeMouseMotionListener(this); window.removeWindowListener(this); window.dispose(); } public void syncCursor() { if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)); else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)); else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)); else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR)); else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)); else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)); else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR)); else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)); else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } // AWT Message translation //////////////////////////////////////////////////////////////// // these functions are all executed in the AWT thread, not the // MessageQueue thread. As a result, they must be *extremely* // careful about invoking methods on instances of Box. Currently, // they should only enqueue messages, use Box.whoIs() // (unsynchronized but thought to be safe), and modify members of // Surface. public void componentHidden(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } public void windowOpened(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { Close(); } public void windowIconified(WindowEvent e) { Minimized(true); } public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.getRootWidth(), root.getRootHeight()); Minimized(false); } public void windowActivated(WindowEvent e) { Focused(true); } public void windowDeactivated(WindowEvent e) { Focused(false); } public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); } public void componentResized(ComponentEvent e) { // we have to periodically do this; I don't know why insets = getInsets(); componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom); } public void componentResized(int newwidth, int newheight) { SizeChange(newwidth, newheight); if (newwidth > root.getRootWidth()) Dirty(root.getRootWidth(), 0, newwidth-root.getRootWidth(), newheight); if (newheight > root.getRootHeight()) Dirty(0, root.getRootHeight(), newwidth, newheight-root.getRootHeight()); Refresh(); } public void keyTyped(KeyEvent k) { } public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); } public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); } public void mouseExited(MouseEvent m) { mouseMoved(m); } public void mouseEntered(MouseEvent m) { mouseMoved(m); } public void mouseDragged(MouseEvent m) { mouseMoved(m); } public void mouseMoved(MouseEvent m) { // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize int newwidth = window.getWidth() - insets.left - insets.right; int newheight = window.getHeight() - insets.top - insets.bottom; if (newwidth != root.getRootWidth() || newheight != root.getRootHeight()) componentResized(newwidth, newheight); Move(m.getX() - insets.left, m.getY() - insets.top); } public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); } public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); } public void mouseClicked(MouseEvent m) { if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers())); else Click(modifiersToButtonNumber(m.getModifiers())); } String translateKey(KeyEvent k) { switch (k.getKeyCode()) { case KeyEvent.VK_ALT: return "alt"; case KeyEvent.VK_BACK_SPACE: return "back_space"; case KeyEvent.VK_CONTROL: return "control"; case KeyEvent.VK_DELETE: return "delete"; case KeyEvent.VK_DOWN: return "down"; case KeyEvent.VK_END: return "end"; case KeyEvent.VK_ENTER: return "enter"; case KeyEvent.VK_ESCAPE: return "escape"; case KeyEvent.VK_F1: return "f1"; case KeyEvent.VK_F10: return "f10"; case KeyEvent.VK_F11: return "f11"; case KeyEvent.VK_F12: return "f12"; case KeyEvent.VK_F2: return "f2"; case KeyEvent.VK_F3: return "f3"; case KeyEvent.VK_F4: return "f4"; case KeyEvent.VK_F5: return "f5"; case KeyEvent.VK_F6: return "f6"; case KeyEvent.VK_F7: return "f7"; case KeyEvent.VK_F8: return "f8"; case KeyEvent.VK_F9: return "f9"; case KeyEvent.VK_HOME: return "home"; case KeyEvent.VK_INSERT: return "insert"; case KeyEvent.VK_LEFT: return "left"; case KeyEvent.VK_META: return "alt"; case KeyEvent.VK_PAGE_DOWN: return "page_down"; case KeyEvent.VK_PAGE_UP: return "page_up"; case KeyEvent.VK_PAUSE: return "pause"; case KeyEvent.VK_PRINTSCREEN: return "printscreen"; case KeyEvent.VK_RIGHT: return "right"; case KeyEvent.VK_SHIFT: return "shift"; case KeyEvent.VK_TAB: return "tab"; case KeyEvent.VK_UP: return "up"; default: char c = k.getKeyChar(); if (c >= 1 && c <= 26) c = (char)('a' + c - 1); return String.valueOf(c); } } } protected void _decodeJPEG(InputStream is, Picture p) { try { Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is)); MediaTracker mediatracker = new MediaTracker(new Canvas()); mediatracker.addImage(i, 1); try { mediatracker.waitForAll(); } catch (InterruptedException e) { } mediatracker.removeImage(i); final int width = i.getWidth(null); final int height = i.getHeight(null); final int[] data = new int[width * height]; PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width); pg.grabPixels(); if ((pg.getStatus() & ImageObserver.ABORT) != 0) Log.info(this, "PixelGrabber reported an error while decoding JPEG image"); p.width = width; p.height = height; p.data = data; } catch (Exception e) { Log.info(this, "Exception caught while decoding JPEG image"); Log.info(this, e); } } }