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(); }
29 protected String _getClipBoard() {
30 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
31 if (cb == null) return null;
32 Transferable clipdata = cb.getContents(null);
33 try { return (String)clipdata.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex) { return null; }
36 protected void _setClipBoard(String s) {
37 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
38 if (clipboard == null) return;
39 StringSelection clipString = new StringSelection(s);
40 clipboard.setContents(clipString, clipString);
43 /** some platforms (cough, cough, NetscapeVM) have totally broken modifier masks; they will need to override this */
44 protected static int modifiersToButtonNumber(int modifiers) {
45 if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1;
46 if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) return 3;
47 if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) return 2;
51 // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
53 protected static class AWTPicture implements Picture {
54 public int getHeight() { return i.getHeight(null); }
55 public int getWidth() { return i.getWidth(null); }
56 public int[] getData() { return data; }
59 public Image i = null;
60 private static MediaTracker mediatracker = new MediaTracker(new Canvas());
61 private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
63 public AWTPicture(int[] b, int w, int h) {
65 Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, cmodel, b, 0, w));
66 mediatracker.addImage(img, 1);
67 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
68 mediatracker.removeImage(img);
73 protected static class AWTDoubleBuffer implements DoubleBuffer {
75 protected Image i = null;
76 protected Graphics g = null;
78 /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
79 static Component component = null;
81 protected AWTDoubleBuffer() { }
82 public AWTDoubleBuffer(int w, int h) {
83 synchronized(AWTDoubleBuffer.class) {
84 if (component == null) {
85 component = new Frame();
86 component.setVisible(false);
87 component.addNotify();
90 i = component.createImage(w, h);
94 public int getHeight() { return i == null ? 0 : i.getHeight(null); }
95 public int getWidth() { return i == null ? 0 : i.getWidth(null); }
96 public void setClip(int x, int y, int x2, int y2) { g.setClip(x, y, x2 - x, y2 - y); }
98 public void drawPicture(Picture source, int x, int y) {
99 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
102 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
103 g.drawImage(((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
106 public void drawString(String font, String text, int x, int y, int argb) {
107 // FEATURE: use an LRU cache for Color objects
108 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
109 g.setFont(getFont(font));
110 g.drawString(text, x, y + 2);
113 public void fillRect(int x, int y, int x2, int y2, int argb) {
114 // FEATURE: use an LRU cache for Color objects
115 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
116 g.fillRect(x, y, x2 - x, y2 - y);
122 protected static class AWTSurface extends Surface
123 implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
125 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
126 if (ourGraphics == null) ourGraphics = window.getGraphics();
127 ourGraphics.drawImage(((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
128 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
131 /** if (component instanceof Frame) then frame == window else frame == null */
133 Window window = null;
135 /** our component's insets */
136 protected Insets insets = new Insets(0, 0, 0, 0);
138 /** a Graphics context on <code>window</code> */
139 protected Graphics ourGraphics = null;
141 /** some JDKs let us recycle a single Dimension object when calling getSize() */
142 Dimension singleSize = new Dimension();
144 public void toBack() { if (window != null) window.toBack(); }
145 public void toFront() { if (window != null) window.toFront(); }
146 public void setLocation(int x, int y) { window.setLocation(x, y); }
147 public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
148 public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
149 public void setSize(int width, int height) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
150 public void setInvisible(boolean b) { window.setVisible(!b); }
151 protected void _setMinimized(boolean b) { if (Log.on) Log.log(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
152 protected void _setMaximized(boolean b) {
154 if (Log.on) Log.log(this, "JDK 1.1 platforms cannot unmaximize windows");
157 window.setLocation(new Point(0, 0));
158 window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
161 class InnerFrame extends Frame {
162 public InnerFrame() throws java.lang.UnsupportedOperationException { }
163 public void update(Graphics gr) { paint(gr); }
164 public void paint(Graphics gr) {
165 Rectangle r = gr.getClipBounds();
167 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
168 int newwidth = Math.max(r.x - insets.left + r.width, width);
169 int newheight = Math.max(r.y - insets.top + r.height, height);
170 if (newwidth > width || newheight > height)
171 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
173 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
177 class InnerWindow extends Window {
178 public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
179 public void update(Graphics gr) { paint(gr); }
180 public void paint(Graphics gr) {
181 Rectangle r = gr.getClipBounds();
182 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
186 AWTSurface(Box root, boolean framed) {
189 if (framed) window = frame = new InnerFrame();
190 else window = new InnerWindow();
192 // this is here to catch HeadlessException on jdk1.4
193 } catch (java.lang.UnsupportedOperationException e) {
194 if (Log.on) Log.log(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
195 if (Log.on) Log.log(this, e);
198 insets = window.getInsets();
200 window.addMouseListener(this);
201 window.addKeyListener(this);
202 window.addComponentListener(this);
203 window.addMouseMotionListener(this);
204 window.addWindowListener(this);
206 // IMPORTANT: this must be called before render() to ensure
207 // that our peer has been created
208 window.setVisible(true);
212 public void _dispose() {
213 window.removeMouseListener(this);
215 // removed to work around a jdk1.3 bug
216 /* window.removeKeyListener(this); */
218 window.removeComponentListener(this);
219 window.removeMouseMotionListener(this);
220 window.removeWindowListener(this);
224 public void syncCursor() {
225 if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
226 else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
227 else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
228 else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
229 else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
230 else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
231 else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
232 else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
233 else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
234 else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
235 else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
236 else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
237 else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
238 else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
241 // AWT Message translation ////////////////////////////////////////////////////////////////
243 // these functions are all executed in the AWT thread, not the
244 // MessageQueue thread. As a result, they must be *extremely*
245 // careful about invoking methods on instances of Box. Currently,
246 // they should only enqueue messages, use Box.whoIs()
247 // (unsynchronized but thought to be safe), and modify members of
250 public void componentHidden(ComponentEvent e) { }
251 public void componentShown(ComponentEvent e) { }
252 public void windowOpened(WindowEvent e) { }
253 public void windowClosed(WindowEvent e) { }
254 public void windowClosing(WindowEvent e) { Close(); }
255 public void windowIconified(WindowEvent e) { Minimized(true); }
256 public void windowDeiconified(WindowEvent e) { dirty(0, 0, width, height); Minimized(false); }
257 public void windowActivated(WindowEvent e) { Focused(true); }
258 public void windowDeactivated(WindowEvent e) { Focused(false); }
259 public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x, window.getLocation().y); }
261 public void componentResized(ComponentEvent e) {
262 // we have to periodically do this; I don't know why
263 insets = window.getInsets();
264 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
267 public void componentResized(int newwidth, int newheight) {
268 int oldwidth = width;
269 int oldheight = height;
270 SizeChange(newwidth, newheight);
272 // we do this because JVMs which don't clear the background won't force repaints of these areas
273 root.dirty(Math.min(oldwidth, newwidth), 0, Math.abs(oldwidth - newwidth), Math.max(oldheight, newheight));
274 root.dirty(0, Math.min(oldheight, newheight), Math.max(oldwidth, newwidth), Math.abs(oldheight - newheight));
279 public void keyTyped(KeyEvent k) { }
280 public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
281 public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
282 public void mouseExited(MouseEvent m) { mouseMoved(m); }
283 public void mouseEntered(MouseEvent m) { mouseMoved(m); }
284 public void mouseDragged(MouseEvent m) { mouseMoved(m); }
285 public void mouseMoved(MouseEvent m) {
287 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
288 int newwidth = window.getWidth() - insets.left - insets.right;
289 int newheight = window.getHeight() - insets.top - insets.bottom;
290 if (newwidth != width || newheight != height) componentResized(newwidth, newheight);
292 Move(m.getX() - insets.left, m.getY() - insets.top);
294 public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
295 public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
296 public void mouseClicked(MouseEvent m) {
297 if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
298 else Click(modifiersToButtonNumber(m.getModifiers()));
301 String translateKey(KeyEvent k) {
302 switch (k.getKeyCode()) {
303 case KeyEvent.VK_ALT: return "alt";
304 case KeyEvent.VK_BACK_SPACE: return "back_space";
305 case KeyEvent.VK_CONTROL: return "control";
306 case KeyEvent.VK_DELETE: return "delete";
307 case KeyEvent.VK_DOWN: return "down";
308 case KeyEvent.VK_END: return "end";
309 case KeyEvent.VK_ENTER: return "enter";
310 case KeyEvent.VK_ESCAPE: return "escape";
311 case KeyEvent.VK_F1: return "f1";
312 case KeyEvent.VK_F10: return "f10";
313 case KeyEvent.VK_F11: return "f11";
314 case KeyEvent.VK_F12: return "f12";
315 case KeyEvent.VK_F2: return "f2";
316 case KeyEvent.VK_F3: return "f3";
317 case KeyEvent.VK_F4: return "f4";
318 case KeyEvent.VK_F5: return "f5";
319 case KeyEvent.VK_F6: return "f6";
320 case KeyEvent.VK_F7: return "f7";
321 case KeyEvent.VK_F8: return "f8";
322 case KeyEvent.VK_F9: return "f9";
323 case KeyEvent.VK_HOME: return "home";
324 case KeyEvent.VK_INSERT: return "insert";
325 case KeyEvent.VK_LEFT: return "left";
326 case KeyEvent.VK_META: return "alt";
327 case KeyEvent.VK_PAGE_DOWN: return "page_down";
328 case KeyEvent.VK_PAGE_UP: return "page_up";
329 case KeyEvent.VK_PAUSE: return "pause";
330 case KeyEvent.VK_PRINTSCREEN: return "printscreen";
331 case KeyEvent.VK_RIGHT: return "right";
332 case KeyEvent.VK_SHIFT: return "shift";
333 case KeyEvent.VK_TAB: return "tab";
334 case KeyEvent.VK_UP: return "up";
336 // we special-case letters since (C-a).getKeyChar() != 'a'
337 case KeyEvent.VK_A: return "a";
338 case KeyEvent.VK_B: return "b";
339 case KeyEvent.VK_C: return "c";
340 case KeyEvent.VK_D: return "d";
341 case KeyEvent.VK_E: return "e";
342 case KeyEvent.VK_F: return "f";
343 case KeyEvent.VK_G: return "g";
344 case KeyEvent.VK_H: return "h";
345 case KeyEvent.VK_I: return "i";
346 case KeyEvent.VK_J: return "j";
347 case KeyEvent.VK_K: return "k";
348 case KeyEvent.VK_L: return "l";
349 case KeyEvent.VK_M: return "m";
350 case KeyEvent.VK_N: return "n";
351 case KeyEvent.VK_O: return "o";
352 case KeyEvent.VK_P: return "p";
353 case KeyEvent.VK_Q: return "q";
354 case KeyEvent.VK_R: return "r";
355 case KeyEvent.VK_S: return "s";
356 case KeyEvent.VK_T: return "t";
357 case KeyEvent.VK_U: return "u";
358 case KeyEvent.VK_V: return "v";
359 case KeyEvent.VK_W: return "w";
360 case KeyEvent.VK_X: return "x";
361 case KeyEvent.VK_Y: return "y";
362 case KeyEvent.VK_Z: return "z";
363 default: return String.valueOf(k.getKeyChar());
368 // Font Handling Stuff //////////////////////////////////////////////////////////
370 protected String[] _listFonts() { return fontList; }
371 private static String[] fontList;
373 String[] awtfonts = Toolkit.getDefaultToolkit().getFontList();
374 fontList = new String[awtfonts.length * 4];
375 for(int i=0; i<awtfonts.length; i++) {
376 fontList[i * 4] = awtfonts[i] + "*";
377 fontList[i * 4 + 1] = awtfonts[i] + "*b";
378 fontList[i * 4 + 2] = awtfonts[i] + "*i";
379 fontList[i * 4 + 3] = awtfonts[i] + "*bi";
383 private static Hash fontCache = new Hash();
384 private static ParsedFont pf = new ParsedFont();
385 private static MetricatedFont getFont(String font) {
386 MetricatedFont ret = (MetricatedFont)fontCache.get(font);
389 if (pf.name.equals("tty")) pf.name = "monospace";
390 ret = new MetricatedFont(pf.name, (pf.bold ? Font.BOLD : 0) | (pf.italic ? Font.ITALIC : 0), pf.size);
391 fontCache.put(font, ret);
396 private static class MetricatedFont extends Font {
397 public FontMetrics metrics = null;
398 public MetricatedFont(String name, int size, int style) {
399 super(name, size, style);
400 metrics = Toolkit.getDefaultToolkit().getFontMetrics(this);