2003/06/18 07:54:30
[org.ibex.core.git] / src / org / xwt / SpecialBoxProperty.java
index cb64849..56ee209 100644 (file)
@@ -1,11 +1,11 @@
 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
 
-import org.mozilla.javascript.*;
-import org.xwt.util.*;
 import java.util.*;
 import java.net.*;
 import java.text.*;
+import org.xwt.js.*;
+import org.xwt.util.*;
 
 /** 
  *  A helper class for properties of Box which require special
@@ -64,7 +64,6 @@ class SpecialBoxProperty {
     static final int white = 0xFFFFFFFF;
 
     static {
-
         specialBoxProperties.put("color", new SpecialBoxProperty() {
                 public Object get(Box b) {
                     if ((b.color & 0xFF000000) == 0) return null;
@@ -81,10 +80,15 @@ class SpecialBoxProperty {
                     String s = value == null ? null : value.toString();
                     if (value == null) newcolor = 0x00000000;
                     else if (s.length() > 0 && s.charAt(0) == '#')
-                        newcolor = 0xFF000000 |
-                            (Integer.parseInt(s.substring(1, 3), 16) << 16) |
-                            (Integer.parseInt(s.substring(3, 5), 16) << 8) |
-                            Integer.parseInt(s.substring(5, 7), 16);
+                        try {
+                            newcolor = 0xFF000000 |
+                                (Integer.parseInt(s.substring(1, 3), 16) << 16) |
+                                (Integer.parseInt(s.substring(3, 5), 16) << 8) |
+                                Integer.parseInt(s.substring(5, 7), 16);
+                        } catch (NumberFormatException e) {
+                            Log.log(this, "invalid color " + s);
+                            return;
+                        }
                     else if (s.equals("black")) newcolor = black;
                     else if (s.equals("blue")) newcolor = blue;
                     else if (s.equals("green")) newcolor = green;
@@ -101,8 +105,7 @@ class SpecialBoxProperty {
                     else if (s.equals("pink")) newcolor = pink;
                     else if (s.equals("yellow")) newcolor = yellow;
                     else if (s.equals("white")) newcolor = white;
-                    else if (Log.on) Log.log(this, "invalid color " + s);
-
+                    else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
                     if (newcolor == b.color) return;
                     b.color = newcolor;
                     b.dirty();
@@ -145,8 +148,7 @@ class SpecialBoxProperty {
                     else if (s.equals("pink")) newtextcolor = pink;
                     else if (s.equals("yellow")) newtextcolor = yellow;
                     else if (s.equals("white")) newtextcolor = white;
-                    else if (Log.on) Log.log(this, "invalid textcolor " + s);
-
+                    else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
                     if (newtextcolor == b.textcolor) return;
                     b.textcolor = newtextcolor;
                     b.dirty();
@@ -163,14 +165,11 @@ class SpecialBoxProperty {
                             if (Log.on) Log.log(this, 
                                                 "ISO Control characters are not permitted in box text strings; offending character is ASCII " +
                                                ((int)t.charAt(i)));
-                            /* FIXME: reinstate
                             return;
-                            */
                         }
 
-                    // FIXME: don't know why this is needed
+                    // FEATURE: try removing the following line; it appears to be redundant
                     b.dirty();
-
                     b.text = t;
                     b.textupdate();
                     b.dirty();
