2003/02/22 03:55:06
[org.ibex.core.git] / src / org / xwt / Box.java
index 1e9bcc0..17283f6 100644 (file)
@@ -122,63 +122,63 @@ public final class Box extends JSObject {
 
     /** The maximum <i>defined</i> width and height of this box */
     public static final int dmax = 0;     
-    private short _dmax_0 = 0;
-    private short _dmax_1 = 0;
-    public final short dmax(int axis) { return axis == 0 ? _dmax_0 : _dmax_1; }
+    private int _dmax_0 = 0;
+    private int _dmax_1 = 0;
+    public final int dmax(int axis) { return axis == 0 ? _dmax_0 : _dmax_1; }
 
     /** The minimum <i>defined</i> width and height of this box */
     public static final int dmin = 1;     
-    private short _dmin_0 = 0;
-    private short _dmin_1 = 0;
-    public final short dmin(int axis) { return axis == 0 ? _dmin_0 : _dmin_1; }
+    private int _dmin_0 = 0;
+    private int _dmin_1 = 0;
+    public final int dmin(int axis) { return axis == 0 ? _dmin_0 : _dmin_1; }
     
     /** The minimum <i>calculated</i> width and height of this box -- unlike dmin, this takes childrens' sizes into account */
     public static final int cmin = 2;     
-    private short _cmin_0 = 0;
-    private short _cmin_1 = 0;
-    public final short cmin(int axis) { return axis == 0 ? _cmin_0 : _cmin_1; }
+    private int _cmin_0 = 0;
+    private int _cmin_1 = 0;
+    public final int cmin(int axis) { return axis == 0 ? _cmin_0 : _cmin_1; }
 
     /** The position of this box, relitave to the parent */
     public static final int abs = 3;      
-    private short _abs_0 = 0;
-    private short _abs_1 = 0;
-    public final short abs(int axis) { return axis == 0 ? _abs_0 : _abs_1; }
+    private int _abs_0 = 0;
+    private int _abs_1 = 0;
+    public final int abs(int axis) { return axis == 0 ? _abs_0 : _abs_1; }
 
     /** The absolute position of this box (ie relitave to the root); set by the parent */
     public static final int pos = 4;      
-    private short _pos_0 = 0;
-    private short _pos_1 = 0;
-    public final short pos(int axis) { return axis == 0 ? _pos_0 : _pos_1; }
+    private int _pos_0 = 0;
+    private int _pos_1 = 0;
+    public final int pos(int axis) { return axis == 0 ? _pos_0 : _pos_1; }
 
     /** The actual size of this box; set by the parent. */
     public static final int size = 5;     
-    short _size_0 = 0;
-    short _size_1 = 0;
-    public final short size(int axis) { return axis == 0 ? _size_0 : _size_1; }
+    int _size_0 = 0;
+    int _size_1 = 0;
+    public final int size(int axis) { return axis == 0 ? _size_0 : _size_1; }
 
     /** The old actual absolute position of this box (ie relitave to the root) */
     public static final int oldpos = 6;   
-    private short _oldpos_0 = 0;
-    private short _oldpos_1 = 0;
-    public final short oldpos(int axis) { return axis == 0 ? _oldpos_0 : _oldpos_1; }
+    private int _oldpos_0 = 0;
+    private int _oldpos_1 = 0;
+    public final int oldpos(int axis) { return axis == 0 ? _oldpos_0 : _oldpos_1; }
 
     /** The old actual size of this box */
     public static final int oldsize = 7;  
-    private short _oldsize_0 = 0;
-    private short _oldsize_1 = 0;
-    public final short oldsize(int axis) { return axis == 0 ? _oldsize_0 : _oldsize_1; }
+    private int _oldsize_0 = 0;
+    private int _oldsize_1 = 0;
+    public final int oldsize(int axis) { return axis == 0 ? _oldsize_0 : _oldsize_1; }
 
     /** The padding along each edge for this box */
     public static final int pad = 8;      
-    private short _pad_0 = 0;
-    private short _pad_1 = 0;
-    public final short pad(int axis) { return axis == 0 ? _pad_0 : _pad_1; }
+    private int _pad_0 = 0;
+    private int _pad_1 = 0;
+    public final int pad(int axis) { return axis == 0 ? _pad_0 : _pad_1; }
 
     /** The dimensions of the text in this box */
     public static final int textdim = 9;
-    private short _textdim_0 = 0;
-    private short _textdim_1 = 0;
-    public final short textdim(int axis) { return axis == 0 ? _textdim_0 : _textdim_1; }
+    private int _textdim_0 = 0;
+    private int _textdim_1 = 0;
+    public final int textdim(int axis) { return axis == 0 ? _textdim_0 : _textdim_1; }
 
 
     // Instance Data /////////////////////////////////////////////////////////////////
@@ -257,6 +257,15 @@ public final class Box extends JSObject {
     public Hash traps = null;
 
 
+    // Vector Support /////////////////////////////////////////////////////////////////////
+
+    /** The SVG path for this node */
+    public String path = null;
+
+    /** The affine multiplied by the CTM *before* this element and its children are processed */
+    public SVG.Affine transform = null;
+
+
     // Instance Data: IndexOf  ////////////////////////////////////////////////////////////
 
     /** The indexof() Function; created lazily */
@@ -285,8 +294,7 @@ public final class Box extends JSObject {
     // Methods which enforce/preserve invariants ////////////////////////////////////////////
 
     /** This method MUST be used to change geometry values -- it ensures that certain invariants are preserved. */
-    public final void set(int which, int axis, int newvalue) { set(which, axis, (short)newvalue); }
-    public final void set(int which, int axis, short newvalue) {
+    public final void set(int which, int axis, int newvalue) {
 
         // if this Box is the root of the Surface, notify the Surface of size changes
         if (getParent() == null && surface != null && which == size)
@@ -313,16 +321,11 @@ public final class Box extends JSObject {
         if (which == dmin) set(size, axis, max(size(axis), newvalue));
         if (which == dmax) set(size, axis, min(size(axis), newvalue));
 
-        // keep obedience to shrink directives
-        if (which == cmin || which == textdim || which == pad || which == dmin)
-            if ((hshrink && axis == 0) || (vshrink && axis == 1))
-                set(dmax, axis, max(cmin(axis), (textdim(axis) + 2 * pad(axis)), dmin(axis)));
-
         // keep cmin in line with dmin/dmax/textdim
         if (which == dmax || which == dmin || which == textdim || which == pad || which == cmin)
             set(cmin, axis,
                 max(
-                    min(cmin(axis), dmax(axis)),
+                    min(dmax(axis), cmin(axis)),
                     dmin(axis),
                     min(dmax(axis), textdim(axis) + 2 * pad(axis))
                     )
@@ -358,13 +361,13 @@ public final class Box extends JSObject {
     /** Ensures that cmin is in sync with the cmin's of our children. This should be called whenever a child is added or
      *  removed, as well as when our pad is changed. */
     final void sync_cmin_to_children() {
-        short co = (short)(2 * pad(o));
-        short cxo = (short)(2 * pad(xo));
+        int co = (int)(2 * pad(o));
+        int cxo = (int)(2 * pad(xo));
         
         for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
             if (bt.invisible || bt.absolute) continue;
             co += bt.cmin(o);
-            cxo = (short)max(bt.cmin(xo) + 2 * pad(xo), cxo);
+            cxo = (int)max(bt.cmin(xo) + 2 * pad(xo), cxo);
         }
         
         set(cmin, o, co);
@@ -413,8 +416,7 @@ public final class Box extends JSObject {
     }
 
     /** loads the image described by string str, possibly blocking for a network load */
-    private static ImageDecoder getImage(String str) {
-        ImageDecoder ret = null;
+    static ImageDecoder getImage(String str, final Function callback) {
         boolean ispng = false;
 
         if (str.indexOf(':') == -1) {
@@ -438,9 +440,39 @@ public final class Box extends JSObject {
                 if (str.endsWith(".jpeg") || str.endsWith(".jpg"))
                     str = "http://xmlrpc.xwt.org/jpeg2png/" + str.substring(str.indexOf("//") + 2);
 
-                if (str.endsWith(".gif")) ret = GIF.decode(new HTTP(str).getInputStream(), str);
-                else ret = PNG.decode(new HTTP(str).getInputStream(), str);
-                return ret;
+                HTTP http = new HTTP(str);
+                final HTTP.HTTPInputStream in = http.GET();
+                final int contentLength = in.getContentLength();
+                InputStream is = new FilterInputStream(in) {
+                        int bytesDownloaded = 0;
+                        boolean clear = true;
+                        public int read() throws IOException {
+                            bytesDownloaded++;
+                            return super.read();
+                        }
+                        public int read(byte[] b, int off, int len) throws IOException {
+                            int ret = super.read(b, off, len);
+                            if (ret != -1) bytesDownloaded += ret;
+                            if (clear && callback != null) {
+                                clear = false;
+                                ThreadMessage.newthread(new JSObject.JSFunction() {
+                                        public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                                            try {
+                                                callback.call(cx, null, null, new Object[] {
+                                                    new Double(bytesDownloaded), new Double(contentLength) });
+                                            } finally {
+                                                clear = true;
+                                            }
+                                            return null;
+                                        }
+                                    });
+                            }
+                            return ret;
+                        }
+                    };
+                
+                if (str.endsWith(".gif")) return GIF.decode(is, str);
+                else return PNG.decode(is, str);
 
             } catch (IOException e) {
                 if (Log.on) Log.log(Box.class, "error while trying to load an image from " + str);
@@ -460,7 +492,7 @@ public final class Box extends JSObject {
         Picture ret = null;
         ret = (Picture)pictureCache.get(os);
         if (ret != null) return ret;
-        ImageDecoder id = getImage(os);
+        ImageDecoder id = getImage(os, null);
         if (id == null) return null;
         ret = Platform.createPicture(id);
         pictureCache.put(os, ret);
@@ -498,7 +530,7 @@ public final class Box extends JSObject {
         } else {
             border = (Picture[])bordercache.get(s);
             if (border == null) {
-                ImageDecoder id = getImage(s);
+                ImageDecoder id = getImage(s, null);
                 if (id == null) {
                     if (Log.on) Log.log(this, "unable to load border image " + s + " at " +
                                     Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
@@ -607,26 +639,31 @@ public final class Box extends JSObject {
     }
 
     /** creates a new box from an anonymous template; <tt>ids</tt> is passed through to Template.apply() */
-    Box(Template anonymous, Vec pboxes, Vec ptemplates) {
+    Box(Template anonymous, Vec pboxes, Vec ptemplates, Function callback, int numerator, int denominator) {
         super(true);
-        set(dmax, 0, Short.MAX_VALUE);
-        set(dmax, 1, Short.MAX_VALUE);
+        set(dmax, 0, Integer.MAX_VALUE);
+        set(dmax, 1, Integer.MAX_VALUE);
         template = anonymous;
-        template.apply(this, pboxes, ptemplates);
+        template.apply(this, pboxes, ptemplates, callback, numerator, denominator);
         templatename = null;
         importlist = null;
     }
 
     /** creates a new box from an unresolved templatename and an importlist; use "box" for an untemplatized box */
-    public Box(String templatename, String[] importlist) {
+    public Box(String templatename, String[] importlist) { this(templatename, importlist, null); }
+    public Box(String templatename, String[] importlist, Function callback) {
         super(true);
-        set(dmax, 0, Short.MAX_VALUE);
-        set(dmax, 1, Short.MAX_VALUE);
+        set(dmax, 0, Integer.MAX_VALUE);
+        set(dmax, 1, Integer.MAX_VALUE);
         this.importlist = importlist;
-        template = "box".equals(templatename) ? null : Template.getTemplate(templatename, importlist);
-        this.templatename = templatename;
+        if (!"box".equals(templatename)) {
+            template = Template.getTemplate(templatename, importlist);
+            if (template == null)
+                if (Log.on) Log.log(this, "couldn't find template \"" + templatename + "\"");
+        }
         if (template != null) {
-            template.apply(this, null, null);
+            this.templatename = templatename;
+            template.apply(this, null, null, callback, 0, template.numUnits());
             if (redirect == this && !"self".equals(template.redirect)) redirect = null;
         }
     }
@@ -637,6 +674,8 @@ public final class Box extends JSObject {
     /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
     void prerender() {
 
+        if (invisible) return;
+
         if (getParent() == null) {
             set(pos, 0, 0);
             set(pos, 1, 0);
@@ -732,8 +771,8 @@ public final class Box extends JSObject {
         for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
             if (bt.invisible) continue;
             if (bt.absolute) {
-                bt.set(size, o, max(bt.cmin(o), min(size(o) - bt.abs(o) - pad(o), bt.dmax(o))));
-                bt.set(size, xo, max(bt.cmin(xo), min(size(xo) - bt.abs(xo) - pad(xo), bt.dmax(xo))));
+                bt.set(size, 0, bt.hshrink ? bt.cmin(0) : max(bt.cmin(0), min(size(0) - bt.abs(0) - pad(0), bt.dmax(0))));
+                bt.set(size, 1, bt.vshrink ? bt.cmin(1) : max(bt.cmin(1), min(size(1) - bt.abs(1) - pad(1), bt.dmax(1))));
             } else if (xo == 0 && bt.hshrink || xo == 1 && bt.vshrink) {
                 bt.set(size, xo, bt.cmin(xo));
             } else {
@@ -768,15 +807,16 @@ public final class Box extends JSObject {
             for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
                 if (bt.absolute || bt.invisible) continue;
 
-                bt.set(size, o, bound(bt.cmin(o), factor * bt.flex, bt.dmax(o)));
+                int btmax = (o == 0 && bt.hshrink) || (o == 1 && bt.vshrink) ? bt.cmin(o) : bt.dmax(o);
+                bt.set(size, o, bound(bt.cmin(o), factor * bt.flex, btmax));
                 total += bt.size(o);
 
                 if (factor * bt.flex < bt.cmin(o) && bt.size(o) == bt.cmin(o)) {
                     nextjoint = min(nextjoint, divide_round_up(bt.cmin(o), bt.flex));
 
-                } else if (bt.size(o) < bt.dmax(o)) {
+                } else if (bt.size(o) < btmax) {
                     remaining_flex += bt.flex;
-                    nextjoint = min(nextjoint, divide_round_up(bt.dmax(o), bt.flex));
+                    nextjoint = min(nextjoint, divide_round_up(btmax, bt.flex));
 
                 }
             }
@@ -793,10 +833,11 @@ public final class Box extends JSObject {
 
         // arbitrarily distribute out any leftovers resulting from rounding errors
         int last = 0;
-        while(goal > total && total != last) {
+        while(goal != total && total != last) {
             last = total;
             for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
-                int newsize = bound(bt.cmin(o), bt.size(o) + 1, bt.dmax(o));
+                int btmax = (o == 0 && bt.hshrink) || (o == 1 && bt.vshrink) ? bt.cmin(o) : bt.dmax(o);
+                int newsize = bound(bt.cmin(o), bt.size(o) + (goal > total ? 1 : -1), btmax);
                 total += newsize - bt.size(o);
                 bt.set(size, o, newsize);
             }
@@ -826,7 +867,8 @@ public final class Box extends JSObject {
     // Rendering Pipeline /////////////////////////////////////////////////////////////////////
 
     /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
-    void render(int xIn, int yIn, int wIn, int hIn, DoubleBuffer buf) {
+    void render(int xIn, int yIn, int wIn, int hIn, DoubleBuffer buf) { render(xIn, yIn, wIn, hIn, buf, null); }
+    void render(int xIn, int yIn, int wIn, int hIn, DoubleBuffer buf, SVG.Affine ctm) {
         if (surface.abort || invisible) return;
 
         // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
@@ -844,7 +886,6 @@ public final class Box extends JSObject {
             int y1 = max(y, pos(1) + bh);
             int x2 = min(x + w, pos(0) + size(0) - bw);
             int y2 = min(y + h, pos(1) + size(1) - bh);
-            buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
             if (y2 - y1 > 0 && x2 - x1 > 0)
                 buf.fillRect(x1,y1,x2,y2,(color & 0xFF000000) != 0 ? color : SpecialBoxProperty.lightGray);
         }
@@ -854,6 +895,24 @@ public final class Box extends JSObject {
             else renderStretchedImage(x, y, w, h, buf);
         }
 
+       // FIXME: this should go at the top of this method
+       SVG.Affine ctm_;
+       if (transform != null) {
+           if (ctm == null) ctm = SVG.Affine.identity();
+           ctm_ = ctm.copy();
+           ctm_.multiply(transform);
+       } else {
+           ctm_ = ctm;
+       }
+
+       if (path != null) {
+           SVG.VP vp = SVG.VP.fromString(path);
+           if (ctm_ != null) vp.multiply(ctm_);
+           vp.sort();
+           vp.fill(buf, null, 0xFFFF0000, null);
+           vp.stroke(buf, 100, textcolor, false);
+       }
+
         if (text != null && !text.equals("")) renderText(x, y, w, h, buf);
 
         // now subtract the pad region from the clip region before proceeding
@@ -863,7 +922,7 @@ public final class Box extends JSObject {
         int h2 = min(y + h, pos(1) + size(1) - pad(1)) - y2;
 
         for(Box b = getChild(0); b != null; b = b.nextSibling())
-            b.render(x2, y2, w2, h2, buf);   
+            b.render(x2, y2, w2, h2, buf, ctm_);
     }
 
     private void renderBorder(int x, int y, int w, int h, DoubleBuffer buf) {
@@ -1035,28 +1094,42 @@ public final class Box extends JSObject {
      *  WARNING: O(n) runtime, unless i == numChildren()
      */
     public void put(int i, Scriptable start, Object value) {
-        if (value == null) {
-            if (i >= 0 && i < numChildren()) getChild(i).remove();
-            return;
-        }
-        if (value instanceof RootProxy) {
-            if (Log.on) Log.log(this, "attempt to reparent a box via its proxy object at " +
-                                Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
-            return;
-        } else if (!(value instanceof Box)) {
+
+        if (value != null && !(value instanceof Box)) {
             if (Log.on) Log.log(this, "attempt to set a numerical property on a box to anything other than a box at " +
                                 Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
-            return;
-        }
-        Box newnode = (Box)value;
-        if (redirect == null) {
-            if (Log.on) Log.log(this, "attempt to add a child to a node with a null redirect at " + 
+
+        } else if (redirect == null) {
+            if (Log.on) Log.log(this, "attempt to add/remove children to/from a node with a null redirect at " + 
                                 Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
-            return;
-        } else if (redirect != this) redirect.put(i, null, newnode);
-        else {
+
+        } else if (redirect != this) {
+            Box b = value == null ? (Box)redirect.get(i, null) : (Box)value;
+            redirect.put(i, null, value);
+            put("0", null, b);
+
+        } else if (value == null) {
+            if (i >= 0 && i < numChildren()) {
+                Box b = getChild(i);
+                b.remove();
+                put("0", null, b);
+            }
+
+        } else if (value instanceof RootProxy) {
+            if (Log.on) Log.log(this, "attempt to reparent a box via its proxy object at " +
+                                Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
+
+        } else {
+            Box newnode = (Box)value;
+            for(Box cur = this; cur != null; cur = cur.getParent())
+                if (cur == newnode) {
+                    if (Log.on) Log.log(this, "attempt to make a node a parent of its own ancestor at " + 
+                                        Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
+                    return;
+                }
+
             if (numKids > 15 && children == null) convert_to_array();
-            if (newnode.parent != null) newnode.remove();
+            newnode.remove();
             newnode.parent = this;
             
             if (children == null) {
@@ -1091,17 +1164,17 @@ public final class Box extends JSObject {
                 }
             }
             newnode.setSurface(surface);
-
+            
             // need both of these in case child was already uncalc'ed
             newnode.mark_for_prerender();
             mark_for_prerender(); 
-
+            
             newnode.dirty();
             sync_cmin_to_children();
-        }
 
-        // note that JavaScript box[0] will invoke put(int i), not put(String s)
-        put("0", null, newnode);
+            // note that JavaScript box[0] will invoke put(int i), not put(String s)
+            put("0", null, newnode);
+        }
     }
     
     public Object get(String name, Scriptable start) { return get(name, start, false); }
@@ -1132,7 +1205,11 @@ public final class Box extends JSObject {
         SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
         if (gph != null) return gph.get(this);
 
-        return super.get(name, start);
+        Object ret = super.get(name, start);
+        if (name.startsWith("$") && ret == null)
+            if (Log.on) Log.log(this, "WARNING: attempt to access " + name + ", but no child with id=\"" + name.substring(1) + "\" found; " +
+                                Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
+        return ret;
     }
 
     /** indicate that we don't want JSObject trying to handle these */
@@ -1146,6 +1223,12 @@ public final class Box extends JSObject {
         return super.has(name, start);
     }
 
+    public Object[] getIds() {
+        Object[] ret = new Object[numChildren()];
+        for(int i=0; i<ret.length; i++) ret[i] = get(i, null);
+        return ret;
+    }
+
     public void put(String name, Scriptable start, Object value) { put(name, start, value, false, null); }
     public void put(String name, Scriptable start, Object value, boolean ignoretraps) { put(name, start, value, ignoretraps, null); }
 
@@ -1254,7 +1337,7 @@ public final class Box extends JSObject {
     /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
     public void remove() {
         if (parent == null) {
-            if (surface != null) surface.dispose();
+            if (surface != null) surface.dispose(true);
             return;
         }
         Box oldparent = getParent();
@@ -1440,6 +1523,10 @@ public final class Box extends JSObject {
 
     /** returns numerator/denominator, but rounds <i>up</i> instead of down */
     static final int divide_round_up(int numerator, int denominator) {
+
+        // cope with bozos who use flex==0.0
+        if (denominator == 0) return Integer.MAX_VALUE;
+
         int ret = numerator / denominator;
         if (ret * denominator < numerator) return ret + 1;
         return ret;