1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
10 import java.awt.datatransfer.*;
11 import java.awt.image.*;
12 import java.awt.event.*;
14 /** Platform subclass for all VM's providing AWT 1.1 functionality */
15 public class AWT extends JVM {
17 protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; }
18 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new AWTPixelBuffer(w, h); }
19 protected Picture _createPicture() { return new AWTPicture(); }
20 protected int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; }
21 protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; }
22 protected Surface _createSurface(Box b, boolean framed) { return new AWTSurface(b, framed); }
24 protected void postInit() {
25 System.setProperty("com.apple.mrj.application.live-resize", "true");
26 System.setProperty("com.apple.mrj.application.growbox.intrudes", "false");
27 if (Log.on) Log.log(Platform.class, " color depth = " + Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp");
30 protected void _criticalAbort(String message) {
31 if (Log.on) Log.log(this, message);
32 final Dialog d = new Dialog(new Frame(), "XWT Cannot Continue");
33 d.setLayout(new BorderLayout());
34 TextArea ta = new TextArea("XWT 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.xwt.Font.Glyph _createGlyph(org.xwt.Font f, char c) { return new AWTGlyph(f, c); }
119 protected static class AWTGlyph extends org.xwt.Font.Glyph {
120 private Image i = null;
121 static ColorModel cmodel = new DirectColorModel(8, 0x00000000, 0x00000000, 0x00000000, 0xFF);
122 public AWTGlyph(org.xwt.Font f, char c) { super(f, c); }
124 if (i == null && isLoaded) {
125 i = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, cmodel, data, 0, width));
126 MediaTracker mediatracker = new MediaTracker(new Canvas());
127 mediatracker.addImage(i, 1);
128 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
129 mediatracker.removeImage(i);
130 synchronized(AWTPixelBuffer.class) {
131 if (AWTPixelBuffer.component == null) {
132 AWTPixelBuffer.component = new Frame();
133 AWTPixelBuffer.component.setVisible(false);
134 AWTPixelBuffer.component.addNotify();
143 protected static class AWTPicture extends Picture {
145 public Image i = null;
146 private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
148 boolean initialized = false;
150 if (initialized) return;
152 i = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, cmodel, data, 0, width));
153 MediaTracker mediatracker = new MediaTracker(new Canvas());
154 mediatracker.addImage(i, 1);
155 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
156 mediatracker.removeImage(i);
157 synchronized(AWTPixelBuffer.class) {
158 if (AWTPixelBuffer.component == null) {
159 AWTPixelBuffer.component = new Frame();
160 AWTPixelBuffer.component.setVisible(false);
161 AWTPixelBuffer.component.addNotify();
167 protected static class AWTPixelBuffer extends PixelBuffer {
169 protected Image i = null;
170 protected Graphics g = null;
172 /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
173 static Component component = null;
175 protected AWTPixelBuffer() { }
176 public AWTPixelBuffer(int w, int h) {
177 synchronized(AWTPixelBuffer.class) {
178 if (component == null) {
179 component = new Frame();
180 component.setVisible(false);
181 component.addNotify();
184 i = component.createImage(w, h);
188 public int getHeight() { return i == null ? 0 : i.getHeight(null); }
189 public int getWidth() { return i == null ? 0 : i.getWidth(null); }
191 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
192 ((AWTPicture)source).init();
193 g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
194 g.drawImage(((AWTPicture)source).i, dx, dy, null);
195 g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
198 /** implemented with java.awt 1.1's setXORMode() */
199 public void drawGlyph(org.xwt.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
201 // XOR the target region
202 g.setXORMode(new Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
203 g.setColor(new Color(0x0, 0x0, 0x0));
204 g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
206 // blacken the area we want the glyph to cover
208 g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
209 g.drawImage(((AWTGlyph)source).getImage(), dx, dy, null);
210 g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
212 // XOR back, turning black into the chosen rgb color
213 g.setXORMode(new Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
214 g.setColor(new Color(0x0, 0x0, 0x0));
215 g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
217 // restore the graphics context
221 // FIXME: try to use os acceleration
222 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
223 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
224 if (x1 == x3 && x2 == x4) {
225 g.fillRect(x1, y1, x4 - x1, y2 - y1);
226 } else for(int y=y1; y<y2; y++) {
227 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
228 int _y1 = (int)Math.floor(y);
229 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
230 int _y2 = (int)Math.floor(y) + 1;
231 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
232 g.fillRect(_x1, _y1, _x2 - _x1, _y2 - _y1);
238 protected static class AWTSurface extends Surface.DoubleBufferedSurface
239 implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
241 public void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
242 if (ourGraphics == null) ourGraphics = window.getGraphics();
243 insets = (frame == null ? window : frame).getInsets();
244 ourGraphics.drawImage(((AWTPixelBuffer)s).i,
249 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
252 /** if (component instanceof Frame) then frame == window else frame == null */
254 Window window = null;
256 /** our component's insets */
257 protected Insets insets = new Insets(0, 0, 0, 0);
259 /** a Graphics context on <code>window</code> */
260 protected Graphics ourGraphics = null;
262 /** some JDKs let us recycle a single Dimension object when calling getSize() */
263 Dimension singleSize = new Dimension();
265 public void toBack() { if (window != null) window.toBack(); }
266 public void toFront() { if (window != null) window.toFront(); }
267 public void setLocation() { window.setLocation(root.x, root.y); }
268 public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
269 public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
270 public void _setSize(int width, int height) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
271 public void setInvisible(boolean b) { window.setVisible(!b); }
272 protected void _setMinimized(boolean b) { if (Log.on) Log.log(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
273 protected void _setMaximized(boolean b) {
275 if (Log.on) Log.log(this, "JDK 1.1 platforms cannot unmaximize windows");
278 window.setLocation(new Point(0, 0));
279 window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
282 class InnerFrame extends Frame {
283 public InnerFrame() throws java.lang.UnsupportedOperationException { }
284 public void update(Graphics gr) { paint(gr); }
285 public void paint(Graphics gr) {
286 Rectangle r = gr.getClipBounds();
288 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
289 int newwidth = Math.max(r.x - insets.left + r.width, root.width);
290 int newheight = Math.max(r.y - insets.top + r.height, root.height);
291 if (newwidth > root.width || newheight > root.height)
292 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
294 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
298 class InnerWindow extends Window {
299 public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
300 public void update(Graphics gr) { paint(gr); }
301 public void paint(Graphics gr) {
302 Rectangle r = gr.getClipBounds();
303 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
307 AWTSurface(Box root, boolean framed) {
309 System.out.println("Y");
311 if (framed) window = frame = new InnerFrame();
312 else window = new InnerWindow();
314 // this is here to catch HeadlessException on jdk1.4
315 } catch (java.lang.UnsupportedOperationException e) {
316 if (Log.on) Log.log(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
317 if (Log.on) Log.log(this, e);
320 insets = window.getInsets();
322 window.addMouseListener(this);
323 window.addKeyListener(this);
324 window.addComponentListener(this);
325 window.addMouseMotionListener(this);
326 window.addWindowListener(this);
328 // IMPORTANT: this must be called before render() to ensure
329 // that our peer has been created
333 protected void makeVisible() { window.setVisible(true); }
335 public void _dispose() {
336 window.removeMouseListener(this);
338 // removed to work around a jdk1.3 bug
339 /* window.removeKeyListener(this); */
341 window.removeComponentListener(this);
342 window.removeMouseMotionListener(this);
343 window.removeWindowListener(this);
347 public void syncCursor() {
348 if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
349 else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
350 else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
351 else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
352 else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
353 else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
354 else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
355 else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
356 else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
357 else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
358 else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
359 else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
360 else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
361 else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
364 // AWT Message translation ////////////////////////////////////////////////////////////////
366 // these functions are all executed in the AWT thread, not the
367 // MessageQueue thread. As a result, they must be *extremely*
368 // careful about invoking methods on instances of Box. Currently,
369 // they should only enqueue messages, use Box.whoIs()
370 // (unsynchronized but thought to be safe), and modify members of
373 public void componentHidden(ComponentEvent e) { }
374 public void componentShown(ComponentEvent e) { }
375 public void windowOpened(WindowEvent e) { }
376 public void windowClosed(WindowEvent e) { }
377 public void windowClosing(WindowEvent e) { Close(); }
378 public void windowIconified(WindowEvent e) { Minimized(true); }
379 public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.width, root.height); Minimized(false); }
380 public void windowActivated(WindowEvent e) { Focused(true); }
381 public void windowDeactivated(WindowEvent e) { Focused(false); }
382 public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
384 public void componentResized(ComponentEvent e) {
385 // we have to periodically do this; I don't know why
386 insets = window.getInsets();
387 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
390 public void componentResized(int newwidth, int newheight) {
391 int oldwidth = root.width;
392 int oldheight = root.height;
393 SizeChange(newwidth, newheight);
395 // we do this because JVMs which don't clear the background won't force repaints of these areas
396 root.dirty(Math.min(oldwidth, newwidth), 0, Math.abs(oldwidth - newwidth), Math.max(oldheight, newheight));
397 root.dirty(0, Math.min(oldheight, newheight), Math.max(oldwidth, newwidth), Math.abs(oldheight - newheight));
402 public void keyTyped(KeyEvent k) { }
403 public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
404 public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
405 public void mouseExited(MouseEvent m) { mouseMoved(m); }
406 public void mouseEntered(MouseEvent m) { mouseMoved(m); }
407 public void mouseDragged(MouseEvent m) { mouseMoved(m); }
408 public void mouseMoved(MouseEvent m) {
410 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
411 int newwidth = window.getWidth() - insets.left - insets.right;
412 int newheight = window.getHeight() - insets.top - insets.bottom;
413 if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
415 Move(m.getX() - insets.left, m.getY() - insets.top);
417 public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
418 public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
419 public void mouseClicked(MouseEvent m) {
420 if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
421 else Click(modifiersToButtonNumber(m.getModifiers()));
424 String translateKey(KeyEvent k) {
425 switch (k.getKeyCode()) {
426 case KeyEvent.VK_ALT: return "alt";
427 case KeyEvent.VK_BACK_SPACE: return "back_space";
428 case KeyEvent.VK_CONTROL: return "control";
429 case KeyEvent.VK_DELETE: return "delete";
430 case KeyEvent.VK_DOWN: return "down";
431 case KeyEvent.VK_END: return "end";
432 case KeyEvent.VK_ENTER: return "enter";
433 case KeyEvent.VK_ESCAPE: return "escape";
434 case KeyEvent.VK_F1: return "f1";
435 case KeyEvent.VK_F10: return "f10";
436 case KeyEvent.VK_F11: return "f11";
437 case KeyEvent.VK_F12: return "f12";
438 case KeyEvent.VK_F2: return "f2";
439 case KeyEvent.VK_F3: return "f3";
440 case KeyEvent.VK_F4: return "f4";
441 case KeyEvent.VK_F5: return "f5";
442 case KeyEvent.VK_F6: return "f6";
443 case KeyEvent.VK_F7: return "f7";
444 case KeyEvent.VK_F8: return "f8";
445 case KeyEvent.VK_F9: return "f9";
446 case KeyEvent.VK_HOME: return "home";
447 case KeyEvent.VK_INSERT: return "insert";
448 case KeyEvent.VK_LEFT: return "left";
449 case KeyEvent.VK_META: return "alt";
450 case KeyEvent.VK_PAGE_DOWN: return "page_down";
451 case KeyEvent.VK_PAGE_UP: return "page_up";
452 case KeyEvent.VK_PAUSE: return "pause";
453 case KeyEvent.VK_PRINTSCREEN: return "printscreen";
454 case KeyEvent.VK_RIGHT: return "right";
455 case KeyEvent.VK_SHIFT: return "shift";
456 case KeyEvent.VK_TAB: return "tab";
457 case KeyEvent.VK_UP: return "up";
459 char c = k.getKeyChar();
460 if (c >= 1 && c <= 26) c = (char)('a' + c - 1);
461 return String.valueOf(c);
466 protected void _decodeJPEG(InputStream is, Picture p) {
468 Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is));
469 MediaTracker mediatracker = new MediaTracker(new Canvas());
470 mediatracker.addImage(i, 1);
471 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
472 mediatracker.removeImage(i);
473 final int width = i.getWidth(null);
474 final int height = i.getHeight(null);
475 final int[] data = new int[width * height];
476 PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width);
478 if ((pg.getStatus() & ImageObserver.ABORT) != 0)
479 Log.log(this, "PixelGrabber reported an error while decoding JPEG image");
483 } catch (Exception e) {
484 Log.log(this, "Exception caught while decoding JPEG image");