2003/11/16 02:40:43
[org.ibex.core.git] / src / org / xwt / Box.java
index 475b380..3ac48d4 100644 (file)
@@ -93,7 +93,7 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
 
     Box parent = null;
     Box redirect = this;
-    int flags = VISIBLE | PACKED;
+    int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */;
 
     private String text = null;
     private Font font = null;
@@ -104,9 +104,9 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
 
     // specified directly by user
     public LENGTH minwidth = 0;
-    public LENGTH maxwidth = 0;
+    public LENGTH maxwidth = MAX_LENGTH;
     public LENGTH minheight = 0;
-    public LENGTH maxheight = 0;
+    public LENGTH maxheight = MAX_LENGTH;
     private short rows = 1;
     private short cols = 0;
     private short rowspan = 1;
@@ -152,8 +152,9 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
         }
     }
 
-    public void putAndTriggerTraps(Object key, Object value) {
-        // FIXME
+    // fixme
+    public void putAndTriggerJSTraps(Object key, Object value) {
+        put(key, value);
     }
 
     /** update MOUSEINSIDE, check for Enter/Leave/Move */
@@ -164,7 +165,7 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
         if (isinside) set(MOUSEINSIDE); else clear(MOUSEINSIDE);
         if (!wasinside && !isinside) return;
         
-        if (isinside && test(CURSOR)) getRoot().cursor = (String)boxToCursor.get(this);
+        if (isinside && test(CURSOR)) Surface.fromBox(getRoot()).cursor = (String)boxToCursor.get(this);
         if (!wasinside && isinside && getTrap("Enter") != null) putAndTriggerJSTraps("Enter", T);
         else if (wasinside && !isinside && getTrap("Leave") != null) putAndTriggerJSTraps("Leave", T);
         else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && getTrap("Move")!= null)
@@ -180,10 +181,11 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
 
     // static stuff so we don't have to keep reallocating
     private static int[] numRowsInCol = new int[65535];
-    private LENGTH[] colWidth = new LENGTH[65535];
-    private LENGTH[] colMaxWidth = new LENGTH[65535];
-    private LENGTH[] rowHeight = new LENGTH[65535];
-    private LENGTH[] rowMaxHeight = new LENGTH[65535];
+    private static LENGTH[] colWidth = new LENGTH[65535];
+    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] = MAX_LENGTH; colMaxWidth[i] = MAX_LENGTH; } }
 
     final Box nextPackedSibling() { Box b = nextSibling(); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); }
     final Box firstPackedChild() { Box b = getChild(0); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); }
@@ -191,9 +193,12 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
     /** only for use on the root box */
     void reflow(int new_width, int new_height) {
         repack();
+        /*
         new_width = bound(max(contentwidth, minwidth), new_width, test(HSHRINK) ? max(contentwidth, minwidth) : maxwidth);
         new_height = bound(max(contentheight, minheight), new_height, test(VSHRINK) ? max(contentheight, minheight) : maxheight);
+        */
         resize(x, y, new_width, new_height);
+        resize_children();
     }
 
     /** pack the boxes into rows and columns; also computes contentwidth */
@@ -202,17 +207,18 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
 
         //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan 
         if (test(FIXED) == COLS) {
-            short r = 0; short rows = 0;
-            for(Box child = firstPackedChild(); child != null; r++)
+            short r = 0;
+            for(Box child = firstPackedChild(); child != null; r++) {
                 for(short col=0, numclear=0; child != null && col < cols;) {
                     if (numRowsInCol[col] > r) continue;
                     if (col != 0 && col + min(cols, child.colspan) > cols) break;
                     if (++numclear < min(cols, child.colspan)) continue;
                     for(int i=col - numclear + 1; i <= col; i++) numRowsInCol[i] += child.rowspan;
                     child.col = col; child.row = r;
-                    child = child.nextPackedSibling();
                     rows = (short)max(rows, child.row + child.rowspan);
+                    child = child.nextPackedSibling();
                 }
+            }
             for(int i=0; i<cols; i++) numRowsInCol[i] = 0;
         }
         //#end
