resolve darcs stupidity
[org.ibex.core.git] / src / org / ibex / Box.java
index 6d74e69..c898d8e 100644 (file)
@@ -2,13 +2,16 @@
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.ibex;
 
-// FEATURE: reflow before allowing js to read from width/height 
-// FEATURE: fastpath for rows=1/cols=1
+// FIXME: are traps on x/y meaningful?
+// FIXME: if we trap on cols, then set rows to 0 (forcing cols to 1), does the cols trap get triggered?
+// FIXME: if we change min{width/height}, thereby forcing a change to max{min/height}, does a trap on those get triggered?
+// FIXME: trap on numchildren?  replaces ChildChanged?
+// FIXME: trap on visible, trigger when parent visibility changes
+
+// FIXME: ax/ay nonsense
+// FIXME: mouse move/release still needs to propagate to boxen in which the mouse was pressed and is still held down
+
 // FEATURE: mark to reflow starting with a certain child
-// FEATURE: separate mark_for_reflow and mark_for_resize
-// FEATURE: make all methods final
-// FEATURE: use a linked list for the "frontier" when packing
-// FEATURE:    or else have a way to mark a column "same as last one"?
 // FEATURE: reintroduce surface.abort
 
 import java.util.*;
@@ -24,32 +27,25 @@ import org.ibex.translators.*;
  *
  *  <p>The rendering process consists of four phases; each requires
  *     one DFS pass over the tree</p>
- *  <ol><li> <b>repacking</b>: children of a box are packed into columns
- *           and rows according to their colspan/rowspan attributes and
- *           ordering.
- *  <ol><li> <b>reconstraining</b>: Minimum and maximum sizes of columns are computed.
- *      <li> <b>resizing</b>: width/height and x/y positions of children
- *           are assigned, and PosChange/SizeChanges are triggered.
- *      <li> <b>repainting</b>: children draw their content onto the PixelBuffer.
+ *  <ol><li> <b>pack()</b>: each box sets its childrens' row/col
+ *  <ol><li> <b>constrain()</b>: contentwidth is computed
+ *      <li> <b>resize()</b>: width/height and x/y positions are set
+ *      <li> <b>render()</b>: children draw their content onto the PixelBuffer.
  *  </ol>
  *
