added missing patches to move to fillTriangle() and Mesh
[org.ibex.core.git] / src / org / ibex / plat / AWT.java
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.
4
5 package org.ibex.plat;
6
7 import org.ibex.util.*;
8 import java.io.*;
9 import org.ibex.js.*;
10 import java.awt.*;
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.*;
17
18 /** Platform subclass for all VM's providing AWT 1.1 functionality */
19 public class AWT extends JVM {
20
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); }
27
28     protected void postInit() {
29         if (Log.on) Log.diag(Platform.class, "               color depth = " +
30                             Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp"); }
31
32     protected void _criticalAbort(String message) {
33         if (Log.on) Log.info(this, message);
34         final Dialog d = new Dialog(new Frame(), "Ibex Cannot Continue");
35         d.setLayout(new BorderLayout());
36         TextArea ta = new TextArea("Ibex cannot continue because:\n\n" + message, 10, 80);
37         ta.setEditable(false);
38         d.add(ta, "Center");
39         Button b = new Button("OK");
40         b.addActionListener(new ActionListener() {
41                 public void actionPerformed(ActionEvent e) {
42                     d.dispose();
43                 }
44             });
45         d.add(b, "South");
46         d.setModal(true);
47         d.pack();
48         d.show();
49         new Semaphore().block();
50     }
51
52     protected String _getClipBoard() {
53         Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
54         if (cb == null) return null;
55         Transferable clipdata = cb.getContents(null);
56         try { return (String)clipdata.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex) { return null; }
57     }
58
59     protected void _setClipBoard(String s) {
60         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
61         if (clipboard == null) return;
62         StringSelection clipString = new StringSelection(s);
63         clipboard.setContents(clipString, clipString);
64     }
65
66     /** some platforms (cough, cough, NetscapeVM) have totally broken modifier masks; they will need to override this */
67     protected static int modifiersToButtonNumber(int modifiers) {
68         if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1;
69         if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
70             // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
71             if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 2;
72             return 3;
73         }
74         if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
75             // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
76             if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 3;
77             return 2;
78         }
79         return 0;
80     }
81
82     static class FileDialogHelper extends FileDialog implements WindowListener, ComponentListener {
83         Semaphore s;
84         public FileDialogHelper(String suggestedFileName, Semaphore s, boolean write) {
85             super(new Frame(), write ? "Save" : "Open", write ? FileDialog.SAVE : FileDialog.LOAD);
86             this.s = s;
87             addWindowListener(this);
88             addComponentListener(this);
89             if (suggestedFileName.indexOf(File.separatorChar) == -1) {
90                 setFile(suggestedFileName);
91             } else {
92                 setDirectory(suggestedFileName.substring(0, suggestedFileName.lastIndexOf(File.separatorChar)));
93                 setFile(suggestedFileName.substring(suggestedFileName.lastIndexOf(File.separatorChar) + 1));
94             }
95             show();
96         }
97         public void windowActivated(WindowEvent e) { }
98         public void windowClosed(WindowEvent e) { s.release(); }
99         public void windowClosing(WindowEvent e) { }
100         public void windowDeactivated(WindowEvent e) { }
101         public void windowDeiconified(WindowEvent e) { }
102         public void windowIconified(WindowEvent e) { }
103         public void windowOpened(WindowEvent e) { }
104         public void componentHidden(ComponentEvent e) { s.release(); }
105         public void componentMoved(ComponentEvent e) { }
106         public void componentResized(ComponentEvent e) { }
107         public void componentShown(ComponentEvent e) { }
108     };
109
110     protected String _fileDialog(String suggestedFileName, boolean write) {
111         final Semaphore s = new Semaphore();
112         FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write);
113         s.block();
114         return fd.getDirectory() == null ? null : (fd.getDirectory() + File.separatorChar + fd.getFile());
115     }
116
117
118     // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
119
120     protected org.ibex.graphics.Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new AWTGlyph(f, c); }
121     protected static class AWTGlyph extends org.ibex.graphics.Font.Glyph {
122         Image i = null;
123         Image i2 = null;
124         private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
125
126         // this doesn't work on Win32 because the JVM is broken
127         /*
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; }
133         };
134         */
135
136         public AWTGlyph(org.ibex.graphics.Font f, char c) { super(f, c); }
137         Image getImage() {
138             if (i == null && isLoaded) {
139
140                 int[] data2 = new int[data.length];
141                 for(int i=0; i<data2.length; i++) data2[i] = ((data[i]) & 0xff) << 24;
142
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();
155                     }
156                 }
157                 data = null;
158             }
159             return i;
160         }
161     }
162
163     protected static class AWTPicture extends Picture {
164         public Image i = null;
165         private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
166         
167         boolean initialized = false;
168         public AWTPicture(JS r) { super(r); }
169         public void init() {
170             if (initialized) return;
171             initialized = true;
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();
184                 }
185             }
186         }
187     }
188     
189     protected static class AWTPixelBuffer implements PixelBuffer {
190         
191         private int[] xpoints = new int[3];
192         private int[] ypoints = new int[3];
193         public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int argb) {
194             xpoints[0] = x1;
195             xpoints[1] = x2;
196             xpoints[2] = x3;
197             ypoints[0] = y1;
198             ypoints[1] = y2;
199             ypoints[2] = y3;
200             Graphics g = getGraphics();
201             g.setColor(new java.awt.Color((argb & 0x00ff0000) >> 16,
202                                           (argb & 0x0000ff00) >> 8,
203                                           argb & 0x000000ff/*,
204                                                              (argb & 0xff000000) >> 24*/));
205             g.fillPolygon(xpoints, ypoints, 3);
206         }
207
208         public void drawPicture(Picture p, Affine a, Mesh h) { throw new Error("drawPicture() not implemented"); }
209         protected Image i = null;
210         protected Graphics g = null;
211         protected AWTSurface surface = null;
212
213         /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
214         static Component component = null;
215
216         public AWTPixelBuffer(Image i) { this.i = i; }
217         public AWTPixelBuffer(AWTSurface s) { this.surface = s; }
218         public AWTPixelBuffer(int w, int h) {
219             synchronized(AWTPixelBuffer.class) {
220                 if (component == null) {
221                     component = new Frame();
222                     component.setVisible(false);
223                     component.addNotify();
224                 }
225             }
226             i = component.createImage(w, h);
227         }
228         Graphics getGraphics() {
229             if (surface != null) return surface.getGraphics();
230             if (g != null) return g;
231             if (i != null) { g = i.getGraphics(); return g; }
232             return null;
233         }
234         
235         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
236             ((AWTPicture)source).init();
237             Graphics g = getGraphics();
238             g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
239             g.drawImage(((AWTPicture)source).i, dx, dy, null);
240             if (i != null) g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
241         }
242
243         public void drawLine(int x1, int y1, int x2, int y2, int rgb) {
244             Graphics g = getGraphics();
245             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
246             g.setColor(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
247             g.drawLine(x1, y1, x2, y2);
248         }
249
250         public void stroke(org.ibex.graphics.Polygon p, int color) { p.stroke(this, color); }
251         public void fill(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { p.fill(this, paint); }
252
253         private static int[] xa = new int[4];
254         private static int[] ya = new int[4];
255         public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
256             Graphics g = getGraphics();
257             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 
258             g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
259             xa[0]=x1;
260             xa[1]=x2;
261             xa[2]=x4;
262             xa[3]=x3;
263             ya[0]=y1;
264             ya[1]=y1;
265             ya[2]=y2;
266             ya[3]=y2;
267             g.fillPolygon(xa, ya, 4);
268         }
269
270         // this doens't seem to work on Windows
271         public void drawGlyph(org.ibex.graphics.Font.Glyph source,Affine a,Mesh h,int rgb,int bg) {
272             //throw new Error("drawGlyph() not implemented");
273             /*
274             Image i = ((AWTGlyph)source).getImage();
275             if (((AWTGlyph)source).i2 == null)
276                 ((AWTGlyph)source).i2 = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB);
277             Image i2 = ((AWTGlyph)source).i2;
278             Graphics g2 = i2.getGraphics();
279             g2.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
280             g2.fillRect(0, 0, i2.getWidth(null), i2.getHeight(null));
281             g2.drawImage(i, 0, 0, null);
282             Graphics g = getGraphics();
283             g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
284             g.drawImage(i2, dx, dy, i2.getWidth(null), i2.getHeight(null), null);
285             g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
286             g.fillRect(cx1, cy1, dx - cx1,  cy2 - cy1);
287             g.fillRect(cx1, cy1, cx2 - cx1, dy - cy1);
288             g.fillRect(dx+i2.getWidth(null), cy1, cx2 - (dx+i2.getWidth(null)), cy2 - cy1);
289             g.fillRect(cx1, dy+i2.getHeight(null), cx2 - cx1, cy2 - (dy+i2.getHeight(null)));
290             g.setClip(0, 0, 1000, 1000);
291             */
292         }
293     }
294     
295     
296     protected static class AWTSurface extends Surface.DoubleBufferedSurface
297         implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
298
299         protected AWTPixelBuffer pb = null;
300         private Graphics g = null;
301         Frame frame = null;
302         Window window = null;
303
304         public void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2) {
305             getGraphics().drawImage(((AWTPixelBuffer)source).i, sx, sy, sx+(dx2-dx), sy+(dy2-dy), dx, dy, dx2, dy2, null);
306         }
307         
308         /** our component's insets */
309         protected Insets insets = new Insets(0, 0, 0, 0);
310         Graphics getGraphics() { if (g==null) g=window==null ? frame.getGraphics() : window.getGraphics(); return g; }
311         public void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
312             if (pb ==null) pb = new AWTPixelBuffer(this);
313             pb.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
314         }
315         //public PixelBuffer getPixelBuffer() { return pb != null ? pb : (pb = new AWTPixelBuffer(this)); } 
316         public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); }
317         public void toBack() { if (window != null) window.toBack(); }
318         public void toFront() { if (window != null) window.toFront(); }
319         public void setLocation() { /*window.setLocation(root.x, root.y); FIXME */ }
320         public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
321         public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
322         public void _setSize(int width, int height) {
323             g = null;
324             window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom));
325         }
326         public void setInvisible(boolean b) { window.setVisible(!b); }
327         protected void _setMinimized(boolean b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
328         protected void _setMaximized(boolean b) {
329             if (!b) {
330                 if (Log.on) Log.info(this, "JDK 1.1 platforms cannot unmaximize windows");
331                 return;
332             }
333             window.setLocation(new Point(0, 0));
334             window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
335             g = null;
336         }
337
338         class InnerFrame extends Frame {
339             public InnerFrame() throws java.lang.UnsupportedOperationException { }
340             public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); }
341             public void update(Graphics gr) {
342                 Rectangle r = gr.getClipBounds();
343                 super.update(gr);
344                 dirtify(r);
345             }
346             public void paint(Graphics gr) {
347                 // Mac OS X Jdk1.4 Bug: after a componentResized(), you must wait for the paint() before you redraw
348                 Rectangle r = gr.getClipBounds();
349                 super.paint(gr);
350                 dirtify(r);
351             }
352             private void dirtify(java.awt.Rectangle r) {
353                 if (r != null) {
354                     Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
355                 } else {
356                     Dirty(0, 0,
357                           Math.max(getWidth() - insets.left - insets.right, root.width),
358                           Math.min(getHeight() - insets.top - insets.bottom, root.height));
359                 }
360                 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
361                 int newwidth = Math.max(getWidth() - insets.left - insets.right, root.width);
362                 int newheight = Math.max(getHeight() - insets.top - insets.bottom, root.height);
363                 if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
364             }
365         }
366
367         class InnerWindow extends Window {
368             public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
369             public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); }
370             public void update(Graphics gr) { paint(gr); }
371             public void paint(Graphics gr) {
372                 g = null;
373                 Rectangle r = gr.getClipBounds();
374                 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
375             }
376         }
377
378         private int oldfill = 0x0;
379         public void render() {
380             // useful optimizatin;
381             if (oldfill != root.fillcolor) {
382                 oldfill = root.fillcolor;
383                 window.setBackground((root.fillcolor & 0xFF000000) == 0 ?
384                                      java.awt.Color.white :
385                                      new java.awt.Color((root.fillcolor >> 16) & 0xff,
386                                                (root.fillcolor >> 8) & 0xff,
387                                                (root.fillcolor) & 0xff));
388             }
389             super.render();
390         }
391
392         Insets getInsets() {
393             Insets ret = window.getInsets();
394             if (System.getProperty("os.name", "").equals("Mac OS X")) ret.bottom = -1 * ret.top;
395             return ret;
396         }
397
398         AWTSurface(Box root, boolean framed) {
399             super(root);
400             try {
401                 if (framed) window = frame = new InnerFrame();
402                 else window = new InnerWindow();
403
404             // this is here to catch HeadlessException on jdk1.4
405             } catch (java.lang.UnsupportedOperationException e) {
406                 if (Log.on) Log.info(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
407                 if (Log.on) Log.info(this, e);
408             }
409
410             insets = getInsets();
411             
412             window.addMouseListener(this);
413             window.addKeyListener(this);
414             window.addComponentListener(this);
415             window.addMouseMotionListener(this);
416             window.addWindowListener(this);
417
418             // IMPORTANT: this must be called before render() to ensure
419             // that our peer has been created
420             makeVisible();
421         }
422
423         protected void makeVisible() { window.setVisible(true); }
424         
425         public void _dispose() {
426             window.removeMouseListener(this);
427
428             // removed to work around a jdk1.3 bug
429             /* window.removeKeyListener(this); */
430
431             window.removeComponentListener(this);
432             window.removeMouseMotionListener(this);
433             window.removeWindowListener(this);
434             window.dispose();
435         }
436
437         public void syncCursor() {
438             if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
439             else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
440             else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
441             else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
442             else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
443             else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
444             else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
445             else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
446             else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
447             else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
448             else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
449             else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
450             else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
451             else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
452         }
453         
454         // AWT Message translation ////////////////////////////////////////////////////////////////
455         
456         // these functions are all executed in the AWT thread, not the
457         // MessageQueue thread. As a result, they must be *extremely*
458         // careful about invoking methods on instances of Box. Currently,
459         // they should only enqueue messages, use Box.whoIs()
460         // (unsynchronized but thought to be safe), and modify members of
461         // Surface.
462         
463         public void componentHidden(ComponentEvent e) { }
464         public void componentShown(ComponentEvent e) { }
465         public void windowOpened(WindowEvent e) { }
466         public void windowClosed(WindowEvent e) { }
467         public void windowClosing(WindowEvent e) { Close(); }
468         public void windowIconified(WindowEvent e) { Minimized(true); }
469         public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.width, root.height); Minimized(false); }
470         public void windowActivated(WindowEvent e) { Focused(true); }
471         public void windowDeactivated(WindowEvent e) { Focused(false); }
472         public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
473
474         public void componentResized(ComponentEvent e) {
475             // we have to periodically do this; I don't know why
476             insets = getInsets();
477             componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
478         }
479
480         public void componentResized(int newwidth, int newheight) {
481             SizeChange(newwidth, newheight);
482             if (newwidth > root.width) Dirty(root.width, 0, newwidth-root.width, newheight);
483             if (newheight > root.height) Dirty(0, root.height, newwidth, newheight-root.height);
484             Refresh();
485         }
486
487         public void keyTyped(KeyEvent k) { }
488         public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
489         public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
490         public void mouseExited(MouseEvent m) { mouseMoved(m); }
491         public void mouseEntered(MouseEvent m) { mouseMoved(m); }
492         public void mouseDragged(MouseEvent m) { mouseMoved(m); }
493         public void mouseMoved(MouseEvent m) {
494
495             // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
496             int newwidth = window.getWidth() - insets.left - insets.right;
497             int newheight = window.getHeight() - insets.top - insets.bottom;
498             if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
499             
500             Move(m.getX() - insets.left, m.getY() - insets.top);
501         }
502         public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
503         public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
504         public void mouseClicked(MouseEvent m) {
505             if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
506             else Click(modifiersToButtonNumber(m.getModifiers()));
507         }
508         
509         String translateKey(KeyEvent k) {
510             switch (k.getKeyCode()) {
511             case KeyEvent.VK_ALT: return "alt";
512             case KeyEvent.VK_BACK_SPACE: return "back_space";
513             case KeyEvent.VK_CONTROL: return "control";
514             case KeyEvent.VK_DELETE: return "delete";
515             case KeyEvent.VK_DOWN: return "down";
516             case KeyEvent.VK_END: return "end";
517             case KeyEvent.VK_ENTER: return "enter";
518             case KeyEvent.VK_ESCAPE: return "escape";
519             case KeyEvent.VK_F1: return "f1";
520             case KeyEvent.VK_F10: return "f10";
521             case KeyEvent.VK_F11: return "f11";
522             case KeyEvent.VK_F12: return "f12";
523             case KeyEvent.VK_F2: return "f2";
524             case KeyEvent.VK_F3: return "f3";
525             case KeyEvent.VK_F4: return "f4";
526             case KeyEvent.VK_F5: return "f5";
527             case KeyEvent.VK_F6: return "f6"; 
528             case KeyEvent.VK_F7: return "f7";
529             case KeyEvent.VK_F8: return "f8";
530             case KeyEvent.VK_F9: return "f9";
531             case KeyEvent.VK_HOME: return "home";
532             case KeyEvent.VK_INSERT: return "insert";
533             case KeyEvent.VK_LEFT: return "left";
534             case KeyEvent.VK_META: return "alt";
535             case KeyEvent.VK_PAGE_DOWN: return "page_down";
536             case KeyEvent.VK_PAGE_UP: return "page_up";
537             case KeyEvent.VK_PAUSE: return "pause";
538             case KeyEvent.VK_PRINTSCREEN: return "printscreen";
539             case KeyEvent.VK_RIGHT: return "right";
540             case KeyEvent.VK_SHIFT: return "shift";
541             case KeyEvent.VK_TAB: return "tab";
542             case KeyEvent.VK_UP: return "up";
543             default:
544                 char c = k.getKeyChar();
545                 if (c >= 1 && c <= 26) c = (char)('a' + c - 1);
546                 return String.valueOf(c);
547             }
548         }
549     }
550
551     protected void _decodeJPEG(InputStream is, Picture p) {
552         try {
553             Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is));
554             MediaTracker mediatracker = new MediaTracker(new Canvas());
555             mediatracker.addImage(i, 1);
556             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
557             mediatracker.removeImage(i);
558             final int width = i.getWidth(null);
559             final int height = i.getHeight(null);
560             final int[] data = new int[width * height];
561             PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width);
562             pg.grabPixels();
563             if ((pg.getStatus() & ImageObserver.ABORT) != 0)
564                 Log.info(this, "PixelGrabber reported an error while decoding JPEG image");
565             p.width = width;
566             p.height = height;
567             p.data = data;
568         } catch (Exception e) {
569             Log.info(this, "Exception caught while decoding JPEG image");
570             Log.info(this, e);
571         }
572     }
573 }