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("origin")) return Main.origin;
42 else if (name.equals("clipboard")) return Platform.getClipBoard();
43 else if (name.equals("altKeyName")) return Platform.altKeyName();
44 else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
45 else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
46 else if (name.equals("static")) return Static.getStatic("");
47 else if (name.equals("theme")) return theme;
48 else if (name.equals("openFile")) return openFile;
49 else if (name.equals("saveFile")) return saveFile;
50 else if (name.equals("saveFileAs")) return saveFileAs;
51 else if (name.equals("utfEncode")) return utfEncode;
52 else if (name.equals("button")) {
53 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 if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
56 else return new Integer(0);
58 else if (name.equals("println")) return println;
59 else if (name.equals("math")) return org.xwt.util.JSObject.defaultObjects.get("Math", null);
60 else return super.get(name, start);
63 public void put(String name, Scriptable start, Object value) {
64 if (name == null) return;
65 else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
66 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
67 else super.put(name, start, value);
71 // JSFunction Instances ///////////////////////////////////////////////////////////////////
73 /** Helper class for defining functions. */
74 private static abstract class JSFunction extends JSObject implements Function {
75 JSFunction() { setSeal(true); }
76 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
79 private static final JSFunction newBrowserWindow = new JSFunction() {
80 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
81 if (args.length != 1 || args[0] == null) return null;
82 Platform.newBrowserWindow(args[0].toString());
87 private static final JSFunction yield = new JSFunction() {
88 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
89 sleep.call(cx, null, null, null);
94 private static final JSFunction println = new JSFunction() {
95 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
98 Log.log(cx.interpreterSourceFile, args[0] == null ? "null" : args[0].toString());
103 private static final JSFunction date = new JSFunction() {
104 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
105 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
106 } catch (Exception e) {
107 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
108 if (Log.on) Log.log(this, e);
114 private static final JSFunction regexp = new JSFunction() {
115 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
116 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
117 } catch (Exception e) {
118 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
119 if (Log.on) Log.log(this, e);
125 private static final JSFunction listfonts = new JSFunction() {
126 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
127 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
131 private static final JSFunction theme = new JSFunction() {
132 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
133 if (args.length != 2) return null;
134 if (args[0] == null || args[1] == null) return null;
135 Template.retheme(args[0].toString(), args[1].toString());
140 private static final JSFunction xmlrpc = new JSFunction() {
141 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
142 if (args.length != 1 || args[0] == null) return null;
143 return new XMLRPC(args[0].toString(), "");
147 private static final JSFunction soap = new JSFunction() {
148 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
149 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
150 else if (args.length == 2 && args[0] != null && args[1] != null)
151 return new SOAP(args[0].toString(), "", args[1].toString(), null);
152 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
153 return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
158 private static final JSFunction textwidth = new JSFunction() {
159 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
160 if (args.length < 1 || args.length > 2) return null;
161 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
162 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
163 String text = args.length == 1 ? args[0].toString() : args[1].toString();
164 XWF xwf = XWF.getXWF(font);
165 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
166 else return new Integer(xwf.stringWidth(text));
171 private static final JSFunction textheight = new JSFunction() {
172 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
173 if (args.length > 1) return null;
174 if (args.length == 1 && args[0] == null) return null;
175 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
176 XWF xwf = XWF.getXWF(font);
177 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
178 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
182 private static final JSFunction newBox = new JSFunction() {
183 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
184 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
185 for(int i=1; i<args.length; i++)
186 if (args[i] instanceof Box)
187 ret.put(ret.numChildren(), null, (Box)args[i]);
188 for(int i=1; i<args.length; i++)
189 if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
190 Scriptable s = (Scriptable)args[i];
191 Object[] keys = s.getIds();
193 // FIXME: need to ensure that this is putGlobally(), but still run traps...
194 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
200 private static final JSFunction sleep = new JSFunction() {
201 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
202 if (args != null && (args.length != 1 || args[0] == null)) return null;
203 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
205 Thread thread = Thread.currentThread();
206 if (!(thread instanceof ThreadMessage)) {
207 if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
210 ThreadMessage mythread = (ThreadMessage)thread;
211 mythread.done.release();
213 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
215 MessageQueue.add(mythread);
221 private static final JSFunction openFile = new JSFunction() {
222 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
223 if (args.length != 1) return null;
224 String file = Platform.fileDialog(args[0].toString(), false);
225 return file == null ? null : new ByteStream(file);
229 private static final JSFunction saveFile = new JSFunction() {
230 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
231 if (args.length != 2) return null;
232 if (!(args[1] instanceof ByteStream)) return null;
233 String file = args[0].toString();
234 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
235 file = Platform.fileDialog(file, true);
236 if (file == null) return null;
237 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
240 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
242 } catch (IOException e) {
243 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
244 if (Log.on) Log.log(ByteStream.class, e);
245 throw new JavaScriptException("error while writing a ByteStream to a file");
250 private static final JSFunction saveFileAs = new JSFunction() {
251 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
252 if (args.length != 2) return null;
253 if (!(args[1] instanceof ByteStream)) return null;
254 String file = args[0].toString();
255 file = Platform.fileDialog(file, true);
256 if (file == null) return null;
257 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
259 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
261 } catch (IOException e) {
262 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
263 if (Log.on) Log.log(ByteStream.class, e);
264 throw new JavaScriptException("error while writing a ByteStream to a file");
269 private static final JSFunction utfEncode = new JSFunction() {
270 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
271 if (args == null || args.length != 1) return null;
272 return new ByteStream(args[0].toString().getBytes());