@@ -224,78 +230,86 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
             colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
         for(int i=0; i<cols; i++) { contentwidth += colWidth[i]; colWidth[i] = 0; }
 
-        contentwidth = bound(minwidth, max(font.textwidth(text), contentwidth), maxwidth);
+        contentwidth = bound(minwidth, max(font == null || text == null ? 0 : font.textwidth(text), contentwidth), maxwidth);
         //#end               
     }
     
     private void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
         // FEATURE reimplement, but we're destroying this
-        /*
         if (x != this.x || y != this.y || width != this.width || height != this.height) {
-        */
             (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
             boolean sizechange = (this.width != width || this.height != height) && getTrap("SizeChange") != null;
             boolean poschange = (this.x != x || this.y != y) && getTrap("PosChange") != null;
             this.width = width; this.height = height; this.x = x; this.y = y;
             dirty();
-            try { if (sizechange) putAndTriggerJSTraps("SizeChange", T); Surface.abort = true; }
+            try { if (sizechange) putAndTriggerJSTraps("SizeChange", T); /*Surface.abort = true;*/ }
             catch (Exception e) { Log.log(this, e); }
-            try { if (poschange) pputAndTriggerJSTraps("PosChange", T); Surface.abort = true; }
+            try { if (poschange) putAndTriggerJSTraps("PosChange", T); /*Surface.abort = true;*/ }
             catch (Exception e) { Log.log(this, e); }
-            /*
         }
-            */
-        if (numchildren > 0) resize_children();
     }
 
     private void resize_children() {
-        int slack;
-        //#repeat col/row colspan/rowspan contentwidth/contentheight x/y width/height \
-        //        HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight
 
+        //#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
-        slack = 0;
+        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++) {
-                slack += colWidth[i];
+                x_slack += colWidth[i];
                 colWidth[i] = max(colWidth[i], child.contentwidth / child.colspan);
-                slack -= colWidth[i];
-                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
-        for(int startslack = 0; slack > 0 && startslack != slack;) {
-            int increment = max(1, slack / cols);
-            startslack = slack;
-            for(short col=0; col < cols && slack > 0; col++) {
+        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++) {
                 int diff = min(colMaxWidth[col], colWidth[col] + increment) - colWidth[col];
-                slack -= diff;
+                x_slack -= diff;
                 colWidth[col] += diff;
             }
         }   
+        //#end
 
-        for(Box child = getChild(0); child != null; child = child.nextPackedSibling()) {
-            int unbounded = 0;
-            for(int i = child.col; i < child.col + child.colspan; i++) unbounded += colWidth[i];
-            child.width = bound(child.contentwidth, unbounded, child.test(HSHRINK) ? child.contentwidth : child.maxwidth);
-            child.x = test(ALIGN_RIGHT) ? slack : test(ALIGN_LEFT) ? slack / 2 : 0;
-            for(int i=0; i < child.col; i++) child.x += colWidth[i];
-            if (child.width < unbounded) child.x += (child.width - unbounded) / 2;
+        // 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;
+            if (!child.test(PACKED)) {
+                child_x = child.x;
+                child_y = child.y;
+                child_width = child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - child.x);
+                child_height = child.test(VSHRINK) ? child.contentheight : min(child.maxheight, height - child.y);
+                child_width = max(child.minwidth, child_width);
+                child_height = max(child.minheight, child_height);
+            } else {
+                int unbounded;
+                //#repeat col/row colspan/rowspan contentwidth/contentheight width/height colMaxWidth/rowMaxHeight \
+                //        child_x/child_y x/y HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight x_slack/y_slack \
+                //        colWidth/rowHeight child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP
+                unbounded = 0;
+                for(int i = child.col; i < child.col + child.colspan; i++) unbounded += colWidth[i];
+                child_width = bound(child.contentwidth, unbounded, child.test(HSHRINK) ? child.contentwidth : child.maxwidth);
+                child_x = test(ALIGN_RIGHT) ? x_slack : test(ALIGN_LEFT) ? 0 : x_slack / 2;
+                for(int i=0; i < child.col; i++) child_x += colWidth[i];
+                if (child_width > unbounded) child_x -= (child_width - unbounded) / 2;
+                //#end
+            }
+            child.resize(child_x, child_y, child_width, child_height);
         }
 
         // cleanup
