X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2FBox.java;h=7a16c49c5e42ad53131ef0645d0ffc6bdb0edd65;hp=514d301a0fcbbe083566c2e5d983da51eefe152d;hb=c17e36b43e73b4ae66ca7ccd018b239433a7457b;hpb=7bfa509c88f79ec899e7e6ebdb775d3c83e420f9 diff --git a/src/org/ibex/Box.java b/src/org/ibex/Box.java index 514d301..7a16c49 100644 --- a/src/org/ibex/Box.java +++ b/src/org/ibex/Box.java @@ -1,5 +1,5 @@ // FIXME -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] package org.ibex; // FEATURE: reflow before allowing js to read from width/height @@ -56,8 +56,6 @@ public final class Box extends JSScope implements Scheduler.Task { //#define CHECKSET_BOOLEAN(prop) boolean nu = toBoolean(value); if (nu == prop) break; prop = nu; //#define CHECKSET_STRING(prop) if ((value==null&&prop==null)||(value!=null&&value.equals(prop))) break; prop=(String)value; - void mark_for_repack() { MARK_REPACK; } - protected Box() { super(null); } static Hash boxToCursor = new Hash(500, 3); @@ -187,9 +185,6 @@ public final class Box extends JSScope implements Scheduler.Task { dirty(); } - public Box getRoot() { return parent == null ? this : parent.getRoot(); } - public Surface getSurface() { return Surface.fromBox(getRoot()); } - // FEATURE: use cx2/cy2 format /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */ public void dirty() { dirty(0, 0, width, height); } @@ -212,66 +207,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[] colMaxWidth = new LENGTH[65535]; - private static LENGTH[] rowHeight = 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; - } + void pack() { + for(Box child = getChild(0); child != null; child = child.nextSibling()) child.pack(); + int frontier_size = 0; contentwidth = 0; contentheight = 0; + //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan \ + // contentheight/contentwidth contentwidth/contentheight + if (treeSize() > 0 && test(FIXED) == COLS) { + rows = 0; + for(Box child = getChild(0); child != null; child = child.nextSibling()) { + if (!child.test(PACKED) || !child.test(VISIBLE)) continue; + child.col = (short)(frontier_size <= 0 ? 0 : (frontier[frontier_size-1].col + frontier[frontier_size-1].colspan)); + child.row = (short)(frontier_size <= 0 ? 0 : frontier[frontier_size-1].row); + if (child.col + min(cols,child.colspan) > cols) { child.col = 0; child.row++; } + for(int i=0; ichild.col) { + child.col = (short)(frontier[i].col + frontier[i].colspan); + if (child.col + min(cols,child.colspan) > cols) { + child.row = (short)(frontier[i].row + frontier[i].rowspan); + for(i--; i>0; i--) child.row = (short)min(row, frontier[i].row + frontier[i].rowspan); + child.col = (short)0; + } + i = -1; + } else break; + frontier[frontier_size++] = child; } - for(int i=0; i 0) do { + computeRegions(); + int nc = numregions * 3 + numkids * 2 + 3; + 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, Simplex.GE, (float)child.contentwidth); + if (!findMinimum) { + // 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, Simplex.LE, (float)child_maxwidth); + } + } + childnum++; + } - //#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 - for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling()) - for(int i=child.col; i < child.col + child.colspan; i++) { - colWidth[i] = max(colWidth[i], child.contentwidth / child.colspan); - colMaxWidth[i] = max(colMaxWidth[i], (child.test(HSHRINK) ? child.contentwidth : child.maxwidth) / child.colspan); + if (!findMinimum) { + // priority 3: equalize columns + float avg = ((float)width)/((float)numregions); + for(int r=0; r 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; - x_slack -= diff; + + try { switch(lp_h.solve()) { + case Simplex.UNBOUNDED: Log.warn(this, "simplex claims unboundedness; this should never happen"); break; + case Simplex.INFEASIBLE: Log.debug(this, "simplex claims infeasibility; this should never happen"); break; + case Simplex.MILP_FAIL: Log.warn(this, "simplex claims MILP_FAIL; this should never happen"); break; + case Simplex.RUNNING: Log.warn(this, "simplex still RUNNING; this should never happen"); break; + case Simplex.FAILURE: Log.warn(this, "simplex claims FAILURE; this should never happen"); break; + } } catch (Error e) { + Log.warn(this, "got an Error in simplex solver; not sure why this happens"); + return; } - slacklevel += increment; - } + + if (findMinimum) contentwidth = Math.round(lp_h.solution[lp_h.rows + (2*numregions + numkids + 1)]); + } while(false); //#end + } - // PHASE 3: assign childrens' actual sizes + void place() { + solve(false); for(Box child = getChild(0); child != null; child = child.nextSibling()) { if (!child.test(VISIBLE)) continue; int child_width, child_height, child_x, child_y; @@ -334,28 +401,34 @@ public final class Box extends JSScope implements Scheduler.Task { child_x = child.ax + (child.test(ALIGN_RIGHT) ? gap_x : !child.test(ALIGN_LEFT) ? gap_x / 2 : 0); child_y = child.ay + (child.test(ALIGN_BOTTOM) ? gap_y : !child.test(ALIGN_TOP) ? gap_y / 2 : 0); } else { - int unbounded; + int diff; //#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 = min(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; + // child_width/child_height ALIGN_RIGHT/ALIGN_BOTTOM ALIGN_LEFT/ALIGN_TOP lp_h/lp \ + // numregions/numregions_v regions/regions_v + child_width = 0; + child_x = 0; + if (cols == 1) { + child_x = 0; + child_width = 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 - (child.test(HSHRINK) ? child.contentwidth : min(child_width, child.maxwidth))); + child_x += (child.test(ALIGN_RIGHT) ? diff : child.test(ALIGN_LEFT) ? 0 : diff / 2); + child_width -= diff; //#end } child.resize(child_x, child_y, child_width, child_height); } - // cleanup - for(int i=0; i 0) + child.place(); } @@ -391,8 +464,7 @@ public final class Box extends JSScope implements Scheduler.Task { int gap_y = height - font.textheight(text); int text_x = globalx + (test(ALIGN_RIGHT) ? gap_x : !test(ALIGN_LEFT) ? gap_x/2 : 0); int text_y = globaly + (test(ALIGN_BOTTOM) ? gap_y : !test(ALIGN_TOP) ? gap_y/2 : 0); - if (font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, null) == -1) - font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, this); + font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2); } for(Box b = getChild(0); b != null; b = b.nextSibling()) @@ -402,22 +474,31 @@ public final class Box extends JSScope implements Scheduler.Task { // Methods to implement org.ibex.js.JS ////////////////////////////////////// - public int globalToLocalX(int x) { return parent == null ? x : parent.globalToLocalX(x - this.x); } - public int globalToLocalY(int y) { return parent == null ? y : parent.globalToLocalY(y - this.y); } - public int localToGlobalX(int x) { return parent == null ? x : parent.globalToLocalX(x + this.x); } - 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; - public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); } + //#end + } + } + return super.callMethod(method, a0, a1, a2, rest, nargs); + } protected boolean isTrappable(Object key, boolean isRead) { if (key == null) return false; @@ -438,6 +519,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); @@ -452,8 +534,8 @@ public final class Box extends JSScope implements Scheduler.Task { case "hshrink": return B(test(HSHRINK)); case "vshrink": return B(test(VSHRINK)); case "aspect": return N(aspect); - case "x": return (parent == null || !test(VISIBLE)) ? N(0) : N(x); - case "y": return (parent == null || !test(VISIBLE)) ? N(0) : N(y); + case "x": return (parent == null || !test(VISIBLE)) ? N(0) : test(PACKED) ? N(x) : N(ax); + case "y": return (parent == null || !test(VISIBLE)) ? N(0) : test(PACKED) ? N(y) : N(ay); 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); @@ -496,11 +578,11 @@ public final class Box extends JSScope implements Scheduler.Task { } } - void setMaxWidth(Object value) { + public void setMaxWidth(Object value) { do { CHECKSET_INT(maxwidth); MARK_RESIZE; } while(false); - if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth; + if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth; } - void setMaxHeight(Object value) { + public void setMaxHeight(Object value) { do { CHECKSET_INT(maxheight); MARK_RESIZE; } while(false); if (parent == null && getSurface() != null) getSurface().pendingHeight = maxheight; } @@ -526,8 +608,8 @@ public final class Box extends JSScope implements Scheduler.Task { case "minheight": CHECKSET_INT(minheight); MARK_RESIZE; if (parent == null && getSurface() != null) getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight); - case "colspan": CHECKSET_SHORT(colspan); MARK_REPACK_parent; - case "rowspan": CHECKSET_SHORT(rowspan); MARK_REPACK_parent; + 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(); @@ -548,6 +630,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; @@ -687,6 +770,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()); } @@ -739,7 +823,7 @@ public final class Box extends JSScope implements Scheduler.Task { } if (!obscured && !found) - if ("_Move".equals(name) || wasinside) + if ("_Move".equals(name) || name.startsWith("_Release") || wasinside) if (name != null) putAndTriggerTrapsAndCatchExceptions(name.substring(1), value); } @@ -802,6 +886,17 @@ public final class Box extends JSScope implements Scheduler.Task { // Trivial Helper Methods (should be inlined) ///////////////////////////////////////// + void mark_for_repack() { MARK_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()); } + 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(); } + public int globalToLocalX(int x) { return parent == null ? x : parent.globalToLocalX(x - this.x); } + public int globalToLocalY(int y) { return parent == null ? y : parent.globalToLocalY(y - this.y); } + public int localToGlobalX(int x) { return parent == null ? x : parent.globalToLocalX(x + this.x); } + public int localToGlobalY(int y) { return parent == null ? y : parent.globalToLocalY(y + this.y); } + static short min(short a, short b) { if (a