@@ -219,7 +218,7 @@ class SpecialBoxProperty {
 
         specialBoxProperties.put("static", new SpecialBoxProperty() {
                 public Object get(Box b) {
-                    String cfsn = JSObject.getCurrentFunctionSourceName();
+                    String cfsn = JS.Thread.fromJavaThread(Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
                     for(int i=0; i<cfsn.length() - 1; i++)
                         if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
                             cfsn = cfsn.substring(0, i);
@@ -257,8 +256,7 @@ class SpecialBoxProperty {
                     boolean newshrink = stob(value);
                     if (b.hshrink == newshrink && b.vshrink == newshrink) return;
                     b.hshrink = b.vshrink = newshrink;
-                    if (b.hshrink) b.set(b.dmax, 0, Box.max(b.cmin(0), b.textdim(0) + 2 * b.pad(0), b.dmin(0)));
-                    if (b.vshrink) b.set(b.dmax, 1, Box.max(b.cmin(1), b.textdim(1) + 2 * b.pad(1), b.dmin(1)));
+                    b.mark_for_prerender();
                 } });
         
         specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
@@ -267,7 +265,7 @@ class SpecialBoxProperty {
                     boolean newshrink = stob(value);
                     if (b.hshrink == newshrink) return;
                     b.hshrink = newshrink;
-                    if (b.hshrink) b.set(b.dmax, 0, Box.max(b.cmin(0), b.textdim(0) + 2 * b.pad(0), b.dmin(0)));
+                    b.mark_for_prerender();
                 }
             });
         
@@ -277,7 +275,7 @@ class SpecialBoxProperty {
                     boolean newshrink = stob(value);
                     if (b.vshrink == newshrink) return;
                     b.vshrink = newshrink;
-                    if (b.vshrink) b.set(b.dmax, 1, Box.max(b.cmin(1), b.textdim(1) + 2 * b.pad(1), b.dmin(1)));
+                    b.mark_for_prerender();
                 }
             });
         
@@ -292,7 +290,7 @@ class SpecialBoxProperty {
                         b.surface.setLocation(stoi(value), b.abs(1));
                         b.surface.centerSurfaceOnRender = false;
                     }
-                    b.set(abs, 0, stosh(value));
+                    b.set(abs, 0, stoi(value));
                 }
             });
         
@@ -307,7 +305,7 @@ class SpecialBoxProperty {
                         b.surface.setLocation(b.abs(0), stoi(value));
                         b.surface.centerSurfaceOnRender = false;
                     }
-                    b.set(abs, 1, stosh(value));
+                    b.set(abs, 1, stoi(value));
                 }
             });
 
@@ -316,11 +314,11 @@ class SpecialBoxProperty {
                 public void put(Box b, Object value) {
                     if (b.sizetoimage) return;
                     if (b.getParent() == null && b.surface != null) {
-                        b.set(size, 0, Box.max(Surface.scarPicture.getWidth(), stosh(value)));
+                        b.set(size, 0, Box.max(Surface.scarPicture.getWidth(), stoi(value)));
                         b.mark_for_prerender();
                     } else {
-                        b.set(dmax, 0, stosh(value));
-                        b.set(dmin, 0, stosh(value));
+                        b.set(dmax, 0, stoi(value));
+                        b.set(dmin, 0, stoi(value));
                     }
                 } });
         
@@ -329,11 +327,11 @@ class SpecialBoxProperty {
                 public void put(Box b, Object value) {
                     if (b.sizetoimage) return;
                     if (b.getParent() == null && b.surface != null) {
-                        b.set(size, 1, Box.max(Surface.scarPicture.getHeight(), stosh(value)));
+                        b.set(size, 1, Box.max(Surface.scarPicture.getHeight(), stoi(value)));
                         b.mark_for_prerender();
                     } else {
-                        b.set(dmax, 1, stosh(value));
-                        b.set(dmin, 1, stosh(value));
+                        b.set(dmax, 1, stoi(value));
+                        b.set(dmin, 1, stoi(value));
                     }
                 } });
 