-        for(int i=0; i<colWidth.length; i++) colWidth[i] = 0;
-        for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = MAX_LENGTH;
-        //#end
+        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; }
 
-        // Phase 3: assign childrens' actual sizes
         for(Box child = getChild(0); child != null; child = child.nextSibling())
-            if (!test(VISIBLE)) continue;
-            else if (!child.test(PACKED))
-                child.resize(child.x, child.y,
-                             child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - child.x),
-                             child.test(VSHRINK) ? child.contentheight : min(child.maxheight, height - child.y));
-            else child.resize(child.x, child.y, child.width, child.height);
+            if (test(VISIBLE))
+                child.resize_children();
     }
 
 
@@ -312,24 +326,24 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
         if (!test(NOCLIP)) {
             cx1 = max(cx1, parent == null ? 0 : globalx);
             cy1 = max(cy1, parent == null ? 0 : globaly);
-            cx2 = min(cx2, parent == null ? 0 : globalx + width);
-            cy2 = min(cy2, parent == null ? 0 : globaly + height);
+            cx2 = min(cx2, globalx + width);
+            cy2 = min(cy2, globaly + height);
             if (cx2 <= cx1 || cy2 <= cy1) return;
         }
 
         if ((fillcolor & 0xFF000000) != 0x00000000)
-            buf.fillJSTrapezoid(globalx, globalx + width, globaly, globalx, globalx + width, globaly + height, fillcolor);
+            buf.fillTrapezoid(globalx, globalx + width, globaly, globalx, globalx + width, globaly + height, fillcolor);
 
         if (texture != null)
             for(int x = globalx; x < cx2; x += texture.getWidth())
                 for(int y = globaly; y < cy2; y += texture.getHeight())
                     buf.drawPicture(texture, x, y, cx1, cy1, cx2, cy2);
-        
+
        if (text != null && !text.equals("") && font != null)
             if (font.rasterizeGlyphs(text, buf, strokecolor, globalx, globaly, cx1, cy1, cx2, cy2, null) == -1)
                 font.rasterizeGlyphs(text, buf, strokecolor, globalx, globaly, cx1, cy1, cx2, cy2,
                                     new Scheduler.Task() { public void perform() { Box b = Box.this; MARK_REFLOW_b; dirty(); }});
-                    
+
         for(Box b = getChild(0); b != null; b = b.nextSibling())
             b.render(globalx, globaly, cx1, cy1, cx2, cy2, buf, null);
     }
@@ -380,8 +394,8 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
         case "vshrink": return B(test(VSHRINK));
         case "x": return (parent == null || !test(VISIBLE)) ? N(0) : N(x);
         case "y": return (parent == null || !test(VISIBLE)) ? N(0) : N(y);
-        case "width" return N(width);
-        case "height" return N(height);
+        case "width": return N(width);
+        case "height": return N(height);
         case "cols": return test(FIXED) == COLS ? N(cols) : N(0);
         case "rows": return test(FIXED) == ROWS ? N(rows) : N(0);
         case "colspan": return N(colspan);
@@ -413,21 +427,24 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
 
         //#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 "thisbox": if (value == null) remove();
         case "shrink": put("hshrink", value); put("vshrink", value);
         case "hshrink": CHECKSET_FLAG(HSHRINK); MARK_RESIZE;
         case "vshrink": CHECKSET_FLAG(VSHRINK); MARK_RESIZE;
