1
2 package org.xwt.plat;
3
4 import org.xwt.*;
5 import org.xwt.util.*;
6 import java.net.*;
7 import java.io.*;
8 import org.xwt.js.*;
9 import java.util.*;
10 import java.awt.*;
11 import java.awt.datatransfer.*;
12 import java.awt.image.*;
13 import java.awt.event.*;
14
15
16 public class AWT extends JVM {
17
18 protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; }
19 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new AWTPixelBuffer(w, h); }
20 protected Picture _createPicture(JS r) { return new AWTPicture(r); }
21 protected int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; }
22 protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; }
23 protected Surface _createSurface(Box b, boolean framed) { return new AWTSurface(b, framed); }
24
25 protected void postInit() {
26 if (Log.on) Log.diag(Platform.class, " color depth = " +
27 Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp");
28 }
29
30 protected void _criticalAbort(String message) {
31 if (Log.on) Log.info(this, message);
32 final Dialog d = new Dialog(new Frame(), "XWT Cannot Continue");
33 d.setLayout(new BorderLayout());
34 TextArea ta = new TextArea("XWT cannot continue because:\n\n" + message, 10, 80);
35 ta.setEditable(false);
36 d.add(ta, "Center");
37 Button b = new Button("OK");
38 b.addActionListener(new ActionListener() {
39 public void actionPerformed(ActionEvent e) {
40 d.dispose();
41 }
42 });
43 d.add(b, "South");
44 d.setModal(true);
45 d.pack();
46 d.show();
47 new Semaphore().block();
48 }
49
50 protected String _getClipBoard() {
51 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
52 if (cb == null) return null;
53 Transferable clipdata = cb.getContents(null);
54 try { return (String)clipdata.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex) { return null; }
55 }
56
57 protected void _setClipBoard(String s) {
58 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
59 if (clipboard == null) return;
60 StringSelection clipString = new StringSelection(s);
61 clipboard.setContents(clipString, clipString);
62 }
63
64
65 protected static int modifiersToButtonNumber(int modifiers) {
66 if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1;
67 if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
68
69 if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 2;
70 return 3;
71 }
72 if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
73
74 if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 3;
75 return 2;
76 }
77 return 0;
78 }
79
80 static class FileDialogHelper extends FileDialog implements WindowListener, ComponentListener {
81 Semaphore s;
82 public FileDialogHelper(String suggestedFileName, Semaphore s, boolean write) {
83 super(new Frame(), write ? "Save" : "Open", write ? FileDialog.SAVE : FileDialog.LOAD);
84 this.s = s;
85 addWindowListener(this);
86 addComponentListener(this);
87 if (suggestedFileName.indexOf(File.separatorChar) == -1) {
88 setFile(suggestedFileName);
89 } else {
90 setDirectory(suggestedFileName.substring(0, suggestedFileName.lastIndexOf(File.separatorChar)));
91 setFile(suggestedFileName.substring(suggestedFileName.lastIndexOf(File.separatorChar) + 1));
92 }
93 show();
94 }
95 public void windowActivated(WindowEvent e) { }
96 public void windowClosed(WindowEvent e) { s.release(); }
97 public void windowClosing(WindowEvent e) { }
98 public void windowDeactivated(WindowEvent e) { }
99 public void windowDeiconified(WindowEvent e) { }
100 public void windowIconified(WindowEvent e) { }
101 public void windowOpened(WindowEvent e) { }
102 public void componentHidden(ComponentEvent e) { s.release(); }
103 public void componentMoved(ComponentEvent e) { }
104 public void componentResized(ComponentEvent e) { }
105 public void componentShown(ComponentEvent e) { }
106 };
107
108 protected String _fileDialog(String suggestedFileName, boolean write) {
109 final Semaphore s = new Semaphore();
110 FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write);
111 s.block();
112 return fd.getDirectory() == null ? null : (fd.getDirectory() + File.separatorChar + fd.getFile());
113 }
114
115
116
117
118 protected org.xwt.Font.Glyph _createGlyph(org.xwt.Font f, char c) { return new AWTGlyph(f, c); }
119 protected static class AWTGlyph extends org.xwt.Font.Glyph {
120 private Image i = null;
121 private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
122
123
124
132
133 public AWTGlyph(org.xwt.Font f, char c) { super(f, c); }
134 Image getImage() {
135 if (i == null && isLoaded) {
136
137 int[] data2 = new int[data.length];
138 for(int i=0; i<data2.length; i++) data2[i] = ((data[i]) & 0xff) << 24;
139
140 MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data2, 0, width);
141 mis.setAnimated(true);
142 i = Toolkit.getDefaultToolkit().createImage(mis);
143 MediaTracker mediatracker = new MediaTracker(new Canvas());
144 mediatracker.addImage(i, 1);
145 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
146 mediatracker.removeImage(i);
147 synchronized(AWTPixelBuffer.class) {
148 if (AWTPixelBuffer.component == null) {
149 AWTPixelBuffer.component = new Frame();
150 AWTPixelBuffer.component.setVisible(false);
151 AWTPixelBuffer.component.addNotify();
152 }
153 }
154 data = null;
155 }
156 return i;
157 }
158 }
159
160 protected static class AWTPicture extends Picture {
161 public Image i = null;
162 private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
163
164 boolean initialized = false;
165 public AWTPicture(JS r) { super(r); }
166 public void init() {
167 if (initialized) return;
168 initialized = true;
169 MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data, 0, width);
170 mis.setAnimated(true);
171 i = Toolkit.getDefaultToolkit().createImage(mis);
172 MediaTracker mediatracker = new MediaTracker(new Canvas());
173 mediatracker.addImage(i, 1);
174 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
175 mediatracker.removeImage(i);
176 synchronized(AWTPixelBuffer.class) {
177 if (AWTPixelBuffer.component == null) {
178 AWTPixelBuffer.component = new Frame();
179 AWTPixelBuffer.component.setVisible(false);
180 AWTPixelBuffer.component.addNotify();
181 }
182 }
183 }
184 }
185
186 protected static class AWTPixelBuffer extends PixelBuffer {
187
188 protected Image i = null;
189 protected Graphics g = null;
190
191
192 static Component component = null;
193
194 protected AWTPixelBuffer() { }
195 public AWTPixelBuffer(int w, int h) {
196 synchronized(AWTPixelBuffer.class) {
197 if (component == null) {
198 component = new Frame();
199 component.setVisible(false);
200 component.addNotify();
201 }
202 }
203 i = component.createImage(w, h);
204 g = i.getGraphics();
205 }
206
207 public int getHeight() { return i == null ? 0 : i.getHeight(null); }
208 public int getWidth() { return i == null ? 0 : i.getWidth(null); }
209
210 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
211 ((AWTPicture)source).init();
212 g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
213 g.drawImage(((AWTPicture)source).i, dx, dy, null);
214 g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
215 }
216
217
218 public void drawGlyph(org.xwt.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
219
220
221 g.setXORMode(new Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
222 g.setColor(new Color(0x0, 0x0, 0x0));
223 g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
224
225
226 g.setPaintMode();
227 g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
228 g.drawImage(((AWTGlyph)source).getImage(), dx, dy, null);
229 g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
230
231
232 g.setXORMode(new Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
233 g.setColor(new Color(0x0, 0x0, 0x0));
234 g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
235
236
237 g.setPaintMode();
238 }
239
240
241 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
242 g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
243 if (x1 == x3 && x2 == x4) {
244 g.fillRect(x1, y1, x4 - x1, y2 - y1);
245 } else for(int y=y1; y<y2; y++) {
246 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
247 int _y1 = (int)Math.floor(y);
248 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
249 int _y2 = (int)Math.floor(y) + 1;
250 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
251 g.fillRect(_x1, _y1, _x2 - _x1, _y2 - _y1);
252 }
253 }
254 }
255
256
257 protected static class AWTSurface extends Surface.DoubleBufferedSurface
258 implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
259
260 public void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
261 insets = (frame == null ? window : frame).getInsets();
262 window.getGraphics().drawImage(((AWTPixelBuffer)s).i,
263 dx + insets.left,
264 dy + insets.top,
265 dx2 + insets.left,
266 dy2 + insets.top,
267 sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
268 }
269
270
271 Frame frame = null;
272 Window window = null;
273
274
275 protected Insets insets = new Insets(0, 0, 0, 0);
276
277
278 Dimension singleSize = new Dimension();
279
280 public void toBack() { if (window != null) window.toBack(); }
281 public void toFront() { if (window != null) window.toFront(); }
282 public void setLocation() { window.setLocation(root.x, root.y); }
283 public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
284 public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
285 public void _setSize(int width, int height) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
286 public void setInvisible(boolean b) { window.setVisible(!b); }
287 protected void _setMinimized(boolean b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
288 protected void _setMaximized(boolean b) {
289 if (!b) {
290 if (Log.on) Log.info(this, "JDK 1.1 platforms cannot unmaximize windows");
291 return;
292 }
293 window.setLocation(new Point(0, 0));
294 window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
295 }
296
297 class InnerFrame extends Frame {
298 public InnerFrame() throws java.lang.UnsupportedOperationException { }
299 public Dimension getMinimumSize() {
300 return new Dimension(root == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); }
301 public void update(Graphics gr) { paint(gr); }
302 public void paint(Graphics gr) {
303 Rectangle r = gr.getClipBounds();
304
305
306 int newwidth = Math.max(r.x - insets.left + r.width, root.width);
307 int newheight = Math.max(r.y - insets.top + r.height, root.height);
308 if (newwidth > root.width || newheight > root.height)
309 componentResized(window.getWidth() - insets.left - insets.right,
310 window.getHeight() - insets.top - insets.bottom);
311
312 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
313 }
314 }
315
316 class InnerWindow extends Window {
317 public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
318 public Dimension getMinimumSize() {
319 return new Dimension(root == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); }
320 public void update(Graphics gr) { paint(gr); }
321 public void paint(Graphics gr) {
322 Rectangle r = gr.getClipBounds();
323 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
324 }
325 }
326
327 public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); }
328
329 public void render() {
330
331 window.setBackground((root.fillcolor & 0xFF000000) == 0 ?
332 Color.white :
333 new Color((root.fillcolor >> 16) & 0xff,
334 (root.fillcolor >> 8) & 0xff,
335 (root.fillcolor) & 0xff));
336 super.render();
337 }
338
339 AWTSurface(Box root, boolean framed) {
340 super(root);
341 try {
342 if (framed) window = frame = new InnerFrame();
343 else window = new InnerWindow();
344
345
346 } catch (java.lang.UnsupportedOperationException e) {
347 if (Log.on) Log.info(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
348 if (Log.on) Log.info(this, e);
349 }
350
351 insets = window.getInsets();
352
353 window.addMouseListener(this);
354 window.addKeyListener(this);
355 window.addComponentListener(this);
356 window.addMouseMotionListener(this);
357 window.addWindowListener(this);
358
359
360
361 makeVisible();
362 }
363
364 protected void makeVisible() { window.setVisible(true); }
365
366 public void _dispose() {
367 window.removeMouseListener(this);
368
369
370
371
372 window.removeComponentListener(this);
373 window.removeMouseMotionListener(this);
374 window.removeWindowListener(this);
375 window.dispose();
376 }
377
378 public void syncCursor() {
379 if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
380 else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
381 else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
382 else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
383 else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
384 else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
385 else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
386 else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
387 else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
388 else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
389 else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
390 else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
391 else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
392 else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
393 }
394
395
396
397
398
399
400
401
402
403
404 public void componentHidden(ComponentEvent e) { }
405 public void componentShown(ComponentEvent e) { }
406 public void windowOpened(WindowEvent e) { }
407 public void windowClosed(WindowEvent e) { }
408 public void windowClosing(WindowEvent e) { Close(); }
409 public void windowIconified(WindowEvent e) { Minimized(true); }
410 public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.width, root.height); Minimized(false); }
411 public void windowActivated(WindowEvent e) { Focused(true); }
412 public void windowDeactivated(WindowEvent e) { Focused(false); }
413 public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
414
415 public void componentResized(ComponentEvent e) {
416
417 insets = window.getInsets();
418 componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
419 }
420
421 public void componentResized(int newwidth, int newheight) { SizeChange(newwidth, newheight); }
422
423 public void keyTyped(KeyEvent k) { }
424 public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
425 public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
426 public void mouseExited(MouseEvent m) { mouseMoved(m); }
427 public void mouseEntered(MouseEvent m) { mouseMoved(m); }
428 public void mouseDragged(MouseEvent m) { mouseMoved(m); }
429 public void mouseMoved(MouseEvent m) {
430
431
432 int newwidth = window.getWidth() - insets.left - insets.right;
433 int newheight = window.getHeight() - insets.top - insets.bottom;
434 if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
435
436 Move(m.getX() - insets.left, m.getY() - insets.top);
437 }
438 public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
439 public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
440 public void mouseClicked(MouseEvent m) {
441 if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
442 else Click(modifiersToButtonNumber(m.getModifiers()));
443 }
444
445 String translateKey(KeyEvent k) {
446 switch (k.getKeyCode()) {
447 case KeyEvent.VK_ALT: return "alt";
448 case KeyEvent.VK_BACK_SPACE: return "back_space";
449 case KeyEvent.VK_CONTROL: return "control";
450 case KeyEvent.VK_DELETE: return "delete";
451 case KeyEvent.VK_DOWN: return "down";
452 case KeyEvent.VK_END: return "end";
453 case KeyEvent.VK_ENTER: return "enter";
454 case KeyEvent.VK_ESCAPE: return "escape";
455 case KeyEvent.VK_F1: return "f1";
456 case KeyEvent.VK_F10: return "f10";
457 case KeyEvent.VK_F11: return "f11";
458 case KeyEvent.VK_F12: return "f12";
459 case KeyEvent.VK_F2: return "f2";
460 case KeyEvent.VK_F3: return "f3";
461 case KeyEvent.VK_F4: return "f4";
462 case KeyEvent.VK_F5: return "f5";
463 case KeyEvent.VK_F6: return "f6";
464 case KeyEvent.VK_F7: return "f7";
465 case KeyEvent.VK_F8: return "f8";
466 case KeyEvent.VK_F9: return "f9";
467 case KeyEvent.VK_HOME: return "home";
468 case KeyEvent.VK_INSERT: return "insert";
469 case KeyEvent.VK_LEFT: return "left";
470 case KeyEvent.VK_META: return "alt";
471 case KeyEvent.VK_PAGE_DOWN: return "page_down";
472 case KeyEvent.VK_PAGE_UP: return "page_up";
473 case KeyEvent.VK_PAUSE: return "pause";
474 case KeyEvent.VK_PRINTSCREEN: return "printscreen";
475 case KeyEvent.VK_RIGHT: return "right";
476 case KeyEvent.VK_SHIFT: return "shift";
477 case KeyEvent.VK_TAB: return "tab";
478 case KeyEvent.VK_UP: return "up";
479 default:
480 char c = k.getKeyChar();
481 if (c >= 1 && c <= 26) c = (char)('a' + c - 1);
482 return String.valueOf(c);
483 }
484 }
485 }
486
487 protected void _decodeJPEG(InputStream is, Picture p) {
488 try {
489 Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is));
490 MediaTracker mediatracker = new MediaTracker(new Canvas());
491 mediatracker.addImage(i, 1);
492 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
493 mediatracker.removeImage(i);
494 final int width = i.getWidth(null);
495 final int height = i.getHeight(null);
496 final int[] data = new int[width * height];
497 PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width);
498 pg.grabPixels();
499 if ((pg.getStatus() & ImageObserver.ABORT) != 0)
500 Log.info(this, "PixelGrabber reported an error while decoding JPEG image");
501 p.width = width;
502 p.height = height;
503 p.data = data;
504 } catch (Exception e) {
505 Log.info(this, "Exception caught while decoding JPEG image");
506 Log.info(this, e);
507 }
508 }
509 }
510