X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2FBox.java;h=13c02ee5b7036b01887ba015b62f1f4a065568d0;hb=80f694a9d56be6df76bb61d5409a4ab0bae6be86;hp=e8c3d35da608ca66af48de77f9f6fbfaad977d8f;hpb=66204595a29d1ac52c97211a6567d1f0b76234a9;p=org.ibex.core.git diff --git a/src/org/ibex/Box.java b/src/org/ibex/Box.java index e8c3d35..13c02ee 100644 --- a/src/org/ibex/Box.java +++ b/src/org/ibex/Box.java @@ -212,50 +212,59 @@ public final class Box extends JSScope implements Scheduler.Task { // Reflow //////////////////////////////////////////////////////////////////////////////////////// - // static stuff so we don't have to keep reallocating - private static int[] numRowsInCol = new int[65535]; - private static LENGTH[] colWidth = new LENGTH[65535]; - private static LENGTH[] colMinWidth = new LENGTH[65535]; - private static LENGTH[] colMaxWidth = new LENGTH[65535]; - private static LENGTH[] rowHeight = new LENGTH[65535]; - private static LENGTH[] rowMinHeight = new LENGTH[65535]; - private static LENGTH[] rowMaxHeight = new LENGTH[65535]; - static { for(int i=0; i r) { numclear = 0; continue; } - if (c != 0 && c + min(cols, child.colspan) - numclear > cols) break; - if (++numclear < min(cols, child.colspan)) continue; - for(int i=c - numclear + 1; i <= c; i++) numRowsInCol[i] += child.rowspan; - child.col = (short)(c - numclear + 1); child.row = r; - rows = (short)max(rows, child.row + child.rowspan); - child = child.nextPackedSibling(); - numclear = 0; + int childnum = 0; + Box lastpacked = null; + int maxfront = 0; + for(Box child = getChild(0); child != null; child = child.nextSibling(), childnum++) { + if (!(child.test(PACKED) && child.test(VISIBLE))) continue; + int col = lastpacked == null ? 0 : (lastpacked.col + lastpacked.colspan); + int row = lastpacked == null ? 0 : lastpacked.row; + int colspan = min(cols, child.colspan); + for(int i=0; i cols) { row++; col = 0; i = -1; continue; } + Box front = getChild(frontier[i]); // FIXME: O(nlgn) + if (front.row + front.rowspan <= row) { frontier[i] = frontier[maxfront-1]; maxfront--; i--; continue; } + if ((front.col <= col && front.col + front.colspan > col) || + (front.col < (col+colspan) && front.col + front.colspan >= (col+colspan))) { + col = front.col + front.colspan; + i = -1; + continue; + } + break; } + child.col = (short)col; + child.row = (short)row; + lastpacked = child; + frontier[maxfront++] = childnum; } - for(int i=0; i i)) + rowcontentwidth += child.contentwidth; + contentwidth = max(contentwidth, rowcontentwidth); } - for(int i=0; i 1) do { + /* boolean easy_width = contentwidth >= width; */ + + // FIXME: numboxes^2, and damn ugly to boot + for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) { + int target = c.col; + for(boolean stop = false;;) { + for(int i=0; i<=numregions; i++) { + if (i == numregions) { regions[numregions++] = target; break; } + if (target == regions[i]) break; + if (target < regions[i]) { int tmp = target; target = regions[i]; regions[i] = tmp; } + } + if (stop) break; + stop = true; + target = min(cols, c.col+c.colspan); + } } - - // PHASE 2: hand out slack - for(int startslack = 0; x_slack > 0 && cols > 0 && startslack != x_slack;) { - if (eligible == 0) break; - int increment = x_slack / eligible; - eligible = 0; - 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); - if (colWidth[col] + diff < colMinWidth[col]) diff = colMinWidth[col] - colWidth[col]; - if (diff == 0) continue; - eligible++; - x_slack -= diff; - colWidth[col] += diff; + if (regions[numregions-1] == cols) numregions--; + else regions[numregions] = cols; + + /* + for(Box c = firstPackedChild(); easy_width && c != null; c = c.nextPackedSibling()) { + if (c.contentwidth == c.maxwidth) continue; + if (c.maxwidth == Integer.MAX_VALUE) continue; + easy_width = false; + } + if (easy_width) for(int i=0; i i && c.maxwidth < Integer.MAX_VALUE) + good = false; + if (good) { easy_width = true; break; } + } + if (easy_width) break; + */ + int nc = numregions * 2 + numkids + 1; + if (coeff == null || nc+1>coeff.length) coeff = new float[nc+1]; + lp_h.init(nc); + + for(int i=0; i= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) + coeff[r] = (float)(regions[r+1] - regions[r]); + lp_h.add_constraint(coeff, LinearProgramming.GE, (float)child.contentwidth); + + // priority 2: honor maxwidths + int child_maxwidth = child.test(HSHRINK) ? min(child.maxwidth, child.contentwidth) : child.maxwidth; + if (child_maxwidth < Integer.MAX_VALUE) { + for(int i=0; i= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) + coeff[r] = (float)(regions[r+1] - regions[r]); + coeff[numregions*2+childnum] = (float)-1.0; + lp_h.add_constraint(coeff, LinearProgramming.LE, (float)child_maxwidth); + } + + childnum++; + } + + // priority 3: equalize columns + float avg = ((float)width)/((float)numregions); + for(int r=0; r unbounded) child_x -= (child_width - unbounded) / 2; + // child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP lp_h/lp easy_width/easy_height \ + // numregions/numregions_v regions/regions_v + child_width = 0; + child_x = 0; + if (cols == 1) { + child_x = 0; + child_width = width; + /* + } else if (easy_width) { + */ + } else { + for(int r=0; r= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) { + child_width += Math.round(lp_h.solution[lp_h.rows+r+1] * (regions[r+1] - regions[r])); + } else if (regions[r+1] <= child.col) { + child_x += Math.round(lp_h.solution[lp_h.rows+r+1] * (regions[r+1] - regions[r])); + } + } + } + diff = (child_width - min(child_width, child.test(HSHRINK) ? child.contentwidth : child.maxwidth)); + child_x += (child.test(ALIGN_RIGHT) ? diff : child.test(ALIGN_LEFT) ? 0 : diff / 2); + child_width = min(child_width, child.test(HSHRINK) ? child.contentwidth : child.maxwidth); //#end } child.resize(child_x, child_y, child_width, child_height); } - // cleanup - for(int i=0; i 0) + child.place_children(); } @@ -418,13 +511,28 @@ public final class Box extends JSScope implements Scheduler.Task { public int localToGlobalY(int y) { return parent == null ? y : parent.globalToLocalY(y + this.y); } public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { - if (nargs != 1 || !"indexof".equals(method)) return super.callMethod(method, a0, a1, a2, rest, nargs); - Box b = (Box)a0; - if (b.parent != this) - return (redirect == null || redirect == this) ? - N(-1) : - redirect.callMethod(method, a0, a1, a2, rest, nargs); - return N(b.getIndexInParent()); + switch (nargs) { + case 1: { + //#switch(method) + case "indexof": + Box b = (Box)a0; + if (b.parent != this) + return (redirect == null || redirect == this) ? + N(-1) : + redirect.callMethod(method, a0, a1, a2, rest, nargs); + return N(b.getIndexInParent()); + + case "distanceto": + Box b = (Box)a0; + JS ret = new JS(); + ret.put("x", N(b.localToGlobalX(0) - localToGlobalX(0))); + ret.put("y", N(b.localToGlobalY(0) - localToGlobalY(0))); + return ret; + + //#end + } + } + return super.callMethod(method, a0, a1, a2, rest, nargs); } public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); } @@ -448,6 +556,7 @@ public final class Box extends JSScope implements Scheduler.Task { //#switch(name) case "surface": return parent == null ? null : parent.getAndTriggerTraps("surface"); case "indexof": return METHOD; + case "distanceto": return METHOD; case "text": return text; case "path": throw new JSExn("cannot read from the path property"); case "fill": return colorToString(fillcolor); @@ -558,6 +667,7 @@ public final class Box extends JSScope implements Scheduler.Task { case "Maximized": if (parent == null && getSurface() != null) getSurface().maximized = toBoolean(value); // FEATURE 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; @@ -697,6 +807,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 && texture.isLoaded) perform(); } else { throw new JSExn("fill must be null, a String, or a stream, not a " + value.getClass()); }