1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
6 import org.mozilla.javascript.*;
11 import java.awt.datatransfer.*;
12 import java.awt.image.*;
13 import java.awt.event.*;
16 /** Platform subclass for all VM's providing AWT 1.1 functionality */
17 public class AWT extends Platform {
19 protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; }
20 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new AWTDoubleBuffer(w, h); }
21 protected Picture _createPicture(int[] b, int w, int h) { return new AWTPicture(b, w, h); }
22 protected int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; }
23 protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; }
24 protected Surface _createSurface(Box b, boolean framed) { return new AWTSurface(b, framed); }
25 protected int _stringWidth(String font, String text) { return getFont(font).metrics.stringWidth(text); }
26 protected int _getMaxAscent(String font) { return getFont(font).metrics.getMaxAscent(); }
27 protected int _getMaxDescent(String font) { return getFont(font).metrics.getMaxDescent(); }
28 protected boolean _supressDirtyOnResize() { return true; }
30 protected String _getClipBoard() {
31 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
32 if (cb == null) return null;
33 Transferable clipdata = cb.getContents(null);
34 try { return (String)clipdata.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex) { return null; }
37 protected void _setClipBoard(String s) {
38 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
39 if (clipboard == null) return;
40 StringSelection clipString = new StringSelection(s);
41 clipboard.setContents(clipString, clipString);
44 /** some platforms (cough, cough, NetscapeVM) have totally broken modifier masks; they will need to override this */
45 protected static int modifiersToButtonNumber(int modifiers) {
46 if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1;
47 if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) return 3;
48 if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) return 2;
52 static class FileDialogHelper extends FileDialog implements WindowListener, ComponentListener {
54 public FileDialogHelper(String suggestedFileName, Semaphore s, boolean write) {
55 super(new Frame(), write ? "Save" : "Open", write ? FileDialog.SAVE : FileDialog.LOAD);
57 addWindowListener(this);
58 addComponentListener(this);
59 if (suggestedFileName.indexOf(File.separatorChar) == -1) {
60 setFile(suggestedFileName);
62 setDirectory(suggestedFileName.substring(0, suggestedFileName.lastIndexOf(File.separatorChar)));
63 setFile(suggestedFileName.substring(suggestedFileName.lastIndexOf(File.separatorChar) + 1));
67 public void windowActivated(WindowEvent e) { }
68 public void windowClosed(WindowEvent e) { s.release(); }
69 public void windowClosing(WindowEvent e) { }
70 public void windowDeactivated(WindowEvent e) { }
71 public void windowDeiconified(WindowEvent e) { }
72 public void windowIconified(WindowEvent e) { }
73 public void windowOpened(WindowEvent e) { }
74 public void componentHidden(ComponentEvent e) { s.release(); }
75 public void componentMoved(ComponentEvent e) { }
76 public void componentResized(ComponentEvent e) { }
77 public void componentShown(ComponentEvent e) { }
80 protected String _fileDialog(String suggestedFileName, boolean write) {
81 final Semaphore s = new Semaphore();
82 FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write);
84 return fd.getDirectory() + File.separatorChar + fd.getFile();
88 // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
90 protected static class AWTPicture implements Picture {
91 public int getHeight() { return i.getHeight(null); }
92 public int getWidth() { return i.getWidth(null); }
93 public int[] getData() { return data; }
96 public Image i = null;
97 private static MediaTracker mediatracker = new MediaTracker(new Canvas());
98 private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
100 public AWTPicture(int[] b, int w, int h) {
102 Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, cmodel, b, 0, w));
103 mediatracker.addImage(img, 1);
104 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
105 mediatracker.removeImage(img);
110 protected static class AWTDoubleBuffer implements DoubleBuffer {
112 protected Image i = null;
113 protected Graphics g = null;
115 /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
116 static Component component = null;
118 protected AWTDoubleBuffer() { }
119 public AWTDoubleBuffer(int w, int h) {
120 synchronized(AWTDoubleBuffer.class) {
121 if (component == null) {
122 component = new Frame();
123 component.setVisible(false);
124 component.addNotify();
127 i = component.createImage(w, h);
131 public int getHeight() { return i == null ? 0 : i.getHeight(null); }
132 public int getWidth() { return i == null ? 0 : i.getWidth(null); }
133 public void setClip(int x, int y, int x2, int y2) { g.setClip(x, y, x2 - x, y2 - y); }
135 public void drawPicture(Picture source, int x, int y) {
136 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
139 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
140 g.drawImage(((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
143 public void drawString(String font, String text, int x, int y, int argb) {
144 // FEATURE: use an LRU cache for Color objects
145 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
146 g.setFont(getFont(font));
147 g.drawString(text, x, y + 2);
150 public void fillRect(int x, int y, int x2, int y2, int argb) {
151 // FEATURE: use an LRU cache for Color objects
152 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
153 g.fillRect(x, y, x2 - x, y2 - y);
159 protected static class AWTSurface extends Surface
160 implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
162 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
163 if (ourGraphics == null) ourGraphics = window.getGraphics();
164 ourGraphics.drawImage(((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
165 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
168 /** if (component instanceof Frame) then frame == window else frame == null */
170 Window window = null;
172 /** our component's insets */
173 protected Insets insets = new Insets(0, 0, 0, 0);
175 /** a Graphics context on <code>window</code> */
176 protected Graphics ourGraphics = null;
178 /** some JDKs let us recycle a single Dimension object when calling getSize() */
179 Dimension singleSize = new Dimension();
181 public void toBack() { if (window != null) window.toBack(); }
182 public void toFront() { if (window != null) window.toFront(); }
183 public void setLocation(int x, int y) { window.setLocation(x, y); }
184 public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
185 public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
186 public void setSize(int width, int height) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
187 public void setInvisible(boolean b) { window.setVisible(!b); }
188 protected void _setMinimized(boolean b) { if (Log.on) Log.log(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
189 protected void _setMaximized(boolean b) {
191 if (Log.on) Log.log(this, "JDK 1.1 platforms cannot unmaximize windows");
194 window.setLocation(new Point(0, 0));
195 window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
198 class InnerFrame extends Frame {
199 public InnerFrame() throws java.lang.UnsupportedOperationException { }
200 public void update(Graphics gr) { paint(gr); }
201 public void paint(Graphics gr) {
202 Rectangle r = gr.getClipBounds();
204 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
205 int newwidth = Math.max(r.x - insets.left + r.width, width);
206 int newheight = Math.max(r.y - insets.top + r.height, height);
207 if (newwidth > width || newheight > height)
208 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
210 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
212 // this is safe because AWT's low-level drawing functions are threadsafe
213 blitDirtyScreenRegions();
217 class InnerWindow extends Window {
218 public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
219 public void update(Graphics gr) { paint(gr); }
220 public void paint(Graphics gr) {
221 Rectangle r = gr.getClipBounds();
222 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
224 // this is safe because AWT's low-level drawing functions are threadsafe
225 blitDirtyScreenRegions();
229 AWTSurface(Box root, boolean framed) {
232 if (framed) window = frame = new InnerFrame();
233 else window = new InnerWindow();
235 // this is here to catch HeadlessException on jdk1.4
236 } catch (java.lang.UnsupportedOperationException e) {
237 if (Log.on) Log.log(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
238 if (Log.on) Log.log(this, e);
241 insets = window.getInsets();
243 window.addMouseListener(this);
244 window.addKeyListener(this);
245 window.addComponentListener(this);
246 window.addMouseMotionListener(this);
247 window.addWindowListener(this);
249 // IMPORTANT: this must be called before render() to ensure
250 // that our peer has been created
251 window.setVisible(true);
255 public void _dispose() {
256 window.removeMouseListener(this);
258 // removed to work around a jdk1.3 bug
259 /* window.removeKeyListener(this); */
261 window.removeComponentListener(this);
262 window.removeMouseMotionListener(this);
263 window.removeWindowListener(this);
267 public void syncCursor() {
268 if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
269 else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
270 else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
271 else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
272 else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
273 else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
274 else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
275 else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
276 else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
277 else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
278 else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
279 else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
280 else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
281 else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
284 // AWT Message translation ////////////////////////////////////////////////////////////////
286 // these functions are all executed in the AWT thread, not the
287 // MessageQueue thread. As a result, they must be *extremely*
288 // careful about invoking methods on instances of Box. Currently,
289 // they should only enqueue messages, use Box.whoIs()
290 // (unsynchronized but thought to be safe), and modify members of
293 public void componentHidden(ComponentEvent e) { }
294 public void componentShown(ComponentEvent e) { }
295 public void windowOpened(WindowEvent e) { }
296 public void windowClosed(WindowEvent e) { }
297 public void windowClosing(WindowEvent e) { Close(); }
298 public void windowIconified(WindowEvent e) { Minimized(true); }
299 public void windowDeiconified(WindowEvent e) { dirty(0, 0, width, height); Minimized(false); }
300 public void windowActivated(WindowEvent e) { Focused(true); }
301 public void windowDeactivated(WindowEvent e) { Focused(false); }
302 public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x, window.getLocation().y); }
304 public void componentResized(ComponentEvent e) {
305 // we have to periodically do this; I don't know why
306 insets = window.getInsets();
307 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
310 public void componentResized(int newwidth, int newheight) {
311 int oldwidth = width;
312 int oldheight = height;
313 SizeChange(newwidth, newheight);
315 // we do this because JVMs which don't clear the background won't force repaints of these areas
316 root.dirty(Math.min(oldwidth, newwidth), 0, Math.abs(oldwidth - newwidth), Math.max(oldheight, newheight));
317 root.dirty(0, Math.min(oldheight, newheight), Math.max(oldwidth, newwidth), Math.abs(oldheight - newheight));
322 public void keyTyped(KeyEvent k) { }
323 public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
324 public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
325 public void mouseExited(MouseEvent m) { mouseMoved(m); }
326 public void mouseEntered(MouseEvent m) { mouseMoved(m); }
327 public void mouseDragged(MouseEvent m) { mouseMoved(m); }
328 public void mouseMoved(MouseEvent m) {
330 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
331 int newwidth = window.getWidth() - insets.left - insets.right;
332 int newheight = window.getHeight() - insets.top - insets.bottom;
333 if (newwidth != width || newheight != height) componentResized(newwidth, newheight);
335 Move(m.getX() - insets.left, m.getY() - insets.top);
337 public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
338 public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
339 public void mouseClicked(MouseEvent m) {
340 if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
341 else Click(modifiersToButtonNumber(m.getModifiers()));
344 String translateKey(KeyEvent k) {
345 switch (k.getKeyCode()) {
346 case KeyEvent.VK_ALT: return "alt";
347 case KeyEvent.VK_BACK_SPACE: return "back_space";
348 case KeyEvent.VK_CONTROL: return "control";
349 case KeyEvent.VK_DELETE: return "delete";
350 case KeyEvent.VK_DOWN: return "down";
351 case KeyEvent.VK_END: return "end";
352 case KeyEvent.VK_ENTER: return "enter";
353 case KeyEvent.VK_ESCAPE: return "escape";
354 case KeyEvent.VK_F1: return "f1";
355 case KeyEvent.VK_F10: return "f10";
356 case KeyEvent.VK_F11: return "f11";
357 case KeyEvent.VK_F12: return "f12";
358 case KeyEvent.VK_F2: return "f2";
359 case KeyEvent.VK_F3: return "f3";
360 case KeyEvent.VK_F4: return "f4";
361 case KeyEvent.VK_F5: return "f5";
362 case KeyEvent.VK_F6: return "f6";
363 case KeyEvent.VK_F7: return "f7";
364 case KeyEvent.VK_F8: return "f8";
365 case KeyEvent.VK_F9: return "f9";
366 case KeyEvent.VK_HOME: return "home";
367 case KeyEvent.VK_INSERT: return "insert";
368 case KeyEvent.VK_LEFT: return "left";
369 case KeyEvent.VK_META: return "alt";
370 case KeyEvent.VK_PAGE_DOWN: return "page_down";
371 case KeyEvent.VK_PAGE_UP: return "page_up";
372 case KeyEvent.VK_PAUSE: return "pause";
373 case KeyEvent.VK_PRINTSCREEN: return "printscreen";
374 case KeyEvent.VK_RIGHT: return "right";
375 case KeyEvent.VK_SHIFT: return "shift";
376 case KeyEvent.VK_TAB: return "tab";
377 case KeyEvent.VK_UP: return "up";
379 char c = k.getKeyChar();
380 if (c >= 1 && c <= 26) c = (char)('a' + c - 1);
381 return String.valueOf(c);
386 // Font Handling Stuff //////////////////////////////////////////////////////////
388 protected String[] _listFonts() { return fontList; }
389 private static String[] fontList;
391 String[] awtfonts = Toolkit.getDefaultToolkit().getFontList();
392 fontList = new String[awtfonts.length * 4];
393 for(int i=0; i<awtfonts.length; i++) {
394 fontList[i * 4] = awtfonts[i] + "*";
395 fontList[i * 4 + 1] = awtfonts[i] + "*b";
396 fontList[i * 4 + 2] = awtfonts[i] + "*i";
397 fontList[i * 4 + 3] = awtfonts[i] + "*bi";
401 private static Hash fontCache = new Hash();
402 private static ParsedFont pf = new ParsedFont();
403 private static MetricatedFont getFont(String font) {
404 MetricatedFont ret = (MetricatedFont)fontCache.get(font);
407 if (pf.name.equals("tty")) pf.name = "monospace";
408 ret = new MetricatedFont(pf.name, (pf.bold ? Font.BOLD : 0) | (pf.italic ? Font.ITALIC : 0), pf.size);
409 fontCache.put(font, ret);
414 private static class MetricatedFont extends Font {
415 public FontMetrics metrics = null;
416 public MetricatedFont(String name, int size, int style) {
417 super(name, size, style);
418 metrics = Toolkit.getDefaultToolkit().getFontMetrics(this);