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