2002/09/15 23:36:01
[org.ibex.core.git] / src / org / xwt / Box.java
index e7d63cb..4c2e1b4 100644 (file)
@@ -414,7 +414,6 @@ public final class Box extends JSObject {
 
     /** loads the image described by string str, possibly blocking for a network load */
     static ImageDecoder getImage(String str, final Function callback) {
-        ImageDecoder ret = null;
         boolean ispng = false;
 
         if (str.indexOf(':') == -1) {
@@ -438,9 +437,10 @@ public final class Box extends JSObject {
                 if (str.endsWith(".jpeg") || str.endsWith(".jpg"))
                     str = "http://xmlrpc.xwt.org/jpeg2png/" + str.substring(str.indexOf("//") + 2);
 
-                final HTTP http = new HTTP(str);
-                final int contentLength = http.getContentLength();
-                InputStream is = new FilterInputStream(http.getInputStream()) {
+                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 {
@@ -467,10 +467,9 @@ public final class Box extends JSObject {
                             return ret;
                         }
                     };
-
-                if (str.endsWith(".gif")) ret = GIF.decode(is, str);
-                else ret = PNG.decode(is, str);
-                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);
@@ -830,10 +829,10 @@ 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 newsize = bound(bt.cmin(o), bt.size(o) + (goal > total ? 1 : -1), bt.dmax(o));
                 total += newsize - bt.size(o);
                 bt.set(size, o, newsize);
             }
@@ -881,7 +880,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);
         }
@@ -1072,36 +1070,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;
 
-        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;
+        } 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);
+
+        } 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);
             }
 
-        if (redirect == null) {
-            if (Log.on) Log.log(this, "attempt to add a child to a node with a null redirect at " + 
+        } 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);
-            return;
-        } else if (redirect != this) redirect.put(i, null, newnode);
-        else {
+
+        } 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) {
@@ -1136,17 +1140,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); }
@@ -1195,6 +1199,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); }
 
@@ -1303,7 +1313,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();