UNDO: backed out broken part of 500 fix
[org.ibex.core.git] / src / org / ibex / Box.java
index 514d301..524cb7c 100644 (file)
@@ -218,7 +218,7 @@ public final class Box extends JSScope implements Scheduler.Task {
     private static LENGTH[] colMaxWidth = new LENGTH[65535];
     private static LENGTH[] rowHeight = new LENGTH[65535];
     private static LENGTH[] rowMaxHeight = new LENGTH[65535];
-    static { for(int i=0; i<rowMaxHeight.length; i++) { rowMaxHeight[i] = 0; colMaxWidth[i] = 0; } }
+    static { for(int i=0; i<rowMaxHeight.length; i++) { rowMaxHeight[i] = MAX_LENGTH; colMaxWidth[i] = MAX_LENGTH; } }
 
     Box nextPackedSibling() { Box b = nextSibling(); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); }
     Box firstPackedChild() { Box b = getChild(0); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); }
@@ -295,32 +295,30 @@ public final class Box extends JSScope implements Scheduler.Task {
         //#repeat col/row colspan/rowspan contentwidth/contentheight x/y width/height colMaxWidth/rowMaxHeight colWidth/rowHeight \
         //        HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight colWidth/rowHeight x_slack/y_slack
         // PHASE 1: compute column min/max sizes
+        int x_slack = width;
+        for(int i=0; i<cols; i++) x_slack -= colWidth[i];
         for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling())
             for(int i=child.col; i < child.col + child.colspan; i++) {
+                x_slack += colWidth[i];
                 colWidth[i] = max(colWidth[i], child.contentwidth / child.colspan);
-                colMaxWidth[i] = max(colMaxWidth[i], (child.test(HSHRINK) ? child.contentwidth : child.maxwidth) / child.colspan);
+                x_slack -= colWidth[i];
+                colMaxWidth[i] = min(colMaxWidth[i], child.test(HSHRINK) ? child.contentwidth : child.maxwidth) / child.colspan;
             }
         
         // PHASE 2: hand out slack
-        int x_slack = width;
-        for(int i=0; i<cols; i++) x_slack -= colWidth[i]; 
-        // loop through until there are no more eligible children or all slack is handed out
-        for(int eligible = cols, slacklevel = 0; x_slack > 0 && eligible > 0;) {
-            int increment = max(1, x_slack / eligible);
-            eligible = 0;
-            for(short col=0, count=0; col < cols; col++) {
-                if (colMaxWidth[col] <= colWidth[col]) continue;
-                if (slacklevel + increment < colWidth[col]) { eligible++; continue; }
-                int diff = min(increment, min(colMaxWidth[col] - colWidth[col], slacklevel + increment - colWidth[col]));
-                eligible++;
-                colWidth[col] += diff;
+        for(int startslack = 0; x_slack > 0 && cols > 0 && startslack != x_slack;) {
+            int increment = max(1, x_slack / cols);
+            startslack = x_slack;
+            for(short col=0; col < cols; col++) {
+                // FIXME: double check this
+                int diff = min(min(colMaxWidth[col], colWidth[col] + increment) - colWidth[col], x_slack);
                 x_slack -= diff;
+                colWidth[col] += diff;
             }
-            slacklevel += increment;
-        }
+        }   
         //#end
 
-        // PHASE 3: assign childrens' actual sizes
+        // Phase 3: assign childrens' actual sizes
         for(Box child = getChild(0); child != null; child = child.nextSibling()) {
             if (!child.test(VISIBLE)) continue;
             int child_width, child_height, child_x, child_y;
@@ -350,8 +348,8 @@ public final class Box extends JSScope implements Scheduler.Task {
         }
 
         // cleanup
-        for(int i=0; i<cols; i++) { colWidth[i] = 0; colMaxWidth[i] = 0; }
-        for(int i=0; i<rows; i++) { rowHeight[i] = 0; rowMaxHeight[i] = 0; }
+        for(int i=0; i<cols; i++) { colWidth[i] = 0; colMaxWidth[i] = MAX_LENGTH; }
+        for(int i=0; i<rows; i++) { rowHeight[i] = 0; rowMaxHeight[i] = MAX_LENGTH; }
 
         for(Box child = getChild(0); child != null; child = child.nextSibling())
             if (test(VISIBLE))
@@ -687,6 +685,7 @@ public final class Box extends JSScope implements Scheduler.Task {
             fillcolor = newfillcolor;
         } else if(value instanceof JS) {
             texture = Picture.load((JS)value, this);
+            if (texture != null) perform();
         } else {
             throw new JSExn("fill must be null, a String, or a stream, not a " + value.getClass());
         }