e84f497368c16e98095e273d5e9eb99ec6692d7f
[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.Mesh p, int color) { /*p.stroke(this, color);*/ }
251         public void fill(org.ibex.graphics.Mesh 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
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 PixelBuffer getPixelBuffer() { return pb==null?(pb=new AWTPixelBuffer(this)):pb; }
305
306         public void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2) {
307             getGraphics().drawImage(((AWTPixelBuffer)source).i, sx, sy, sx+(dx2-dx), sy+(dy2-dy), dx, dy, dx2, dy2, null);
308         }
309         
310         /** our component's insets */
311         protected Insets insets = new Insets(0, 0, 0, 0);
312         Graphics getGraphics() { if (g==null) g=window==null ? frame.getGraphics() : window.getGraphics(); return g; }
313         public void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
314             if (pb ==null) pb = new AWTPixelBuffer(this);
315             pb.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
316         }
317         //public PixelBuffer getPixelBuffer() { return pb != null ? pb : (pb = new AWTPixelBuffer(this)); } 
318         public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); }
319         public void toBack() { if (window != null) window.toBack(); }
320         public void toFront() { if (window != null) window.toFront(); }
321         public void setLocation() { /*window.setLocation(root.x, root.y); FIXME */ }
322         public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
323         public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
324         public void _setSize(int width, int height) {
325             g = null;
326             window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom));
327         }
328         public void setInvisible(boolean b) { window.setVisible(!b); }
329         protected void _setMinimized(boolean b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
330         protected void _setMaximized(boolean b) {
331             if (!b) {
332                 if (Log.on) Log.info(this, "JDK 1.1 platforms cannot unmaximize windows");
333                 return;
334             }
335             window.setLocation(new Point(0, 0));
336             window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
337             g = null;
338         }
339
340         class InnerFrame extends Frame {
341             public InnerFrame() throws java.lang.UnsupportedOperationException { }
342             public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); }
343             public void update(Graphics gr) {
344                 Rectangle r = gr.getClipBounds();
345                 super.update(gr);
346                 dirtify(r);
347             }
348             public void paint(Graphics gr) {
349                 // Mac OS X Jdk1.4 Bug: after a componentResized(), you must wait for the paint() before you redraw
350                 Rectangle r = gr.getClipBounds();
351                 super.paint(gr);
352                 dirtify(r);
353             }
354             private void dirtify(java.awt.Rectangle r) {
355                 if (r != null) {
356                     Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
357                 } else {
358                     Dirty(0, 0,
359                           Math.max(getWidth() - insets.left - insets.right, root.width),
360                           Math.min(getHeight() - insets.top - insets.bottom, root.height));
361                 }
362                 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
363                 int newwidth = Math.max(getWidth() - insets.left - insets.right, root.width);
364                 int newheight = Math.max(getHeight() - insets.top - insets.bottom, root.height);
365                 if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
366             }
367         }
368
369         class InnerWindow extends Window {
370             public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
371             public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); }
372             public void update(Graphics gr) { paint(gr); }
373             public void paint(Graphics gr) {
374                 g = null;
375                 Rectangle r = gr.getClipBounds();
376                 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
377             }
378         }
379
380         private int oldfill = 0x0;
381         public void render() {
382             // useful optimizatin;
383             /*
384             if (oldfill != root.fillcolor) {
385                 oldfill = root.fillcolor;
386                 window.setBackground((root.fillcolor & 0xFF000000) == 0 ?
387                                      java.awt.Color.white :
388                                      new java.awt.Color((root.fillcolor >> 16) & 0xff,
389                                                (root.fillcolor >> 8) & 0xff,
390                                                (root.fillcolor) & 0xff));
391             }
392             */
393             super.render();
394         }
395
396         Insets getInsets() {
397             Insets ret = window.getInsets();
398             if (System.getProperty("os.name", "").equals("Mac OS X")) ret.bottom = -1 * ret.top;
399             return ret;
400         }
401
402         AWTSurface(Box root, boolean framed) {
403             super(root);
404             try {
405                 if (framed) window = frame = new InnerFrame();
406                 else window = new InnerWindow();
407
408             // this is here to catch HeadlessException on jdk1.4
409             } catch (java.lang.UnsupportedOperationException e) {
410                 if (Log.on) Log.info(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
411                 if (Log.on) Log.info(this, e);
412             }
413
414             insets = getInsets();
415             
416             window.addMouseListener(this);
417             window.addKeyListener(this);
418             window.addComponentListener(this);
419             window.addMouseMotionListener(this);
420             window.addWindowListener(this);
421
422             // IMPORTANT: this must be called before render() to ensure
423             // that our peer has been created
424             makeVisible();
425         }
426
427         protected void makeVisible() { window.setVisible(true); }
428         
429         public void _dispose() {
430             window.removeMouseListener(this);
431
432             // removed to work around a jdk1.3 bug
433             /* window.removeKeyListener(this); */
434
435             window.removeComponentListener(this);
436             window.removeMouseMotionListener(this);
437             window.removeWindowListener(this);
438             window.dispose();
439         }
440
441         public void syncCursor() {
442             if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
443             else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
444             else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
445             else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
446             else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
447             else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
448             else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
449             else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
450             else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
451             else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
452             else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
453             else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
454             else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
455             else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
456         }
457         
458         // AWT Message translation ////////////////////////////////////////////////////////////////
459         
460         // these functions are all executed in the AWT thread, not the
461         // MessageQueue thread. As a result, they must be *extremely*
462         // careful about invoking methods on instances of Box. Currently,
463         // they should only enqueue messages, use Box.whoIs()
464         // (unsynchronized but thought to be safe), and modify members of
465         // Surface.
466         
467         public void componentHidden(ComponentEvent e) { }
468         public void componentShown(ComponentEvent e) { }
469         public void windowOpened(WindowEvent e) { }
470         public void windowClosed(WindowEvent e) { }
471         public void windowClosing(WindowEvent e) { Close(); }
472         public void windowIconified(WindowEvent e) { Minimized(true); }
473         public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.width, root.height); Minimized(false); }
474         public void windowActivated(WindowEvent e) { Focused(true); }
475         public void windowDeactivated(WindowEvent e) { Focused(false); }
476         public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
477
478         public void componentResized(ComponentEvent e) {
479             // we have to periodically do this; I don't know why
480             insets = getInsets();
481             componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
482         }
483
484         public void componentResized(int newwidth, int newheight) {
485             SizeChange(newwidth, newheight);
486             if (newwidth > root.width) Dirty(root.width, 0, newwidth-root.width, newheight);
487             if (newheight > root.height) Dirty(0, root.height, newwidth, newheight-root.height);
488             Refresh();
489         }
490
491         public void keyTyped(KeyEvent k) { }
492         public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
493         public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
494         public void mouseExited(MouseEvent m) { mouseMoved(m); }
495         public void mouseEntered(MouseEvent m) { mouseMoved(m); }
496         public void mouseDragged(MouseEvent m) { mouseMoved(m); }
497         public void mouseMoved(MouseEvent m) {
498
499             // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
500             int newwidth = window.getWidth() - insets.left - insets.right;
501             int newheight = window.getHeight() - insets.top - insets.bottom;
502             if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
503             
504             Move(m.getX() - insets.left, m.getY() - insets.top);
505         }
506         public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
507         public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
508         public void mouseClicked(MouseEvent m) {
509             if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
510             else Click(modifiersToButtonNumber(m.getModifiers()));
511         }
512         
513         String translateKey(KeyEvent k) {
514             switch (k.getKeyCode()) {
515             case KeyEvent.VK_ALT: return "alt";
516             case KeyEvent.VK_BACK_SPACE: return "back_space";
517             case KeyEvent.VK_CONTROL: return "control";
518             case KeyEvent.VK_DELETE: return "delete";
519             case KeyEvent.VK_DOWN: return "down";
520             case KeyEvent.VK_END: return "end";
521             case KeyEvent.VK_ENTER: return "enter";
522             case KeyEvent.VK_ESCAPE: return "escape";
523             case KeyEvent.VK_F1: return "f1";
524             case KeyEvent.VK_F10: return "f10";
525             case KeyEvent.VK_F11: return "f11";
526             case KeyEvent.VK_F12: return "f12";
527             case KeyEvent.VK_F2: return "f2";
528             case KeyEvent.VK_F3: return "f3";
529             case KeyEvent.VK_F4: return "f4";
530             case KeyEvent.VK_F5: return "f5";
531             case KeyEvent.VK_F6: return "f6"; 
532             case KeyEvent.VK_F7: return "f7";
533             case KeyEvent.VK_F8: return "f8";
534             case KeyEvent.VK_F9: return "f9";
535             case KeyEvent.VK_HOME: return "home";
536             case KeyEvent.VK_INSERT: return "insert";
537             case KeyEvent.VK_LEFT: return "left";
538             case KeyEvent.VK_META: return "alt";
539             case KeyEvent.VK_PAGE_DOWN: return "page_down";
540             case KeyEvent.VK_PAGE_UP: return "page_up";
541             case KeyEvent.VK_PAUSE: return "pause";
542             case KeyEvent.VK_PRINTSCREEN: return "printscreen";
543             case KeyEvent.VK_RIGHT: return "right";
544             case KeyEvent.VK_SHIFT: return "shift";
545             case KeyEvent.VK_TAB: return "tab";
546             case KeyEvent.VK_UP: return "up";
547             default:
548                 char c = k.getKeyChar();
549                 if (c >= 1 && c <= 26) c = (char)('a' + c - 1);
550                 return String.valueOf(c);
551             }
552         }
553     }
554
555     protected void _decodeJPEG(InputStream is, Picture p) {
556         try {
557             Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is));
558             MediaTracker mediatracker = new MediaTracker(new Canvas());
559             mediatracker.addImage(i, 1);
560             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
561             mediatracker.removeImage(i);
562             final int width = i.getWidth(null);
563             final int height = i.getHeight(null);
564             final int[] data = new int[width * height];
565             PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width);
566             pg.grabPixels();
567             if ((pg.getStatus() & ImageObserver.ABORT) != 0)
568                 Log.info(this, "PixelGrabber reported an error while decoding JPEG image");
569             p.width = width;
570             p.height = height;
571             p.data = data;
572         } catch (Exception e) {
573             Log.info(this, "Exception caught while decoding JPEG image");
574             Log.info(this, e);
575         }
576     }
577 }