@@ -375,7 +373,10 @@ class SpecialBoxProperty {
                 } });
         
         specialBoxProperties.put("invisible", new SpecialBoxProperty() {
-                public Object get(Box b) { return b.invisible ? Boolean.TRUE : Boolean.FALSE; }
+                public Object get(Box b) {
+                    for (Box cur = b; cur != null; cur = cur.getParent()) { if (cur.invisible) return Boolean.TRUE; }
+                    return Boolean.FALSE;
+                }
                 public void put(Box b, Object value) {
                     boolean newinvisible = stob(value);
                     if (newinvisible == b.invisible) return;
@@ -416,9 +417,8 @@ class SpecialBoxProperty {
         specialBoxProperties.put("font", new SpecialBoxProperty() {
                 public Object get(Box b) { return b.font; }
                 public void put(Box b, Object value) {
-                    if (value == null) value = Platform.getDefaultFont();
-                    b.font = value.toString();
-                    b.textupdate();
+                    b.font = value == null ? null : value.toString();
+                    b.fontChanged();
                     b.dirty();
                 } });
         
@@ -426,7 +426,7 @@ class SpecialBoxProperty {
                 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(0)); }
                 public void put(Box b, Object value) {
                     if (b.surface == null || b.getParent() == null) return;
-                    b.put("x", null, new Integer(stoi(value) - stoi(get(b.getParent()))));
+                    b.put("x", new Integer(stoi(value) - stoi(get(b.getParent()))));
                 }
             });
         
@@ -434,7 +434,7 @@ class SpecialBoxProperty {
                 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(1)); }
                 public void put(Box b, Object value) {
                     if (b.surface == null || b.getParent() == null) return;
-                    b.put("y", null, new Integer(stoi(value) - stoi(get(b.getParent()))));
+                    b.put("y", new Integer(stoi(value) - stoi(get(b.getParent()))));
                 }
             });
         
@@ -477,20 +477,15 @@ class SpecialBoxProperty {
         SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
                 public void put(String name, Box b, Object value) {
                     if (b.surface == null) return;
-                    if (b.getParent() == null) {
-                        if (b.surface.boxContainingMouse != null && b.surface.boxContainingMouse.getParent() != null)
-                            b.surface.boxContainingMouse.put(name, b.surface.boxContainingMouse, value);
-                    } else {
-                        // check siblings
-                        for(Box c = b.prevSibling(); c != null; c = c.prevSibling())
-                            if (c.inside(c.surface.mousex, c.surface.mousey)) {
-                                c.put(name, c, value);
-                                return;
-                            }
-                        // move up a level
-                        if (b.getParent() != null && b.getParent().getParent() != null)
-                            b.getParent().put(name, b.getParent(), value);
+                    for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
+                        Box siblingChild = c.whoIs(c.surface.mousex, c.surface.mousey);
+                        if (siblingChild != null) {
+                            siblingChild.put(name, value);
+                            return;
+                        }
                     }
+                    if (b.getParent() != null)
+                        b.getParent().put(name, value);
                 }};
 
         specialBoxProperties.put("Press1", mouseEventHandler);
@@ -510,6 +505,7 @@ class SpecialBoxProperty {
                 public Object get(Box b) {
                     if (b.surface == null) return null;
                     else if (b.getRoot() == null) return null;
+                    else if (b.getParent() == null) return b;
                     else return b.getRoot().getRootProxy();
                 } });
 
