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 component = new Frame();
82 component.setVisible(false);
83 component.addNotify();
86 public AWTDoubleBuffer(int w, int h) {
87 i = component.createImage(w, h);
91 public int getHeight() { return i == null ? 0 : i.getHeight(null); }
92 public int getWidth() { return i == null ? 0 : i.getWidth(null); }
93 public void setClip(int x, int y, int x2, int y2) { g.setClip(x, y, x2 - x, y2 - y); }
95 public void drawPicture(Picture source, int x, int y) {
96 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
99 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
100 g.drawImage(((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
103 public void drawString(String font, String text, int x, int y, int argb) {
104 // FEATURE: use an LRU cache for Color objects
105 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
106 g.setFont(getFont(font));
107 g.drawString(text, x, y + 2);
110 public void fillRect(int x, int y, int x2, int y2, int argb) {
111 // FEATURE: use an LRU cache for Color objects
112 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
113 g.fillRect(x, y, x2 - x, y2 - y);
119 protected static class AWTSurface extends Surface
120 implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
122 public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
123 if (ourGraphics == null) ourGraphics = window.getGraphics();
124 ourGraphics.drawImage(((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
125 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
128 /** if (component instanceof Frame) then frame == window else frame == null */
130 Window window = null;
132 /** our component's insets */
133 protected Insets insets = new Insets(0, 0, 0, 0);
135 /** a Graphics context on <code>window</code> */
136 protected Graphics ourGraphics = null;
138 /** some JDKs let us recycle a single Dimension object when calling getSize() */
139 Dimension singleSize = new Dimension();
141 public void toBack() { if (window != null) window.toBack(); }
142 public void toFront() { if (window != null) window.toFront(); }
143 public void setLocation(int x, int y) { window.setLocation(x, y); }
144 public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
145 public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
146 public void setSize(int width, int height) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
147 public void setInvisible(boolean b) { window.setVisible(!b); }
148 protected void _setMinimized(boolean b) { if (Log.on) Log.log(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
149 protected void _setMaximized(boolean b) {
151 if (Log.on) Log.log(this, "JDK 1.1 platforms cannot unmaximize windows");
154 window.setLocation(new Point(0, 0));
155 window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
158 AWTSurface(Box root, boolean framed) {
161 if (framed) window = frame = new Frame() {
162 public void update(Graphics gr) { paint(gr); }
163 public void paint(Graphics gr) {
164 Rectangle r = gr.getClipBounds();
165 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
167 else window = new Window(new Frame()) {
168 public void update(Graphics gr) { paint(gr); }
169 public void paint(Graphics gr) {
170 Rectangle r = gr.getClipBounds();
171 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
174 window.addMouseListener(this);
175 window.addKeyListener(this);
176 window.addComponentListener(this);
177 window.addMouseMotionListener(this);
178 window.addWindowListener(this);
180 // IMPORTANT: this must be called before render() to ensure
181 // that our peer has been created
182 window.setVisible(true);
184 insets = window.getInsets();
187 public void _dispose() {
188 window.removeMouseListener(this);
190 // removed to work around a jdk1.3 bug
191 /* window.removeKeyListener(this); */
193 window.removeComponentListener(this);
194 window.removeMouseMotionListener(this);
195 window.removeWindowListener(this);
199 public void syncCursor() {
200 if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
201 else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
202 else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
203 else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
204 else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
205 else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
206 else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
207 else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
208 else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
209 else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
210 else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
211 else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
212 else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
213 else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
216 // AWT Message translation ////////////////////////////////////////////////////////////////
218 // these functions are all executed in the AWT thread, not the
219 // MessageQueue thread. As a result, they must be *extremely*
220 // careful about invoking methods on instances of Box. Currently,
221 // they should only enqueue messages, use Box.whoIs()
222 // (unsynchronized but thought to be safe), and modify members of
225 public void componentHidden(ComponentEvent e) { }
226 public void componentShown(ComponentEvent e) { }
227 public void windowOpened(WindowEvent e) { }
228 public void windowClosed(WindowEvent e) { }
229 public void windowClosing(WindowEvent e) { Close(); }
230 public void windowIconified(WindowEvent e) { Minimized(true); }
231 public void windowDeiconified(WindowEvent e) { dirty(0, 0, width, height); Minimized(false); }
232 public void windowActivated(WindowEvent e) { Focused(true); }
233 public void windowDeactivated(WindowEvent e) { Focused(false); }
234 public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x, window.getLocation().y); }
235 public void componentResized(ComponentEvent e) {
236 // we have to periodically do this; I don't know why
237 insets = window.getInsets();
238 SizeChange(window.getWidth(), window.getHeight());
242 public void keyTyped(KeyEvent k) { }
243 public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
244 public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
245 public void mouseExited(MouseEvent m) { mouseMoved(m); }
246 public void mouseEntered(MouseEvent m) { mouseMoved(m); }
247 public void mouseDragged(MouseEvent m) { mouseMoved(m); }
248 public void mouseMoved(MouseEvent m) { Move(m.getX() - insets.left, m.getY() - insets.top); }
249 public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
250 public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
251 public void mouseClicked(MouseEvent m) {
252 if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
253 else Click(modifiersToButtonNumber(m.getModifiers()));
256 String translateKey(KeyEvent k) {
257 switch (k.getKeyCode()) {
258 case KeyEvent.VK_ALT: return "alt";
259 case KeyEvent.VK_BACK_SPACE: return "back_space";
260 case KeyEvent.VK_CONTROL: return "control";
261 case KeyEvent.VK_DELETE: return "delete";
262 case KeyEvent.VK_DOWN: return "down";
263 case KeyEvent.VK_END: return "end";
264 case KeyEvent.VK_ENTER: return "enter";
265 case KeyEvent.VK_ESCAPE: return "escape";
266 case KeyEvent.VK_F1: return "f1";
267 case KeyEvent.VK_F10: return "f10";
268 case KeyEvent.VK_F11: return "f11";
269 case KeyEvent.VK_F12: return "f12";
270 case KeyEvent.VK_F2: return "f2";
271 case KeyEvent.VK_F3: return "f3";
272 case KeyEvent.VK_F4: return "f4";
273 case KeyEvent.VK_F5: return "f5";
274 case KeyEvent.VK_F6: return "f6";
275 case KeyEvent.VK_F7: return "f7";
276 case KeyEvent.VK_F8: return "f8";
277 case KeyEvent.VK_F9: return "f9";
278 case KeyEvent.VK_HOME: return "home";
279 case KeyEvent.VK_INSERT: return "insert";
280 case KeyEvent.VK_LEFT: return "left";
281 case KeyEvent.VK_META: return "alt";
282 case KeyEvent.VK_PAGE_DOWN: return "page_down";
283 case KeyEvent.VK_PAGE_UP: return "page_up";
284 case KeyEvent.VK_PAUSE: return "pause";
285 case KeyEvent.VK_PRINTSCREEN: return "printscreen";
286 case KeyEvent.VK_RIGHT: return "right";
287 case KeyEvent.VK_SHIFT: return "shift";
288 case KeyEvent.VK_TAB: return "tab";
289 case KeyEvent.VK_UP: return "up";
291 // we special-case letters since (C-a).getKeyChar() != 'a'
292 case KeyEvent.VK_A: return "a";
293 case KeyEvent.VK_B: return "b";
294 case KeyEvent.VK_C: return "c";
295 case KeyEvent.VK_D: return "d";
296 case KeyEvent.VK_E: return "e";
297 case KeyEvent.VK_F: return "f";
298 case KeyEvent.VK_G: return "g";
299 case KeyEvent.VK_H: return "h";
300 case KeyEvent.VK_I: return "i";
301 case KeyEvent.VK_J: return "j";
302 case KeyEvent.VK_K: return "k";
303 case KeyEvent.VK_L: return "l";
304 case KeyEvent.VK_M: return "m";
305 case KeyEvent.VK_N: return "n";
306 case KeyEvent.VK_O: return "o";
307 case KeyEvent.VK_P: return "p";
308 case KeyEvent.VK_Q: return "q";
309 case KeyEvent.VK_R: return "r";
310 case KeyEvent.VK_S: return "s";
311 case KeyEvent.VK_T: return "t";
312 case KeyEvent.VK_U: return "u";
313 case KeyEvent.VK_V: return "v";
314 case KeyEvent.VK_W: return "w";
315 case KeyEvent.VK_X: return "x";
316 case KeyEvent.VK_Y: return "y";
317 case KeyEvent.VK_Z: return "z";
318 default: return String.valueOf(k.getKeyChar());
323 // Font Handling Stuff //////////////////////////////////////////////////////////
325 protected String[] _listFonts() { return fontList; }
326 private static String[] fontList;
328 String[] awtfonts = Toolkit.getDefaultToolkit().getFontList();
329 fontList = new String[awtfonts.length * 4];
330 for(int i=0; i<awtfonts.length; i++) {
331 fontList[i * 4] = awtfonts[i] + "*";
332 fontList[i * 4 + 1] = awtfonts[i] + "*b";
333 fontList[i * 4 + 2] = awtfonts[i] + "*i";
334 fontList[i * 4 + 3] = awtfonts[i] + "*bi";
338 private static Hash fontCache = new Hash();
339 private static ParsedFont pf = new ParsedFont();
340 private static MetricatedFont getFont(String font) {
341 MetricatedFont ret = (MetricatedFont)fontCache.get(font);
344 if (pf.name.equals("tty")) pf.name = "monospace";
345 ret = new MetricatedFont(pf.name, (pf.bold ? Font.BOLD : 0) | (pf.italic ? Font.ITALIC : 0), pf.size);
346 fontCache.put(font, ret);
351 private static class MetricatedFont extends Font {
352 public FontMetrics metrics = null;
353 public MetricatedFont(String name, int size, int style) {
354 super(name, size, style);
355 metrics = Toolkit.getDefaultToolkit().getFontMetrics(this);