2003/11/16 02:40:43
[org.ibex.core.git] / src / org / xwt / Box.java
index 60f1fbd..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,7 +152,9 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
         }
     }
 
+    // fixme
     public void putAndTriggerJSTraps(Object key, Object value) {
+        put(key, value);
     }
 
     /** update MOUSEINSIDE, check for Enter/Leave/Move */
@@ -191,8 +193,10 @@ 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();
     }
@@ -203,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
@@ -225,7 +230,7 @@ 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 == null ? 0 : font.textwidth(text), contentwidth), maxwidth);
+        contentwidth = bound(minwidth, max(font == null || text == null ? 0 : font.textwidth(text), contentwidth), maxwidth);
         //#end               
     }
     
@@ -245,27 +250,27 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
     }
 
     private void resize_children() {
-        int slack;
 
-        //#repeat col/row colspan/rowspan contentwidth/contentheight x/y width/height colMaxWidth/rowMaxHeight \
-        //        HSHRINK/VSHRINK maxwidth/maxheight cols/rows minwidth/minheight colWidth/rowHeight
+        //#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;
             }
         }   
@@ -274,22 +279,27 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
         // 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.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));
-                continue;
+                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
             }
-            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 colWidth/rowHeight child_width/child_height
-            unbounded = 0;
-            for(int i = child.col; i < child.col + child.colspan; i++) unbounded += colWidth[i];
-            int child_width = bound(child.contentwidth, unbounded, child.test(HSHRINK) ? child.contentwidth : child.maxwidth);
-            int 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;
-            //#end
             child.resize(child_x, child_y, child_width, child_height);
         }
 
@@ -318,22 +328,22 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable {
             cy1 = max(cy1, parent == null ? 0 : globaly);
             cx2 = min(cx2, globalx + width);
             cy2 = min(cy2, globaly + height);
-            //if (cx2 <= cx1 || cy2 <= cy1) return;
+            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);
     }
@@ -417,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;
@@ -513,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;
@@ -612,6 +630,7 @@ 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); }