@@ -571,7 +567,7 @@ class SpecialBoxProperty {
 
         specialBoxProperties.put("Close", new SpecialBoxProperty() {
                 public void put(Box b, Object value) {
-                    if (b.getParent() == null && b.surface != null) b.surface.dispose();
+                    if (b.getParent() == null && b.surface != null) b.surface.dispose(true);
                 }
             });
 
@@ -590,7 +586,7 @@ class SpecialBoxProperty {
                 public void put(Box b, Object value) {
                     if (b.redirect == null) return;
                     if (b.redirect != b) { put(b.redirect, value); return; }
-                    short newval = stosh(value);
+                    int newval = stoi(value);
                     if (newval == b.pad(0)) return;
                     b.set(pad, 0, newval);
                 }
@@ -605,7 +601,7 @@ class SpecialBoxProperty {
                 public void put(Box b, Object value) {
                     if (b.redirect == null) return;
                     if (b.redirect != b) { put(b.redirect, value); return; }
-                    short newval = stosh(value);
+                    int newval = stoi(value);
                     if (newval == b.pad(1)) return;
                     b.set(pad, 1, newval);
                 }
@@ -619,7 +615,7 @@ class SpecialBoxProperty {
                 public Object get(Box b) { return new Integer(b.dmin(0)); }
                 public void put(Box b, Object value) {
                     if (b.sizetoimage) return;
-                    b.set(dmin, 0, stosh(value));
+                    b.set(dmin, 0, stoi(value));
                 }
             });
 
@@ -627,7 +623,7 @@ class SpecialBoxProperty {
                 public Object get(Box b) { return new Integer(b.dmax(0)); }
                 public void put(Box b, Object value) {
                     if (b.sizetoimage) return;
-                    b.set(dmax, 0, stosh(value));
+                    b.set(dmax, 0, stoi(value));
                 }
             });
 
@@ -635,7 +631,7 @@ class SpecialBoxProperty {
                 public Object get(Box b) { return new Integer(b.dmin(1)); }
                 public void put(Box b, Object value) {
                     if (b.sizetoimage) return;
-                    b.set(dmin, 1, stosh(value));
+                    b.set(dmin, 1, stoi(value));
                 }
             });
 
@@ -643,9 +639,28 @@ class SpecialBoxProperty {
                 public Object get(Box b) { return new Integer(b.dmax(1)); }
                 public void put(Box b, Object value) {
                     if (b.sizetoimage) return;
-                    b.set(dmax, 1, stosh(value));
+                    b.set(dmax, 1, stoi(value));
                 }
             });
+
+        specialBoxProperties.put("redirect", new SpecialBoxProperty() {
+                public void put(Box b, Object value) { }
+                public Object get(Box b) {
+                    if (b.redirect == null) return null;
+                    if (b.redirect == b) return Boolean.TRUE;
+                    return get(b.redirect);
+                }
+            });
+
+        specialBoxProperties.put("apply", new SpecialBoxProperty() {
+                public void put(Box b, Object value) { }
+                public Object get(final Box b) { return new Apply(b); }
+            });
+        
+        specialBoxProperties.put("id", new SpecialBoxProperty() {
+                public void put(Box b, Object value) { }
+                public Object get(Box b) { return b.id; }
+            });
     }
 
         
@@ -668,19 +683,33 @@ class SpecialBoxProperty {
         catch (NumberFormatException e) { return 0; }
     }
         
-    /** helper that converts a String to a short according to JavaScript coercion rules */
-    public static short stosh(Object o) {
-        if (o == null) return 0;
-        if (o instanceof Number) return ((Number)o).shortValue();
-
-        String s;
-        if (!(o instanceof String)) s = o.toString();
-        else s = (String)o;
-
-        try { return Short.parseShort(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
-        catch (NumberFormatException e) { return 0; }
+    private static class Apply extends JS.Callable {
+        Box b;
+        public Apply(Box b) { this.b = b; }
+        public Object call(JS.Array args) throws JS.Exn {
+            if (args.elementAt(0) instanceof String) {
+                String templatename = (String)args.elementAt(0);
+                Template t = Template.getTemplate(templatename, null);
+                if (t == null) {
+                    if (Log.on) Log.logJS(this, "template " + templatename + " not found");
+                } else {
+                    if (ThreadMessage.suspendThread()) try {
+                        JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
+                        t.apply(b, null, null, callback, 0, t.numUnits());
+                    } finally {
+                        ThreadMessage.resumeThread();
+                    }
+                }
+                
+            } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
+                JS s = (JS)args.elementAt(0);
+                Object[] keys = s.keys();
+                for(int j=0; j<keys.length; j++) b.put(keys[j].toString(), s.get(keys[j]));
+            }
+            
+            return b;
+        }
     }
-        
 }