-        case "width": CHECKSET_INT(width); MARK_RESIZE;
+        case "width": if (parent==null&&Surface.fromBox(this)!=null) { CHECKSET_INT(width); } else { put("maxwidth", value); put("minwidth", value); MARK_RESIZE; }
+        case "height": if (parent == null&&Surface.fromBox(this)!=null) { CHECKSET_INT(height); } else { put("maxheight", value); put("minheight", value); MARK_RESIZE; }
         case "maxwidth": CHECKSET_INT(maxwidth); MARK_RESIZE;
         case "minwidth": CHECKSET_INT(minwidth); MARK_RESIZE;
-        case "height": CHECKSET_INT(height); MARK_RESIZE;
         case "maxheight": CHECKSET_INT(maxheight); MARK_RESIZE;
         case "minheight": CHECKSET_INT(minheight); MARK_RESIZE;
         case "colspan": CHECKSET_SHORT(colspan); MARK_REPACK_parent;
         case "rowspan": CHECKSET_SHORT(colspan); MARK_REPACK_parent;
-        case "rows": CHECKSET_SHORT(rows); MARK_REPACK;  // FEATURE: error checking
-        case "cols": CHECKSET_SHORT(cols); MARK_REPACK;  // FEATURE: error checking
+        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 "noclip": CHECKSET_FLAG(NOCLIP); if (parent == null) dirty(); else parent.dirty();
         case "visible": CHECKSET_FLAG(VISIBLE); dirty(); MARK_RESIZE; dirty();
         case "packed": CHECKSET_FLAG(PACKED); MARK_REPACK_parent;
@@ -509,7 +526,12 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
     }
 
     private void setFill(Object value) {
-        if (value == null || !(value instanceof Res)) return;
+        if (value == null) return;
+        if (value instanceof String) {
+            // FIXME check double set
+            fillcolor = stringToColor((String)value);
+        }
+        if (!(value instanceof Res)) return;
         Picture pic = Picture.fromRes((Res)value, null);
         if (pic != null) {
             texture = pic;
@@ -533,8 +555,8 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
         int mousex = globalToLocalX(surface.mousex);
         int mousey = globalToLocalY(surface.mousey);
         for(Box c = prevSibling(); c != null; c = c.prevSibling())
-            if (c.inside(mousex - c.x, mousey - c.y)) { JSTrap.putAndTriggerJSTraps(c, name, value); return; }
-        if (parent != null) JSTrap.putAndTriggerJSTraps(parent, name, value);
+            if (c.inside(mousex - c.x, mousey - c.y)) { c.putAndTriggerJSTraps(name, value); return; }
+        if (parent != null) parent.putAndTriggerJSTraps(name, value);
     }
 
     private static int stringToColor(String s) {
@@ -608,19 +630,23 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
     final boolean inside(int x, int y) { return test(VISIBLE) && x >= 0 && y >= 0 && x < width && y < height; }
 
     protected final void set(int mask) { flags |= mask; }
+    protected final void set(int mask, boolean setclear) { if (setclear) set(mask); else clear(mask); }
     protected final void clear(int mask) { flags &= ~mask; }
     protected final boolean test(int mask) { return ((flags & mask) == mask); }
     
     protected Box left = null;
     protected Box right = null;
-    protected Box parent_ = null;    // not to be confused with Box.parent_!
     protected Box rootChild = null;
+    protected Box peerTree_parent = null;
+    public abstract Box peerTree_leftmost();
+    public abstract Box peerTree_rightmost();
+    public abstract Box insertBeforeMe(Box cell);
+    public abstract Box insertAfterMe(Box cell);
     protected abstract Box fixAfterInsertion();
     protected abstract Box fixAfterDeletion();
-    protected abstract Box rightmost();
-    protected abstract Box leftmost();
     protected abstract Box rotateLeft();
     protected abstract Box rotateRight();
+    protected abstract int numPeerChildren();
 }