- *  The first three passes together are called the <i>reflow</i> phase.
- *  Reflowing is done in a seperate pass since PosChanges and
- *  SizeChanges trigger an Surface.abort; if rendering were done in the same
- *  pass, rendering work done prior to the Surface.abort would be wasted.
+ *  The first three passes together are called the <i>reflow</i>
+ *  phase.  Reflowing is done in a seperate pass since SizeChanges
+ *  trigger a Surface.abort; if rendering were done in the same pass,
+ *  rendering work done prior to the Surface.abort would be wasted.
  */
 public final class Box extends JSScope implements Scheduler.Task {
 
     // Macros //////////////////////////////////////////////////////////////////////
 
-    //#define LENGTH int
-    //#define MARK_REPACK for(Box b2 = this; b2 != null && !b2.test(REPACK); b2 = b2.parent) b2.set(REPACK);
-    //#define MARK_REPACK_b for(Box b2 = b; b2 != null && !b2.test(REPACK); b2 = b2.parent) b2.set(REPACK);
-    //#define MARK_REPACK_parent for(Box b2 = parent; b2 != null && !b2.test(REPACK); b2 = b2.parent) b2.set(REPACK);
-    //#define MARK_REFLOW for(Box b2 = this; b2 != null && !b2.test(REFLOW); b2 = b2.parent) b2.set(REFLOW);
-    //#define MARK_REFLOW_b for(Box b2 = b; b2 != null && !b2.test(REFLOW); b2 = b2.parent) b2.set(REFLOW);
-    //#define MARK_RESIZE for(Box b2 = this; b2 != null && !b2.test(RESIZE); b2 = b2.parent) b2.set(RESIZE);
-    //#define MARK_RESIZE_b for(Box b2 = b; b2 != null && !b2.test(RESIZE); b2 = b2.parent) b2.set(RESIZE);
+    final void REPLACE() { for(Box b2 = this; b2 != null && !b2.test(REPLACE); b2 = b2.parent) b2.set(REPLACE); }
+    final void RECONSTRAIN() { for(Box b2 = this; b2 != null && !b2.test(RECONSTRAIN); b2 = b2.parent) b2.set(RECONSTRAIN); }
+    final void REPACK() { for(Box b2 = this; b2 != null && !b2.test(REPACK); b2 = b2.parent) b2.set(REPACK); }
+
     //#define CHECKSET_SHORT(prop) short nu = (short)toInt(value); if (nu == prop) break; prop = nu;
     //#define CHECKSET_INT(prop) int nu = toInt(value); if (nu == prop) break; prop = nu;
     //#define CHECKSET_FLAG(flag) boolean nu = toBoolean(value); if (nu == test(flag)) break; if (nu) set(flag); else clear(flag);
@@ -58,37 +54,11 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     protected Box() { super(null); }
 
-    static Hash boxToCursor = new Hash(500, 3);               // FIXME memory leak
-    public static final int MAX_LENGTH = Integer.MAX_VALUE;
-    static final Font DEFAULT_FONT;
-   
-    static {
-        Font f = null;
-        try { f = Font.getFont((Stream)Main.builtin.get("fonts/vera/Vera.ttf"), 10); }
-        catch(JSExn e) { Log.info(Box.class, "should never happen: "+e); }
-        DEFAULT_FONT = f;
-    }
+    // FIXME memory leak
+    static Hash boxToCursor = new Hash(500, 3);
+
+    static final Font DEFAULT_FONT = Font.getFont((Stream)Main.builtin.get("fonts/vera/Vera.ttf"), 10);
 
-    // FIXME update these
-    // box properties can not be trapped
-    static final String[] props = new String[] {
-        "shrink", "hshrink", "vshrink", "x", "y", "width", "height", "cols", "rows",
-        "colspan", "rowspan", "align", "visible", "packed", "globalx", "globaly",
-        "minwidth", "maxwidth", "minheight", "maxheight", "indexof", "thisbox", "clip",
-        "numchildren", "redirect", "cursor", "mouse"
-    };
-
-    // FIXME update these
-    // events can have write traps, but not read traps
-    static final String[] events = new String[] {
-        "Press1", "Press2", "Press3",
-        "Release1", "Release2", "Release3",
-        "Click1", "Click2", "Click3",
-        "DoubleClick1", "DoubleClick2", "DoubleClick3",
-        "Enter", "Leave", "Move", "ChildChange",
-        "KeyPressed", "KeyReleased", "SizeChange",
-        "Focused", "Maximized", "Minimized", "Close"
-    };
 
     // Flags //////////////////////////////////////////////////////////////////////
 
@@ -105,9 +75,9 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     static final int ISROOT       = 0x00000080;
     static final int REPACK       = 0x00000100;
-    static final int REFLOW       = 0x00000200;
-    static final int RESIZE       = 0x00000400;
-    static final int RECONSTRAIN  = 0x00000800;
+    static final int RECONSTRAIN  = 0x00000200;
+    static final int REPLACE      = 0x00000400;
+
     static final int ALIGN_TOP    = 0x00001000;
     static final int ALIGN_BOTTOM = 0x00002000;
     static final int ALIGN_LEFT   = 0x00004000;
@@ -123,7 +93,7 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     Box parent = null;
     Box redirect = this;
-    int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */ | STOP_UPWARD_PROPAGATION | CLIP | MOVED;
+    int flags = VISIBLE | PACKED | REPACK | RECONSTRAIN | REPLACE | FIXED | STOP_UPWARD_PROPAGATION | CLIP | MOVED;
 
     private String text = null;
     private Font font = DEFAULT_FONT; 
@@ -135,10 +105,10 @@ public final class Box extends JSScope implements Scheduler.Task {
     private int aspect = 0;
 
     // specified directly by user
-    public LENGTH minwidth = 0;
-    public LENGTH maxwidth = MAX_LENGTH;
-    public LENGTH minheight = 0;
-    public LENGTH maxheight = MAX_LENGTH;
+    public int minwidth = 0;
+    public int maxwidth = Integer.MAX_VALUE;
+    public int minheight = 0;
+    public int maxheight = Integer.MAX_VALUE;
     private short rows = 1;
     private short cols = 0;
     private short rowspan = 1;
@@ -147,14 +117,14 @@ public final class Box extends JSScope implements Scheduler.Task {
     // computed during reflow
     private short row = 0;
     private short col = 0;
-    public LENGTH x = 0;
-    public LENGTH y = 0;
-    public LENGTH ax = 0;   // FEATURE: roll these into x/y; requires lots of changes
-    public LENGTH ay = 0;   // FEATURE: roll these into x/y; requires lots of changes; perhaps y()?
-    public LENGTH width = 0;
-    public LENGTH height = 0;
-    private LENGTH contentwidth = 0;      // == max(minwidth, textwidth, sum(child.contentwidth))
-    private LENGTH contentheight = 0;
+    public int x = 0;
+    public int y = 0;
+    public int ax = 0;   // FEATURE: roll these into x/y; requires lots of changes
+    public int ay = 0;   // FEATURE: roll these into x/y; requires lots of changes; perhaps y()?
+    public int width = 0;
+    public int height = 0;
+    private int contentwidth = 0;      // == max(minwidth, textwidth, sum(child.contentwidth))
+    private int contentheight = 0;
 
     /*
     private VectorGraphics.VectorPath path = null;
@@ -163,6 +133,8 @@ public final class Box extends JSScope implements Scheduler.Task {
     private VectorGraphics.Affine rtransform = null;
     */
 
+    //#define DIRTY dirty()
+
     // Instance Methods /////////////////////////////////////////////////////////////////////
 
     public final int fontSize() { return font == null ? DEFAULT_FONT.pointsize : font.pointsize; }
@@ -170,7 +142,10 @@ public final class Box extends JSScope implements Scheduler.Task {
     /** invoked when a resource needed to render ourselves finishes loading */
     public void perform() throws JSExn {
         if (texture == null) { Log.warn(Box.class, "perform() called with null texture"); return; }
-        if (texture.isLoaded) { setMinWidth(max(texture.width, maxwidth)); setMinHeight(max(texture.height, maxheight)); }
+        if (texture.isLoaded) {
+            setWidth(max(texture.width, maxwidth), maxwidth); 
+            setHeight(max(texture.height, maxheight), maxheight); 
+            DIRTY; }
         else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res.unclone()); }
     }
 
@@ -196,11 +171,17 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     // Reflow ////////////////////////////////////////////////////////////////////////////////////////
 
+    /** should only be invoked on the root box */
+    void reflow() { pack(); resize(x, y, maxwidth, maxheight); place(); }
+
     private static Box[] frontier = new Box[65535];
     /** pack the boxes into rows and columns, compute contentwidth */
     void pack() {
-        for(Box child = getChild(0); child != null; child = child.nextSibling()) child.pack();
-        int frontier_size = 0; contentwidth = 0; contentheight = 0;
+        if (!test(REPACK)) { constrain(); return; }
+        boolean haskid = false;
+        for(Box child = getChild(0); child != null; child = child.nextSibling()) { haskid = true; child.pack(); }
+        if (!haskid) { clear(REPACK); constrain(); return; }
+        int frontier_size = 0;
         //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan \
         //        contentheight/contentwidth contentwidth/contentheight
         if (test(FIXED) == COLS) {
@@ -228,6 +209,13 @@ public final class Box extends JSScope implements Scheduler.Task {
             for(int i=0; i<frontier_size; i++){ rows=(short)max(rows, frontier[i].row + frontier[i].rowspan); frontier[i] = null; }
         }
         //#end
+        clear(REPACK);
+        set(RECONSTRAIN);   // FIXME: be smarter / more incremental
+        constrain();
+    }
+
+    public void constrain() {
+        if (!test(RECONSTRAIN)) return;
         solve(true);
         //#repeat contentwidth/contentheight contentheight/contentwidth minwidth/minheight row/col col/row \
         //        textwidth/textheight maxwidth/maxheight cols/rows rows/cols colspan/rowspan rowspan/colspan
@@ -235,70 +223,44 @@ public final class Box extends JSScope implements Scheduler.Task {
                              max(contentwidth, font == null || text == null ? 0 : font.textwidth(text)),
                              maxwidth);
         //#end
+        set(REPLACE); // FIXME: be smarter / more incremental
     }
     
-    void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
+    void resize(int x, int y, int width, int height) {
         if (x == this.x && y == this.y && width == this.width && height == this.height) return;
         boolean sizechange = (this.width != width || this.height != height) && getTrap("SizeChange") != null;
-        try {
-            int thisx = parent == null ? 0 : this.x;
-            int thisy = parent == null ? 0 : this.y;
-            if (this.x != x || this.y != y) set(MOVED);
-            if (texture == null && (text == null || text.equals("")) && !test(MOVED)) {
-                if ((fillcolor & 0xff000000) != 0) {
-                    Box who = (parent == null ? this : parent);
-                    who.dirty(thisx+min(this.width,width), thisy, Math.abs(width-this.width), max(this.height, height));
-                    who.dirty(thisx, thisy+min(this.height,height), min(this.width, width), Math.abs(height-this.height));
-                }
-                //return;
+        int thisx = parent == null ? 0 : this.x;
+        int thisy = parent == null ? 0 : this.y;
+        Box who = (parent == null ? this : parent);
+        if (this.x != x || this.y != y) set(MOVED);
+        if (texture == null && (text == null || text.equals("")) && !test(MOVED)) {
+            if ((fillcolor & 0xff000000) != 0 || parent == null) {
+                who.dirty(thisx+min(this.width,width), thisy, Math.abs(width-this.width), max(this.height, height));
+                who.dirty(thisx, thisy+min(this.height,height), max(this.width, width), Math.abs(height-this.height));
             }
-            (parent == null ? this : parent).dirty(thisx, thisy, this.width, this.height);
             this.width = width; this.height = height; this.x = x; this.y = y;
-            dirty();
-        } finally {
+        } else {
+            who.dirty(thisx, thisy, this.width, this.height);
             this.width = width; this.height = height; this.x = x; this.y = y;
-            if (sizechange) putAndTriggerTrapsAndCatchExceptions("SizeChange", T);
+            DIRTY;
         }
+        if (sizechange) putAndTriggerTrapsAndCatchExceptions("SizeChange", T);
     }
 
-    private static float[] coeff = null;
-    private static Simplex lp_h = new Simplex(65535, 65535, 65535);
-    private static Simplex lp = new Simplex(65535, 65535, 65535);
-
+    private float targetColumnSize = (float)0.0;
+    private float targetRowSize = (float)0.0;
+    private static float[] sizes = new float[65535];
+    private static float[] sizes_v = new float[65535];
     private static int[] regions = new int[65535];
     private static int[] regions_v = new int[65535];
     private static int numregions = 0;
     private static int numregions_v = 0;
-    //#repeat col/row colspan/rowspan contentwidth/contentheight width/height HSHRINK/VSHRINK numregions/numregions_v \
-    //        maxwidth/maxheight cols/rows minwidth/minheight lp_h/lp lp_h/lp easy_width/easy_height regions/regions_v \
-    //        computeRegions/computeRegions_v
-    private void computeRegions() {
-        numregions = 0;
-        for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) {
-            regions[numregions++] = c.col;
-            regions[numregions++] = min(cols, c.col+c.colspan);
-        }
-        Vec.sortInts(regions, 0, numregions);
-        int j = 0;
-        int newnumregions = numregions;
-        for(int i=1; i<numregions; i++) {
-            if (regions[j] != regions[i]) j++;
-            else newnumregions--;
-            regions[j] = regions[i];
-        }
-        numregions = newnumregions;
-        if (regions[numregions-1] == cols) numregions--;
-        else regions[numregions] = cols;
-    }
-    //#end
 
-    private static float[] sizes = new float[65535];
-    private static float[] sizes_v = new float[65535];
     void solve(boolean findMinimum) {
         int numkids = 0; for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) numkids++;
         //#repeat col/row colspan/rowspan contentwidth/contentheight width/height HSHRINK/VSHRINK numregions/numregions_v \
-        //        maxwidth/maxheight cols/rows minwidth/minheight lp_h/lp lp_h/lp easy_width/easy_height regions/regions_v \
-        //        computeRegions/computeRegions_v targetColumnSize/targetRowSize sizes/sizes_v
+        //        maxwidth/maxheight cols/rows minwidth/minheight regions/regions_v targetColumnSize/targetRowSize sizes/sizes_v \
+        //        HSHRINK/VSHRINK
         if (numkids == 0) {
             if (findMinimum) contentwidth = 0;
             else targetColumnSize = 0;
@@ -311,7 +273,26 @@ public final class Box extends JSScope implements Scheduler.Task {
                 targetColumnSize = width;
             }
         } else if (cols > 1) do {
-            computeRegions();
+
+            // FIXME: cache these?
+            // compute regions
+            numregions = 0;
+            for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) {
+                regions[numregions++] = c.col;
+                regions[numregions++] = min(cols, c.col+c.colspan);
+            }
+            Vec.sortInts(regions, 0, numregions);
+            int j = 0;
+            int newnumregions = numregions;
+            for(int i=1; i<numregions; i++) {
+                if (regions[j] != regions[i]) j++;
+                else newnumregions--;
+                regions[j] = regions[i];
+            }
+            numregions = newnumregions;
+            if (regions[numregions-1] == cols) numregions--;
+            else regions[numregions] = cols;
+
             int target = findMinimum ? 0 : Math.max(width, contentwidth);
             // priority 0: (inviolable) honor minwidths
             // priority 1: sum of columns no greater than parent
@@ -331,9 +312,10 @@ public final class Box extends JSScope implements Scheduler.Task {
                         if (regions[r+1] < child.col) continue;
                         if (regions[r] >= min(child.col+child.colspan,cols)) { minregion = r; break; }
                         total -= sizes[r];
+                        int child_maxwidth = child.test(HSHRINK)?child.contentwidth:child.maxwidth;
                         if (sizes[r] <= (float)(targetColumnSize*(regions[r+1]-regions[r])))
-                            if ((child.colspan * targetColumnSize) > (child.maxwidth + (float)0.5))
-                                sizes[r] = (float)Math.min(sizes[r], (regions[r+1]-regions[r])*(child.maxwidth/child.colspan));
+                            if ((child.colspan * targetColumnSize) > (child_maxwidth + (float)0.5))
+                                sizes[r] = (float)Math.min(sizes[r], (regions[r+1]-regions[r])*(child_maxwidth/child.colspan));
                         if ((child.colspan * targetColumnSize) < (child.contentwidth - (float)0.5))
                             sizes[r] = (float)Math.max(sizes[r], (regions[r+1]-regions[r])*(child.contentwidth/child.colspan));
                         total += sizes[r];
@@ -356,13 +338,12 @@ public final class Box extends JSScope implements Scheduler.Task {
         } while(false);
         //#end
     }
-    private float targetColumnSize = (float)0.0;
-    private float targetRowSize = (float)0.0;
 
     void place() {
         solve(false);
         for(Box child = getChild(0); child != null; child = child.nextSibling()) {
             if (!child.test(VISIBLE)) continue;
+            if (!child.test(REPLACE)) continue;
             int child_width, child_height, child_x, child_y;
             if (!child.test(PACKED)) {
                 child_width = child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - Math.abs(child.ax));
@@ -426,7 +407,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         if ((fillcolor & 0xFF000000) != 0x00000000 || parent == null)
             buf.fillTrapezoid(cx1, cx2, cy1, cx1, cx2, cy2, (fillcolor & 0xFF000000) == 0 ? 0xffffffff : fillcolor);
 
-        // FIXME: do aspect in here
         if (texture != null && texture.isLoaded)
             for(int x = globalx; x < cx2; x += texture.width)
                 for(int y = globaly; y < cy2; y += texture.height)
@@ -473,18 +453,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
 
-    protected boolean isTrappable(Object key, boolean isRead) {
-        if (key == null) return false;
-        else if (key instanceof String) {
-            // not allowed to trap box properties, and no read traps on events
-            String name = (String)key;
-            for (int i=0; i < props.length; i++) if (name.equals(props[i])) return false; 
-            if (isRead) for (int i=0; i < events.length; i++) if (name.equals(events[i])) return false; 
-        }
-
-        return true;
-    }
-
     public Object get(Object name) throws JSExn {
         if (name instanceof Number)
             return redirect == null ? null : redirect == this ? getChild(toInt(name)) : redirect.get(name);
@@ -513,8 +481,8 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "rows": return test(FIXED) == ROWS ? N(rows) : N(0);
         case "colspan": return N(colspan);
         case "rowspan": return N(rowspan);
-        case "width": return N(width);
-        case "height": return N(height);
+        case "width": getRoot().reflow(); return N(width);
+        case "height": getRoot().reflow(); return N(height);
         case "minwidth": return N(minwidth);
         case "maxwidth": return N(maxwidth);
         case "minheight": return N(minheight);
@@ -551,99 +519,87 @@ public final class Box extends JSScope implements Scheduler.Task {
         }
     }
 
-    public void setMinWidth(int minwidth) {
-        if (this.minwidth == minwidth) return;
-        MARK_RESIZE; MARK_REFLOW; MARK_REPACK; this.minwidth = minwidth;
-    }
-    public void setMinHeight(int minheight) {
-        if (this.minheight == minheight) return;
-        MARK_RESIZE; MARK_REFLOW; MARK_REPACK; this.minheight = minheight;
-    }
-    public void setMaxWidth(Object value) {
-        do { CHECKSET_INT(maxwidth); MARK_RESIZE; } while(false);
-        if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth; 
-    }
-    public void setMaxHeight(Object value) {
-        do { CHECKSET_INT(maxheight); MARK_RESIZE; } while(false);
-        if (parent == null && getSurface() != null) getSurface().pendingHeight = maxheight;
+    //#repeat setWidth/setHeight minwidth/minheight maxwidth/maxheight pendingWidth/pendingHeight
+    public void setWidth(int min, int max) {
+        // FIXME: deal with conflicting min/max
+        if (this.minwidth == min && this.maxwidth == max) return;
+        this.minwidth = min;
+        this.maxwidth = max;
+        RECONSTRAIN();
+        if (parent != null || getSurface() == null) return;
+        getSurface().pendingWidth = maxwidth;
+
+        // FIXME: the repeat doesn't work right here
+        getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
     }
+    //#end
 
     public void put(Object name, Object value) throws JSExn {
         if (name instanceof Number) { put(toInt(name), value); return; }
         //#switch(name)
-        case "text": CHECKSET_STRING(text); MARK_RESIZE; dirty();
-        case "strokecolor": value = N(stringToColor((String)value)); CHECKSET_INT(strokecolor); MARK_RESIZE; dirty();
-        case "textcolor": value = N(stringToColor((String)value)); CHECKSET_INT(strokecolor); MARK_RESIZE; dirty();
-        case "text": CHECKSET_STRING(text); MARK_RESIZE; dirty();
-        case "strokewidth": CHECKSET_SHORT(strokewidth); dirty();
-        case "shrink": put("hshrink", value); put("vshrink", value);
-        case "hshrink": CHECKSET_FLAG(HSHRINK); MARK_RESIZE;
-        case "vshrink": CHECKSET_FLAG(VSHRINK); MARK_RESIZE;
-        case "width": put("maxwidth", value); put("minwidth", value); MARK_RESIZE;
-        case "height": put("maxheight", value); put("minheight", value); MARK_RESIZE;
-        case "maxwidth": setMaxWidth(value);
-        case "minwidth": CHECKSET_INT(minwidth); MARK_RESIZE;
-                         if (parent == null && getSurface() != null)
-                             getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
-        case "maxheight": setMaxHeight(value);
-        case "minheight": CHECKSET_INT(minheight); MARK_RESIZE;
-                         if (parent == null && getSurface() != null)
-                             getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
-        case "colspan": if (toInt(value) <= 0) return; CHECKSET_SHORT(colspan); MARK_REPACK_parent;
-        case "rowspan": if (toInt(value) <= 0) return; CHECKSET_SHORT(rowspan); MARK_REPACK_parent;
-        case "rows": CHECKSET_SHORT(rows); if (rows==0){set(FIXED, COLS);if(cols==0)cols=1;} else set(FIXED, ROWS); MARK_REPACK;
-        case "cols": CHECKSET_SHORT(cols); if (cols==0){set(FIXED, ROWS);if(rows==0)rows=1;} else set(FIXED, COLS); MARK_REPACK;
-        case "clip": CHECKSET_FLAG(CLIP); if (parent == null) dirty(); else parent.dirty();
-        case "visible": CHECKSET_FLAG(VISIBLE); dirty(); MARK_RESIZE; dirty();
-        case "packed": CHECKSET_FLAG(PACKED); MARK_REPACK_parent;
-        case "aspect": CHECKSET_INT(aspect); dirty();
-        case "globalx": put("x", N(globalToLocalX(toInt(value))));
-        case "globaly": put("y", N(globalToLocalY(toInt(value))));
-        case "align": clear(ALIGNS); setAlign(value == null ? "center" : value); MARK_RESIZE;
-        case "cursor": setCursor(value);
-        case "fill": setFill(value);
+        case "thisbox":     if (value == null) removeSelf();
+        case "text":        if (value == null) value = ""; CHECKSET_STRING(text); RECONSTRAIN(); DIRTY;
+        case "strokecolor": value = N(stringToColor((String)value)); CHECKSET_INT(strokecolor); DIRTY;
+        case "textcolor":   value = N(stringToColor((String)value)); CHECKSET_INT(strokecolor); DIRTY;
+        case "strokewidth": CHECKSET_SHORT(strokewidth); DIRTY;
+        case "shrink":      CHECKSET_FLAG(HSHRINK | VSHRINK); RECONSTRAIN();
+        case "hshrink":     CHECKSET_FLAG(HSHRINK); RECONSTRAIN();
+        case "vshrink":     CHECKSET_FLAG(VSHRINK); RECONSTRAIN();
+        case "width":       setWidth(toInt(value), toInt(value));
+        case "height":      setHeight(toInt(value), toInt(value));
+        case "maxwidth":    setWidth(minwidth, toInt(value));
+        case "minwidth":    setWidth(toInt(value), maxwidth);
+        case "maxheight":   setHeight(minheight, toInt(value));
+        case "minheight":   setHeight(toInt(value), maxheight);
+        case "colspan":     if (toInt(value) > 0) { CHECKSET_SHORT(colspan); if (parent != null) parent.REPACK(); }
+        case "rowspan":     if (toInt(value) > 0) { CHECKSET_SHORT(rowspan); if (parent != null) parent.REPACK(); }
+        case "visible":     CHECKSET_FLAG(VISIBLE); RECONSTRAIN(); DIRTY;
+        case "packed":      CHECKSET_FLAG(PACKED); if (parent != null) { parent.REPACK(); } else { REPACK(); }
+        case "align":       clear(ALIGNS); setAlign(value == null ? "center" : value); REPLACE();
+        case "cursor":      setCursor(value);
+        case "fill":        setFill(value);
+        case "clip":        CHECKSET_FLAG(CLIP); if (parent == null) DIRTY; else parent.DIRTY;
+        case "rows": CHECKSET_SHORT(rows); if (rows==0){set(FIXED, COLS);if(cols==0)cols=1;} else set(FIXED, ROWS); REPACK();
+        case "cols": CHECKSET_SHORT(cols); if (cols==0){set(FIXED, ROWS);if(rows==0)rows=1;} else set(FIXED, COLS); REPACK();
+
+        // FIXME: remove
         case "mouse":
             int mousex = toInt(((JS)value).get("x"));
             int mousey = toInt(((JS)value).get("y"));
             getSurface()._mousex = localToGlobalX(mousex);
             getSurface()._mousey = localToGlobalY(mousey);
+
         case "Minimized": if (parent == null && getSurface() != null) getSurface().minimized = toBoolean(value);  // FEATURE
         case "Maximized": if (parent == null && getSurface() != null) getSurface().maximized = toBoolean(value);  // FEATURE
-        case "Close": if (parent == null && getSurface() != null) getSurface().dispose(true);
+        case "Close":     if (parent == null && getSurface() != null) getSurface().dispose(true);
         case "redirect":
-            if (value == null) { redirect = null; return; }
-            for(Box cur = (Box)value; cur != null; cur = cur.parent)
-                if (cur == redirect) {
-                    redirect = (Box)value;
-                    return;
-                }
+            for(Box cur = (Box)value; cur != null || cur == redirect; cur = cur.parent)
+                if (cur == redirect) { redirect = (Box)value; return; }
             JS.error("redirect can only be set to a descendant of its current value");
+        case "fontsize": font = Font.getFont(font == null ? null : font.stream, toInt(value)); RECONSTRAIN(); DIRTY;
         case "font":
             if(!(value instanceof Stream)) throw new JSExn("You can only put streams to the font property");
+            if (font == value) return;  // FIXME: unclone()
             font = value == null ? null : Font.getFont((Stream)value, font == null ? 10 : font.pointsize);
-            MARK_RESIZE;
-            dirty();
-        case "fontsize": font = Font.getFont(font == null ? null : font.stream, toInt(value)); MARK_RESIZE; dirty();
+            RECONSTRAIN();
+            DIRTY;
         case "x": if (parent==null && Surface.fromBox(this)!=null) {
             CHECKSET_INT(x);
         } else {
             if (test(PACKED) && parent != null) return;
-            dirty(); CHECKSET_INT(ax);
-            dirty(); MARK_RESIZE;
-            dirty();
+            CHECKSET_INT(ax);
+            REPLACE();
         }
         case "y": if (parent==null && Surface.fromBox(this)!=null) {
             CHECKSET_INT(y);
         } else {
             if (test(PACKED) && parent != null) return;
-            dirty(); CHECKSET_INT(ay);
-            dirty(); MARK_RESIZE;
-            dirty();
+            CHECKSET_INT(ay);
+            REPLACE();
         }
-        case "titlebar":
-            if (getSurface() != null && value != null) getSurface().setTitleBarText(JS.toString(value));
-            super.put(name,value);
-            
+        case "titlebar": if (getSurface()!=null) getSurface().setTitleBarText(toString(value)); super.put(name,value);
+        // FIXME: icon
+
         case "Press1":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "Press2":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "Press3":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
@@ -659,7 +615,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "KeyPressed":    if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "KeyReleased":   if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "Move":          if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
-
         case "HScroll":       if (!test(STOP_UPWARD_PROPAGATION) && parent != null)
             parent.putAndTriggerTraps(name, N(((Number)value).floatValue() * ((float)parent.fontSize()) / ((float)fontSize())));
         case "VScroll":       if (!test(STOP_UPWARD_PROPAGATION) && parent != null)
@@ -684,12 +639,10 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "_VScroll":      propagateDownward(name, value, false);
 
         case "SizeChange":    return;
-        case "ChildChange":    return;
+        case "ChildChange":   return;
         case "Enter":         return;
         case "Leave":         return;
 
-        case "thisbox":       if (value == null) removeSelf();
-
         default:              super.put(name, value);
         //#end
     }
@@ -710,8 +663,8 @@ public final class Box extends JSScope implements Scheduler.Task {
     }
 
     private void setAlign(Object value) {
+        clear(ALIGNS);
         //#switch(value)
-        case "center": clear(ALIGNS);
         case "topleft": set(ALIGN_TOP | ALIGN_LEFT);
         case "bottomleft": set(ALIGN_BOTTOM | ALIGN_LEFT);
         case "topright": set(ALIGN_TOP | ALIGN_RIGHT);
@@ -730,33 +683,34 @@ public final class Box extends JSScope implements Scheduler.Task {
         set(CURSOR);
         boxToCursor.put(this, value);
         Surface surface = getSurface();
-        if (surface != null) {
-            String tempcursor = surface.cursor;
-            propagateDownward(null, null, false);
-            if (surface.cursor != tempcursor) surface.syncCursor();
-        }
+        if (surface == null) return;
+        String tempcursor = surface.cursor;
+        propagateDownward(null, null, false);
+        if (surface.cursor != tempcursor) surface.syncCursor();
     }
 
     private void setFill(Object value) throws JSExn {
         if (value == null) {
-            // FIXME: Check this... does this make it transparent? 
+            if (texture == null && fillcolor == 0) return;
             texture = null;
             fillcolor = 0;
         } else if (value instanceof String) {
-            // FIXME check double set
             int newfillcolor = stringToColor((String)value);
             if (newfillcolor == fillcolor) return;
             fillcolor = newfillcolor;
-        } else if(value instanceof JS) {
-            texture = Picture.load((JS)value, this);
+            texture = null;
+        } else if (value instanceof JS) {
+            Picture newtex = Picture.load((JS)value, this);
+            if (texture == newtex) return;
+            texture = newtex;
+            fillcolor = 0;
             if (texture != null && texture.isLoaded) perform();
         } else {
             throw new JSExn("fill must be null, a String, or a stream, not a " + value.getClass());
         }
-        dirty();
+        DIRTY;
     }
 
-    // FIXME: mouse move/release still needs to propagate to boxen in which the mouse was pressed and is still held down
     /**
      *  Handles events which propagate down the box tree.  If obscured
      *  is set, then we merely check for Enter/Leave.
@@ -865,7 +819,7 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
 
-    void mark_for_repack() { MARK_REPACK; }
+    void mark_for_repack() { REPACK(); }
     public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); }
     public Box getRoot() { return parent == null ? this : parent.getRoot(); }
     public Surface getSurface() { return Surface.fromBox(getRoot()); }
@@ -917,12 +871,12 @@ public final class Box extends JSScope implements Scheduler.Task {
     /** remove the i^th child */
     public void removeChild(int i) {
         Box b = getChild(i);
-        MARK_REFLOW_b;
-        b.dirty();
+        b.RECONSTRAIN();
+        b.DIRTY;
         b.clear(MOUSEINSIDE);
         deleteNode(i);
         b.parent = null;
-        MARK_REFLOW;
+        RECONSTRAIN();
         putAndTriggerTrapsAndCatchExceptions("ChildChange", b);
     }
     
@@ -975,10 +929,10 @@ public final class Box extends JSScope implements Scheduler.Task {
             b.parent = this;
             
             // need both of these in case child was already uncalc'ed
-            MARK_REFLOW_b;
-            MARK_REFLOW;
+            b.RECONSTRAIN();
+            RECONSTRAIN();
             
-            b.dirty(); 
+            b.DIRTY; 
             putAndTriggerTrapsAndCatchExceptions("ChildChange", b);
         }
     }
@@ -1002,45 +956,3 @@ public final class Box extends JSScope implements Scheduler.Task {
 
 
 
-        /*
-        offset_x = 0;
-        if (path != null) {
-            if (rpath == null) rpath = path.realize(transform == null ? VectorGraphics.Affine.identity() : transform);
-            if ((flags & HSHRINK) != 0) contentwidth = max(contentwidth, rpath.boundingBoxWidth());
-            if ((flags & VSHRINK) != 0) contentheight = max(contentheight, rpath.boundingBoxHeight());
-            // FIXME: separate offset_x needed for the path
-        }
-        // #repeat x1/y1 x2/y2 x3/y3 x4/y4 contentwidth/contentheight left/top right/bottom
-        int x1 = transform == null ? 0 : (int)transform.multiply_px(0, 0);
-        int x2 = transform == null ? 0 : (int)transform.multiply_px(contentwidth, 0);
-        int x3 = transform == null ? contentwidth : (int)transform.multiply_px(contentwidth, contentheight);
-        int x4 = transform == null ? contentwidth : (int)transform.multiply_px(0, contentheight);
-        int left = min(min(x1, x2), min(x3, x4));
-        int right = max(max(x1, x2), max(x3, x4));
-        contentwidth = max(contentwidth, right - left);
-        offset_x = -1 * left;
-        // #end
-        */
-
-
-                    /*
-        if (path != null) {
-            if (rtransform == null) rpath = null;
-            else if (!rtransform.equalsIgnoringTranslation(a)) rpath = null;
-            else {
-                rpath.translate((int)(a.e - rtransform.e), (int)(a.f - rtransform.f));
-                rtransform = a.copy();
-            }
-            if (rpath == null) rpath = path.realize((rtransform = a) == null ? VectorGraphics.Affine.identity() : a);
-            if ((strokecolor & 0xff000000) != 0) rpath.stroke(buf, 1, strokecolor);
-            if ((fillcolor & 0xff000000) != 0) rpath.fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
-        }
-*/
-
-
-/*
-            VectorGraphics.Affine a2 = VectorGraphics.Affine.translate(b.x, b.y);
-            if (transform != null) a2.multiply(transform);
-            a2.multiply(VectorGraphics.Affine.translate(offset_x, offset_y));
-            a2.multiply(a);
-*/