1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.ibex.util.*;
8 import java.awt.datatransfer.*;
9 import java.awt.image.*;
10 import java.awt.event.*;
11 import org.ibex.graphics.*;
12 import org.ibex.core.*;
13 import org.ibex.net.*;
15 /** Platform subclass for all VM's providing AWT 1.1 functionality */
16 public class AWT extends JVM {
18 protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; }
19 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new AWTPixelBuffer(w, h); }
20 protected Picture _createPicture(JS r) { return new AWTPicture(r); }
21 protected int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; }
22 protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; }
23 protected Surface _createSurface(Box b, boolean framed) { return new AWTSurface(b, framed); }
25 protected void postInit() {
26 if (Log.on) Log.diag(Platform.class, " color depth = " +
27 Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp");
30 protected void _criticalAbort(String message) {
31 if (Log.on) Log.info(this, message);
32 final Dialog d = new Dialog(new Frame(), "Ibex Cannot Continue");
33 d.setLayout(new BorderLayout());
34 TextArea ta = new TextArea("Ibex cannot continue because:\n\n" + message, 10, 80);
35 ta.setEditable(false);
37 Button b = new Button("OK");
38 b.addActionListener(new ActionListener() {
39 public void actionPerformed(ActionEvent e) {
47 new Semaphore().block();
50 protected String _getClipBoard() {
51 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
52 if (cb == null) return null;
53 Transferable clipdata = cb.getContents(null);
54 try { return (String)clipdata.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex) { return null; }
57 protected void _setClipBoard(String s) {
58 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
59 if (clipboard == null) return;
60 StringSelection clipString = new StringSelection(s);
61 clipboard.setContents(clipString, clipString);
64 /** some platforms (cough, cough, NetscapeVM) have totally broken modifier masks; they will need to override this */
65 protected static int modifiersToButtonNumber(int modifiers) {
66 if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1;
67 if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
68 // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
69 if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 2;
72 if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
73 // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
74 if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 3;
80 static class FileDialogHelper extends FileDialog implements WindowListener, ComponentListener {
82 public FileDialogHelper(String suggestedFileName, Semaphore s, boolean write) {
83 super(new Frame(), write ? "Save" : "Open", write ? FileDialog.SAVE : FileDialog.LOAD);
85 addWindowListener(this);
86 addComponentListener(this);
87 if (suggestedFileName.indexOf(File.separatorChar) == -1) {
88 setFile(suggestedFileName);
90 setDirectory(suggestedFileName.substring(0, suggestedFileName.lastIndexOf(File.separatorChar)));
91 setFile(suggestedFileName.substring(suggestedFileName.lastIndexOf(File.separatorChar) + 1));
95 public void windowActivated(WindowEvent e) { }
96 public void windowClosed(WindowEvent e) { s.release(); }
97 public void windowClosing(WindowEvent e) { }
98 public void windowDeactivated(WindowEvent e) { }
99 public void windowDeiconified(WindowEvent e) { }
100 public void windowIconified(WindowEvent e) { }
101 public void windowOpened(WindowEvent e) { }
102 public void componentHidden(ComponentEvent e) { s.release(); }
103 public void componentMoved(ComponentEvent e) { }
104 public void componentResized(ComponentEvent e) { }
105 public void componentShown(ComponentEvent e) { }
108 protected String _fileDialog(String suggestedFileName, boolean write) {
109 final Semaphore s = new Semaphore();
110 FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write);
112 return fd.getDirectory() == null ? null : (fd.getDirectory() + File.separatorChar + fd.getFile());
116 // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
118 protected org.ibex.graphics.Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new AWTGlyph(f, c); }
119 protected static class AWTGlyph extends org.ibex.graphics.Font.Glyph {
120 private Image i = null;
121 private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
123 // this doesn't work on Win32 because the JVM is broken
125 static final ColorModel cmodel = new ColorModel(8) {
126 public int getRed(int p) { return 0; }
127 public int getGreen(int p) { return 0; }
128 public int getBlue(int p) { return 0; }
129 public int getAlpha(int p) { return p & 0xFF; }
133 public AWTGlyph(org.ibex.graphics.Font f, char c) { super(f, c); }
135 if (i == null && isLoaded) {
137 int[] data2 = new int[data.length];
138 for(int i=0; i<data2.length; i++) data2[i] = ((data[i]) & 0xff) << 24;
140 MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data2, 0, width);
141 mis.setAnimated(true);
142 i = Toolkit.getDefaultToolkit().createImage(mis);
143 MediaTracker mediatracker = new MediaTracker(new Canvas());
144 mediatracker.addImage(i, 1);
145 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
146 mediatracker.removeImage(i);
147 synchronized(AWTPixelBuffer.class) {
148 if (AWTPixelBuffer.component == null) {
149 AWTPixelBuffer.component = new Frame();
150 AWTPixelBuffer.component.setVisible(false);
151 AWTPixelBuffer.component.addNotify();
160 protected static class AWTPicture extends Picture {
161 public Image i = null;
162 private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
164 boolean initialized = false;
165 public AWTPicture(JS r) { super(r); }
167 if (initialized) return;
169 MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data, 0, width);
170 mis.setAnimated(true);
171 i = Toolkit.getDefaultToolkit().createImage(mis);
172 MediaTracker mediatracker = new MediaTracker(new Canvas());
173 mediatracker.addImage(i, 1);
174 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
175 mediatracker.removeImage(i);
176 synchronized(AWTPixelBuffer.class) {
177 if (AWTPixelBuffer.component == null) {
178 AWTPixelBuffer.component = new Frame();
179 AWTPixelBuffer.component.setVisible(false);
180 AWTPixelBuffer.component.addNotify();
186 protected static class AWTPixelBuffer extends PixelBuffer {
188 protected Image i = null;
189 protected Graphics g = null;
191 /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
192 static Component component = null;
194 protected AWTPixelBuffer() { }
195 public AWTPixelBuffer(int w, int h) {
196 synchronized(AWTPixelBuffer.class) {
197 if (component == null) {
198 component = new Frame();
199 component.setVisible(false);
200 component.addNotify();
203 i = component.createImage(w, h);
207 public int getHeight() { return i == null ? 0 : i.getHeight(null); }
208 public int getWidth() { return i == null ? 0 : i.getWidth(null); }
210 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
211 ((AWTPicture)source).init();
212 g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
213 g.drawImage(((AWTPicture)source).i, dx, dy, null);
214 g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
217 /** implemented with java.awt 1.1's setXORMode() */
218 public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
220 // XOR the target region
221 g.setXORMode(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
222 g.setColor(new java.awt.Color(0x0, 0x0, 0x0));
223 g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
225 // blacken the area we want the glyph to cover
227 g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
228 g.drawImage(((AWTGlyph)source).getImage(), dx, dy, null);
229 g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
231 // XOR back, turning black into the chosen rgb color
232 g.setXORMode(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
233 g.setColor(new java.awt.Color(0x0, 0x0, 0x0));
234 g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
236 // restore the graphics context
240 // FIXME: try to use os acceleration
241 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
242 g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
243 if (x1 == x3 && x2 == x4) {
244 g.fillRect(x1, y1, x4 - x1, y2 - y1);
245 } else for(int y=y1; y<y2; y++) {
246 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
247 int _y1 = (int)Math.floor(y);
248 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
249 int _y2 = (int)Math.floor(y) + 1;
250 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
251 g.fillRect(_x1, _y1, _x2 - _x1, _y2 - _y1);
257 protected static class AWTSurface extends Surface.DoubleBufferedSurface
258 implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
260 public void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
261 insets = (frame == null ? window : frame).getInsets();
262 window.getGraphics().drawImage(((AWTPixelBuffer)s).i,
267 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
270 /** if (component instanceof Frame) then frame == window else frame == null */
272 Window window = null;
274 /** our component's insets */
275 protected Insets insets = new Insets(0, 0, 0, 0);
277 /** some JDKs let us recycle a single Dimension object when calling getSize() */
278 Dimension singleSize = new Dimension();
280 public void toBack() { if (window != null) window.toBack(); }
281 public void toFront() { if (window != null) window.toFront(); }
282 public void setLocation() { window.setLocation(root.x, root.y); }
283 public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
284 public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
285 public void _setSize(int width, int height) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
286 public void setInvisible(boolean b) { window.setVisible(!b); }
287 protected void _setMinimized(boolean b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
288 protected void _setMaximized(boolean b) {
290 if (Log.on) Log.info(this, "JDK 1.1 platforms cannot unmaximize windows");
293 window.setLocation(new Point(0, 0));
294 window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
297 class InnerFrame extends Frame {
298 public InnerFrame() throws java.lang.UnsupportedOperationException { }
299 public Dimension getMinimumSize() {
300 return new Dimension(root == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); }
301 public void update(Graphics gr) { paint(gr); }
302 public void paint(Graphics gr) {
303 Rectangle r = gr.getClipBounds();
305 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
306 int newwidth = Math.max(r.x - insets.left + r.width, root.width);
307 int newheight = Math.max(r.y - insets.top + r.height, root.height);
308 if (newwidth > root.width || newheight > root.height)
309 componentResized(window.getWidth() - insets.left - insets.right,
310 window.getHeight() - insets.top - insets.bottom);
312 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
316 class InnerWindow extends Window {
317 public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
318 public Dimension getMinimumSize() {
319 return new Dimension(root == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); }
320 public void update(Graphics gr) { paint(gr); }
321 public void paint(Graphics gr) {
322 Rectangle r = gr.getClipBounds();
323 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
327 public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); }
329 private int oldfill = 0x0;
330 public void render() {
331 // useful optimizatin;
332 if (oldfill != root.fillcolor) {
333 oldfill = root.fillcolor;
334 window.setBackground((root.fillcolor & 0xFF000000) == 0 ?
335 java.awt.Color.white :
336 new java.awt.Color((root.fillcolor >> 16) & 0xff,
337 (root.fillcolor >> 8) & 0xff,
338 (root.fillcolor) & 0xff));
343 AWTSurface(Box root, boolean framed) {
346 if (framed) window = frame = new InnerFrame();
347 else window = new InnerWindow();
349 // this is here to catch HeadlessException on jdk1.4
350 } catch (java.lang.UnsupportedOperationException e) {
351 if (Log.on) Log.info(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
352 if (Log.on) Log.info(this, e);
355 insets = window.getInsets();
357 window.addMouseListener(this);
358 window.addKeyListener(this);
359 window.addComponentListener(this);
360 window.addMouseMotionListener(this);
361 window.addWindowListener(this);
363 // IMPORTANT: this must be called before render() to ensure
364 // that our peer has been created
368 protected void makeVisible() { window.setVisible(true); }
370 public void _dispose() {
371 window.removeMouseListener(this);
373 // removed to work around a jdk1.3 bug
374 /* window.removeKeyListener(this); */
376 window.removeComponentListener(this);
377 window.removeMouseMotionListener(this);
378 window.removeWindowListener(this);
382 public void syncCursor() {
383 if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
384 else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
385 else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
386 else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
387 else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
388 else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
389 else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
390 else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
391 else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
392 else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
393 else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
394 else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
395 else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
396 else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
399 // AWT Message translation ////////////////////////////////////////////////////////////////
401 // these functions are all executed in the AWT thread, not the
402 // MessageQueue thread. As a result, they must be *extremely*
403 // careful about invoking methods on instances of Box. Currently,
404 // they should only enqueue messages, use Box.whoIs()
405 // (unsynchronized but thought to be safe), and modify members of
408 public void componentHidden(ComponentEvent e) { }
409 public void componentShown(ComponentEvent e) { }
410 public void windowOpened(WindowEvent e) { }
411 public void windowClosed(WindowEvent e) { }
412 public void windowClosing(WindowEvent e) { Close(); }
413 public void windowIconified(WindowEvent e) { Minimized(true); }
414 public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.width, root.height); Minimized(false); }
415 public void windowActivated(WindowEvent e) { Focused(true); }
416 public void windowDeactivated(WindowEvent e) { Focused(false); }
417 public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
419 public void componentResized(ComponentEvent e) {
420 // we have to periodically do this; I don't know why
421 insets = window.getInsets();
422 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
425 public void componentResized(int newwidth, int newheight) { SizeChange(newwidth, newheight); }
427 public void keyTyped(KeyEvent k) { }
428 public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
429 public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
430 public void mouseExited(MouseEvent m) { mouseMoved(m); }
431 public void mouseEntered(MouseEvent m) { mouseMoved(m); }
432 public void mouseDragged(MouseEvent m) { mouseMoved(m); }
433 public void mouseMoved(MouseEvent m) {
435 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
436 int newwidth = window.getWidth() - insets.left - insets.right;
437 int newheight = window.getHeight() - insets.top - insets.bottom;
438 if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
440 Move(m.getX() - insets.left, m.getY() - insets.top);
442 public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
443 public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
444 public void mouseClicked(MouseEvent m) {
445 if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
446 else Click(modifiersToButtonNumber(m.getModifiers()));
449 String translateKey(KeyEvent k) {
450 switch (k.getKeyCode()) {
451 case KeyEvent.VK_ALT: return "alt";
452 case KeyEvent.VK_BACK_SPACE: return "back_space";
453 case KeyEvent.VK_CONTROL: return "control";
454 case KeyEvent.VK_DELETE: return "delete";
455 case KeyEvent.VK_DOWN: return "down";
456 case KeyEvent.VK_END: return "end";
457 case KeyEvent.VK_ENTER: return "enter";
458 case KeyEvent.VK_ESCAPE: return "escape";
459 case KeyEvent.VK_F1: return "f1";
460 case KeyEvent.VK_F10: return "f10";
461 case KeyEvent.VK_F11: return "f11";
462 case KeyEvent.VK_F12: return "f12";
463 case KeyEvent.VK_F2: return "f2";
464 case KeyEvent.VK_F3: return "f3";
465 case KeyEvent.VK_F4: return "f4";
466 case KeyEvent.VK_F5: return "f5";
467 case KeyEvent.VK_F6: return "f6";
468 case KeyEvent.VK_F7: return "f7";
469 case KeyEvent.VK_F8: return "f8";
470 case KeyEvent.VK_F9: return "f9";
471 case KeyEvent.VK_HOME: return "home";
472 case KeyEvent.VK_INSERT: return "insert";
473 case KeyEvent.VK_LEFT: return "left";
474 case KeyEvent.VK_META: return "alt";
475 case KeyEvent.VK_PAGE_DOWN: return "page_down";
476 case KeyEvent.VK_PAGE_UP: return "page_up";
477 case KeyEvent.VK_PAUSE: return "pause";
478 case KeyEvent.VK_PRINTSCREEN: return "printscreen";
479 case KeyEvent.VK_RIGHT: return "right";
480 case KeyEvent.VK_SHIFT: return "shift";
481 case KeyEvent.VK_TAB: return "tab";
482 case KeyEvent.VK_UP: return "up";
484 char c = k.getKeyChar();
485 if (c >= 1 && c <= 26) c = (char)('a' + c - 1);
486 return String.valueOf(c);
491 protected void _decodeJPEG(InputStream is, Picture p) {
493 Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is));
494 MediaTracker mediatracker = new MediaTracker(new Canvas());
495 mediatracker.addImage(i, 1);
496 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
497 mediatracker.removeImage(i);
498 final int width = i.getWidth(null);
499 final int height = i.getHeight(null);
500 final int[] data = new int[width * height];
501 PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width);
503 if ((pg.getStatus() & ImageObserver.ABORT) != 0)
504 Log.info(this, "PixelGrabber reported an error while decoding JPEG image");
508 } catch (Exception e) {
509 Log.info(this, "Exception caught while decoding JPEG image");