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();
15 public String getClassName() { return "XWT"; }
16 private XWT() { setSeal(true); }
18 public Object get(String name, Scriptable start) {
19 if (name == null) return null;
20 else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
21 else if (name.equals("parseFloat")) return JSObject.defaultObjects.get("parseFloat", null);
22 else if (name.equals("parseInt")) return JSObject.defaultObjects.get("parseInt", null);
23 else if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
24 else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
25 else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
26 else if (name.equals("date")) return date;
27 else if (name.equals("listfonts")) return listfonts;
28 else if (name.equals("regexp")) return regexp;
29 else if (name.equals("sleep")) return sleep;
30 else if (name.equals("yield")) return yield;
31 else if (name.equals("newBrowserWindow")) return newBrowserWindow;
32 else if (name.equals("textwidth")) return textwidth;
33 else if (name.equals("textheight")) return textheight;
34 else if (name.equals("newBox")) return newBox;
35 else if (name.equals("soap")) return soap;
36 else if (name.equals("xmlrpc")) return xmlrpc;
37 else if (name.equals("clipboard")) return Platform.getClipBoard();
38 else if (name.equals("altKeyName")) return Platform.altKeyName();
39 else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
40 else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
41 else if (name.equals("static")) return Static.getStatic("");
42 else if (name.equals("theme")) return theme;
43 else if (name.equals("button")) {
44 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
45 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
46 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
47 else return new Integer(0);
49 else if (name.equals("println")) return println;
50 else if (name.equals("math")) return org.xwt.util.JSObject.defaultObjects.get("Math", null);
51 else return super.get(name, start);
54 public void put(String name, Scriptable start, Object value) {
55 if (name == null) return;
56 else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
57 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
58 else super.put(name, start, value);
62 // JSFunction Instances ///////////////////////////////////////////////////////////////////
64 /** Helper class for defining functions. */
65 private static abstract class JSFunction extends JSObject implements Function {
66 JSFunction() { setSeal(true); }
67 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
70 private static final JSFunction newBrowserWindow = new JSFunction() {
71 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
72 if (args.length != 1 || args[0] == null) return null;
73 Platform.newBrowserWindow(args[0].toString());
78 private static final JSFunction yield = new JSFunction() {
79 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
80 sleep.call(cx, null, null, null);
85 private static final JSFunction println = new JSFunction() {
86 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
89 Log.log(cx.interpreterSourceFile, args[0] == null ? "null" : args[0].toString());
94 private static final JSFunction date = new JSFunction() {
95 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
96 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
97 } catch (Exception e) {
98 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
99 if (Log.on) Log.log(this, e);
105 private static final JSFunction regexp = new JSFunction() {
106 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
107 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
108 } catch (Exception e) {
109 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
110 if (Log.on) Log.log(this, e);
116 private static final JSFunction listfonts = new JSFunction() {
117 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
118 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
122 private static final JSFunction theme = new JSFunction() {
123 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
124 if (args.length != 2) return null;
125 if (args[0] == null || args[1] == null) return null;
126 Template.retheme(args[0].toString(), args[1].toString());
131 private static final JSFunction xmlrpc = new JSFunction() {
132 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
133 if (args.length != 1 || args[0] == null) return null;
134 return new XMLRPC(args[0].toString(), "");
138 private static final JSFunction soap = new JSFunction() {
139 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
140 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
141 else if (args.length == 2 && args[0] != null && args[1] != null)
142 return new SOAP(args[0].toString(), "", args[1].toString(), null);
143 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
144 return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
149 private static final JSFunction textwidth = new JSFunction() {
150 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
151 if (args.length < 1 || args.length > 2) return null;
152 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
153 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
154 String text = args.length == 1 ? args[0].toString() : args[1].toString();
155 XWF xwf = XWF.getXWF(font);
156 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
157 else return new Integer(xwf.stringWidth(text));
162 private static final JSFunction textheight = new JSFunction() {
163 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
164 if (args.length > 1) return null;
165 if (args.length == 1 && args[0] == null) return null;
166 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
167 XWF xwf = XWF.getXWF(font);
168 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
169 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
173 private static final JSFunction newBox = new JSFunction() {
174 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
175 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
176 for(int i=1; i<args.length; i++)
177 if (args[i] instanceof Box)
178 ret.put(ret.numChildren(), null, (Box)args[i]);
179 for(int i=1; i<args.length; i++)
180 if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
181 Scriptable s = (Scriptable)args[i];
182 Object[] keys = s.getIds();
184 // FIXME: need to ensure that this is putGlobally(), but still run traps...
185 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
191 private static final JSFunction sleep = new JSFunction() {
192 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
193 if (args != null && (args.length != 1 || args[0] == null)) return null;
194 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
196 Thread thread = Thread.currentThread();
197 if (!(thread instanceof ThreadMessage)) {
198 if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
201 ThreadMessage mythread = (ThreadMessage)thread;
202 mythread.done.release();
204 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
206 MessageQueue.add(mythread);