1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
9 import org.mozilla.javascript.*;
11 /** Singleton class that provides all functionality in the xwt.* namespace */
12 public final class XWT extends JSObject {
14 public static final XWT singleton = new XWT();
16 /** each key is a string representing a filename which the user has already given XWT permission to write to */
17 private static Hashtable safeFiles = new Hashtable();
19 public String getClassName() { return "XWT"; }
20 private XWT() { setSeal(true); }
22 public Object get(String name, Scriptable start) {
23 if (name == null) return null;
24 else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
25 else if (name.equals("parseFloat")) return JSObject.defaultObjects.get("parseFloat", null);
26 else if (name.equals("parseInt")) return JSObject.defaultObjects.get("parseInt", null);
27 else if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
28 else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
29 else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
30 else if (name.equals("date")) return date;
31 else if (name.equals("listfonts")) return listfonts;
32 else if (name.equals("regexp")) return regexp;
33 else if (name.equals("sleep")) return sleep;
34 else if (name.equals("yield")) return yield;
35 else if (name.equals("newBrowserWindow")) return newBrowserWindow;
36 else if (name.equals("textwidth")) return textwidth;
37 else if (name.equals("textheight")) return textheight;
38 else if (name.equals("newBox")) return newBox;
39 else if (name.equals("soap")) return soap;
40 else if (name.equals("xmlrpc")) return xmlrpc;
41 else if (name.equals("clipboard")) return Platform.getClipBoard();
42 else if (name.equals("altKeyName")) return Platform.altKeyName();
43 else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
44 else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
45 else if (name.equals("static")) return Static.getStatic("");
46 else if (name.equals("theme")) return theme;
47 else if (name.equals("openFile")) return openFile;
48 else if (name.equals("saveFile")) return saveFile;
49 else if (name.equals("saveFileAs")) return saveFileAs;
50 else if (name.equals("utfEncode")) return utfEncode;
51 else if (name.equals("button")) {
52 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
53 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
54 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
55 else return new Integer(0);
57 else if (name.equals("println")) return println;
58 else if (name.equals("math")) return org.xwt.util.JSObject.defaultObjects.get("Math", null);
59 else return super.get(name, start);
62 public void put(String name, Scriptable start, Object value) {
63 if (name == null) return;
64 else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
65 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
66 else super.put(name, start, value);
70 // JSFunction Instances ///////////////////////////////////////////////////////////////////
72 /** Helper class for defining functions. */
73 private static abstract class JSFunction extends JSObject implements Function {
74 JSFunction() { setSeal(true); }
75 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
78 private static final JSFunction newBrowserWindow = new JSFunction() {
79 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
80 if (args.length != 1 || args[0] == null) return null;
81 Platform.newBrowserWindow(args[0].toString());
86 private static final JSFunction yield = new JSFunction() {
87 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
88 sleep.call(cx, null, null, null);
93 private static final JSFunction println = new JSFunction() {
94 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
97 Log.log(cx.interpreterSourceFile, args[0] == null ? "null" : args[0].toString());
102 private static final JSFunction date = new JSFunction() {
103 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
104 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
105 } catch (Exception e) {
106 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
107 if (Log.on) Log.log(this, e);
113 private static final JSFunction regexp = new JSFunction() {
114 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
115 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
116 } catch (Exception e) {
117 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
118 if (Log.on) Log.log(this, e);
124 private static final JSFunction listfonts = new JSFunction() {
125 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
126 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
130 private static final JSFunction theme = new JSFunction() {
131 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
132 if (args.length != 2) return null;
133 if (args[0] == null || args[1] == null) return null;
134 Template.retheme(args[0].toString(), args[1].toString());
139 private static final JSFunction xmlrpc = new JSFunction() {
140 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
141 if (args.length != 1 || args[0] == null) return null;
142 return new XMLRPC(args[0].toString(), "");
146 private static final JSFunction soap = new JSFunction() {
147 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
148 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
149 else if (args.length == 2 && args[0] != null && args[1] != null)
150 return new SOAP(args[0].toString(), "", args[1].toString(), null);
151 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
152 return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
157 private static final JSFunction textwidth = new JSFunction() {
158 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
159 if (args.length < 1 || args.length > 2) return null;
160 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
161 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
162 String text = args.length == 1 ? args[0].toString() : args[1].toString();
163 XWF xwf = XWF.getXWF(font);
164 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
165 else return new Integer(xwf.stringWidth(text));
170 private static final JSFunction textheight = new JSFunction() {
171 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
172 if (args.length > 1) return null;
173 if (args.length == 1 && args[0] == null) return null;
174 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
175 XWF xwf = XWF.getXWF(font);
176 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
177 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
181 private static final JSFunction newBox = new JSFunction() {
182 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
183 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
184 for(int i=1; i<args.length; i++)
185 if (args[i] instanceof Box)
186 ret.put(ret.numChildren(), null, (Box)args[i]);
187 for(int i=1; i<args.length; i++)
188 if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
189 Scriptable s = (Scriptable)args[i];
190 Object[] keys = s.getIds();
192 // FIXME: need to ensure that this is putGlobally(), but still run traps...
193 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
199 private static final JSFunction sleep = new JSFunction() {
200 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
201 if (args != null && (args.length != 1 || args[0] == null)) return null;
202 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
204 Thread thread = Thread.currentThread();
205 if (!(thread instanceof ThreadMessage)) {
206 if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
209 ThreadMessage mythread = (ThreadMessage)thread;
210 mythread.done.release();
212 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
214 MessageQueue.add(mythread);
220 private static final JSFunction openFile = new JSFunction() {
221 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
222 if (args == null || args.length != 1) return null;
223 String file = Platform.fileDialog(args[0].toString(), false);
224 return file == null ? null : new ByteStream(file);
228 private static final JSFunction saveFile = new JSFunction() {
229 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
230 if (args == null || args.length != 2) return null;
231 if (!(args[1] instanceof ByteStream)) return null;
232 String file = args[0].toString();
233 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
234 file = Platform.fileDialog(file, true);
235 if (file == null) return null;
236 System.out.println(">>" + file + "<<");
237 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
240 System.out.println("WRITING TO " + file);
241 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
243 } catch (IOException e) {
244 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
245 if (Log.on) Log.log(ByteStream.class, e);
246 throw new JavaScriptException("error while writing a ByteStream to a file");
251 private static final JSFunction saveFileAs = new JSFunction() {
252 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
253 if (args == null || args.length != 2) return null;
254 if (!(args[1] instanceof ByteStream)) return null;
255 String file = args[0].toString();
256 file = Platform.fileDialog(file, true);
257 if (file == null) return null;
258 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
260 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
262 } catch (IOException e) {
263 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
264 if (Log.on) Log.log(ByteStream.class, e);
265 throw new JavaScriptException("error while writing a ByteStream to a file");
270 private static final JSFunction utfEncode = new JSFunction() {
271 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
272 if (args == null || args.length != 1) return null;
273 return new ByteStream(args[0].toString().getBytes());