X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fplat%2FAWT.java;h=9ca0366e0567f779ebdf599e950fd45d16ee7dbe;hp=fc08b1d4998b446c07577c7550dd43046153a4d5;hb=3419f19eae26a68874ff4c9eb56e93c7d84b93a1;hpb=b857cf06b4b1fe9a5f6200728cc1e5f94903b05a diff --git a/src/org/ibex/plat/AWT.java b/src/org/ibex/plat/AWT.java index fc08b1d..9ca0366 100644 --- a/src/org/ibex/plat/AWT.java +++ b/src/org/ibex/plat/AWT.java @@ -1,4 +1,7 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +// 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.*; @@ -117,7 +120,8 @@ public class AWT extends JVM { 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 { - private Image i = null; + 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 @@ -183,15 +187,17 @@ public class AWT extends JVM { } } - protected static class AWTPixelBuffer extends PixelBuffer { + protected static class AWTPixelBuffer implements PixelBuffer { 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; - protected AWTPixelBuffer() { } + 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) { @@ -201,55 +207,69 @@ public class AWT extends JVM { } } i = component.createImage(w, h); - g = i.getGraphics(); + } + Graphics getGraphics() { + if (surface != null) return surface.getGraphics(); + if (g != null) return g; + if (i != null) { g = i.getGraphics(); return g; } + return null; } - public int getHeight() { return i == null ? 0 : i.getHeight(null); } - public int getWidth() { return i == null ? 0 : i.getWidth(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); - g.setClip(0, 0, i.getWidth(null), i.getHeight(null)); + if (i != null) g.setClip(0, 0, i.getWidth(null), i.getHeight(null)); } - /** implemented with java.awt 1.1's setXORMode() */ - public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) { - - // XOR the target region - g.setXORMode(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff)); - g.setColor(new java.awt.Color(0x0, 0x0, 0x0)); - g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1); - - // blacken the area we want the glyph to cover - g.setPaintMode(); - g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1); - g.drawImage(((AWTGlyph)source).getImage(), dx, dy, 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); + } - // XOR back, turning black into the chosen rgb color - g.setXORMode(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff)); - g.setColor(new java.awt.Color(0x0, 0x0, 0x0)); - g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1); + public void stroke(org.ibex.graphics.Polygon p, int color) { p.stroke(this, color); } + public void fill(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { p.fill(this, paint); } - // restore the graphics context - g.setPaintMode(); + 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); } - // FIXME: try to use os acceleration - public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) { + // this doens't seem to work on Windows + public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, + int rgb, int argb) { + 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))); - if (x1 == x3 && x2 == x4) { - g.fillRect(x1, y1, x4 - x1, y2 - y1); - } else for(int y=y1; y _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; } - g.fillRect(_x1, _y1, _x2 - _x1, _y2 - _y1); - } + 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); } } @@ -257,32 +277,33 @@ public class AWT extends JVM { protected static class AWTSurface extends Surface.DoubleBufferedSurface implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener { - public void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) { - insets = (frame == null ? window : frame).getInsets(); - window.getGraphics().drawImage(((AWTPixelBuffer)s).i, - dx + insets.left, - dy + insets.top, - dx2 + insets.left, - dy2 + insets.top, - sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null); - } - - /** if (component instanceof Frame) then frame == window else frame == null */ + protected AWTPixelBuffer pb = null; + private Graphics g = null; Frame frame = null; Window window = null; + + 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); - - /** some JDKs let us recycle a single Dimension object when calling getSize() */ - Dimension singleSize = new Dimension(); - + 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); } + 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) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); } + 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) { @@ -292,40 +313,49 @@ public class AWT extends JVM { } 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 == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); } - public void update(Graphics gr) { paint(gr); } + 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.width), + Math.min(getHeight() - insets.top - insets.bottom, root.height)); + } // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize - int newwidth = Math.max(r.x - insets.left + r.width, root.width); - int newheight = Math.max(r.y - insets.top + r.height, root.height); - if (newwidth > root.width || newheight > root.height) - componentResized(window.getWidth() - insets.left - insets.right, - window.getHeight() - insets.top - insets.bottom); - - Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height); + int newwidth = Math.max(getWidth() - insets.left - insets.right, root.width); + int newheight = Math.max(getHeight() - insets.top - insets.bottom, root.height); + if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight); } } class InnerWindow extends Window { public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); } - public Dimension getMinimumSize() { - return new Dimension(root == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); } + 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); } } - public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); } - private int oldfill = 0x0; public void render() { // useful optimizatin; @@ -340,6 +370,12 @@ public class AWT extends JVM { 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 { @@ -352,7 +388,7 @@ public class AWT extends JVM { if (Log.on) Log.info(this, e); } - insets = window.getInsets(); + insets = getInsets(); window.addMouseListener(this); window.addKeyListener(this); @@ -418,11 +454,16 @@ public class AWT extends JVM { public void componentResized(ComponentEvent e) { // we have to periodically do this; I don't know why - insets = window.getInsets(); + 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); } + public void componentResized(int newwidth, int newheight) { + SizeChange(newwidth, newheight); + if (newwidth > root.width) Dirty(root.width, 0, newwidth-root.width, newheight); + if (newheight > root.height) Dirty(0, root.height, newwidth, newheight-root.height); + Refresh(); + } public void keyTyped(KeyEvent k) { } public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }