// 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
-// 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
+
// 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.*;
// 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);
//#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; }
+ //#define CHECKSET_STRING(prop) if ((value==null&&prop==null)||(value!=null&&JS.toString(value).equals(prop))) break; prop=JS.toString(value);
protected Box() { super(null); }
- static Hash boxToCursor = new Hash(500, 3);
+ static Hash boxToCursor = new Hash(500, 3); // FIXME memory leak
public static final int MAX_LENGTH = Integer.MAX_VALUE;
static final Font DEFAULT_FONT;
}
// 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",
- "KeyPressed", "KeyReleased", "PosChange", "SizeChange",
- "childadded", "childremoved",
- "Focused", "Maximized", "Minimized", "Close",
- "icon", "titlebar", "toback", "tofront"
+ "Enter", "Leave", "Move", "ChildChange",
+ "KeyPressed", "KeyReleased", "SizeChange",
+ "Focused", "Maximized", "Minimized", "Close"
};
// Flags //////////////////////////////////////////////////////////////////////
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;
static final int CURSOR = 0x00010000; // if true, this box has a cursor in the cursor hash; FEATURE: GC issues?
static final int CLIP = 0x00020000;
static final int STOP_UPWARD_PROPAGATION = 0x00040000;
+ static final int MOVED = 0x00080000;
// Instance Data //////////////////////////////////////////////////////////////////////
Box parent = null;
Box redirect = this;
- int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */ | STOP_UPWARD_PROPAGATION | CLIP;
+ int flags = VISIBLE | PACKED | REPACK | RECONSTRAIN | REPLACE | FIXED | STOP_UPWARD_PROPAGATION | CLIP | MOVED;
private String text = null;
private Font font = DEFAULT_FONT;
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 VectorGraphics.Affine rtransform = null;
*/
+ //#define DIRTY dirty()
+
// Instance Methods /////////////////////////////////////////////////////////////////////
+ public final int fontSize() { return font == null ? DEFAULT_FONT.pointsize : font.pointsize; }
/** invoked when a resource needed to render ourselves finishes loading */
public void perform() throws JSExn {
-
- // FIXME; we can't assume that just because we were performed the image is loaded.
- // as external events have occured, check the state of box
- if (texture != null) {
- if (texture.isLoaded) { minwidth = min(texture.width, maxwidth); minheight = min(texture.height, maxheight); }
- else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res); }
- }
-
- MARK_REPACK;
- MARK_REFLOW;
- MARK_RESIZE;
- dirty();
+ if (texture == null) { Log.warn(Box.class, "perform() called with null texture"); return; }
+ if (texture.isLoaded) {
+ setMinWidth(max(texture.width, minwidth));
+ setMinHeight(max(texture.height, minheight));
+ DIRTY; }
+ else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res.unclone()); }
}
- 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); }
// 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<rowMaxHeight.length; i++) { rowMaxHeight[i] = MAX_LENGTH; colMaxWidth[i] = MAX_LENGTH; } }
-
- 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(); }
-
- /** pack the boxes into rows and columns; also computes contentwidth */
- void repack() {
- for(Box child = getChild(0); child != null; child = child.nextSibling()) child.repack();
-
- //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan
+ /** 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() {
+ 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) {
- short r = 0;
- for(Box child = firstPackedChild(); child != null; r++) {
- for(short c=0, numclear=0; child != null && c < cols; c++) {
- if (numRowsInCol[c] > 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;
- }
+ rows = 0;
+ for(Box child = getChild(0); child != null; child = child.nextSibling()) {
+ if (!child.test(PACKED) || !child.test(VISIBLE)) continue;
+ if (cols == 1) { child.row = rows; rows += child.rowspan; child.col = 0; 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; i<frontier_size; i++)
+ if (frontier[i].row + frontier[i].rowspan <= child.row) {
+ frontier[i--] = frontier[--frontier_size]; frontier[frontier_size] = null;
+ } else if (frontier[i].col<child.col+min(cols,child.colspan)&&frontier[i].col+frontier[i].colspan>child.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<cols; i++) numRowsInCol[i] = 0;
+ 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();
+ }
- //#repeat contentwidth/contentheight colWidth/rowHeight colspan/rowspan col/row cols/rows minwidth/minheight \
- // textwidth/textheight maxwidth/maxheight
- contentwidth = 0;
- for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling())
- 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 || text == null ? 0 : font.textwidth(text), contentwidth), maxwidth);
- //#end
+ 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
+ contentwidth = bound(minwidth,
+ 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) {
- if (x != this.x || y != this.y || width != this.width || height != this.height) {
- boolean sizechange = (this.width != width || this.height != height) && getTrap("SizeChange") != null;
- boolean poschange = (this.x != x || this.y != y) && getTrap("PosChange") != null;
- //do {
- int thisx = parent == null ? 0 : this.x;
- int thisy = parent == null ? 0 : this.y;
-
- // we can't reenable this until we track
- // surface-relative sizes; imagine the case of a clear
- // surface with nonclear children
-
- /*
- if (texture == null && (text == null || text.equals(""))) {
- if ((fillcolor & 0xff000000) == 0) break;
- // FEATURE: more optimizations here
- if (this.x == x && this.y == y) {
- 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));
- break;
- }
- }
- */
- (parent == null ? this : parent).dirty(thisx, thisy, this.width, this.height);
- this.width = width; this.height = height; this.x = x; this.y = y;
- dirty();
- //} while (false);
- //this.width = width; this.height = height; this.x = x; this.y = y;
- if (sizechange) putAndTriggerTrapsAndCatchExceptions("SizeChange", T);
- if (poschange) putAndTriggerTrapsAndCatchExceptions("PosChange", T);
+ 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;
+ 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));
+ }
+ this.width = width; this.height = height; this.x = x; this.y = y;
+ } else {
+ who.dirty(thisx, thisy, this.width, this.height);
+ this.width = width; this.height = height; this.x = x; this.y = y;
+ DIRTY;
}
+ if (sizechange) putAndTriggerTrapsAndCatchExceptions("SizeChange", T);
}
- void resize_children() {
-
- //#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
- 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++) {
- x_slack += colWidth[i];
- colWidth[i] = max(colWidth[i], child.contentwidth / child.colspan);
- x_slack -= colWidth[i];
- colMaxWidth[i] = min(colMaxWidth[i], child.test(HSHRINK) ? child.contentwidth : child.maxwidth) / child.colspan;
+ 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;
+
+ 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 regions/regions_v targetColumnSize/targetRowSize sizes/sizes_v \
+ // HSHRINK/VSHRINK
+ if (numkids == 0) {
+ if (findMinimum) contentwidth = 0;
+ else targetColumnSize = 0;
+ } else if (cols == 1) {
+ if (findMinimum) {
+ contentwidth = 0;
+ for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling())
+ contentwidth = max(contentwidth, c.contentwidth);
+ } else {
+ targetColumnSize = width;
+ }
+ } else if (cols > 1) do {
+
+ // 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);
}
-
- // PHASE 2: hand out slack
- 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++) {
- // FIXME: double check this
- int diff = min(min(colMaxWidth[col], colWidth[col] + increment) - colWidth[col], x_slack);
- x_slack -= diff;
- colWidth[col] += diff;
+ 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
+ // priority 2: honor maxwidths
+ // priority 3: equalize columns
+ float targetColumnSize = target == 0 ? 0 : this.targetColumnSize;
+ float last_columnsize = 0;
+ float last_total = 0;
+ float total;
+ boolean first = true;
+ while(true) {
+ total = (float)0.0;
+ for(int r=0; r<numregions; r++) total += (sizes[r] = (float)(targetColumnSize * (regions[r+1]-regions[r])));
+ int minregion = 0;
+ for(Box child = firstPackedChild(); child != null; child = child.nextPackedSibling())
+ for(int r=(child.col==0?0:minregion); r<numregions; r++) {
+ 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.contentwidth - (float)0.5))
+ sizes[r] = (float)Math.max(sizes[r], (regions[r+1]-regions[r])*(child.contentwidth/child.colspan));
+ total += sizes[r];
+ }
+ float save = targetColumnSize;
+ if (Math.abs(total - target) <= (float)1.0) break;
+ if (!first) {
+ if (Math.abs(total - last_total) <= (float)1.0) break;
+ } else {
+ last_columnsize = ((total - target) / (float)cols) + targetColumnSize;
+ }
+ if (total < target) targetColumnSize += Math.abs((last_columnsize - targetColumnSize) / (float)1.1);
+ else if (total > target) targetColumnSize -= Math.abs((last_columnsize - targetColumnSize) / (float)1.1);
+ last_columnsize = save;
+ last_total = total;
+ first = false;
}
- }
+ if (findMinimum) contentwidth = Math.round(total);
+ else this.targetColumnSize = targetColumnSize;
+ } 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;
+ if (!child.test(REPLACE)) 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 = child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - Math.abs(child.ax));
+ child_height = child.test(VSHRINK) ? child.contentheight : min(child.maxheight, height - Math.abs(child.ay));
child_width = max(child.minwidth, child_width);
child_height = max(child.minheight, child_height);
+ int gap_x = width - child_width;
+ int gap_y = height - child_height;
+ 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 targetColumnSize/targetRowSize sizes/sizes_v
+ child_x = 0;
+ if (cols == 1) {
+ child_width = width;
+ } else {
+ child_width = 0;
+ for(int r=0; r<numregions; r++) {
+ if (regions[r] < child.col) child_x += Math.round(sizes[r]);
+ else if (regions[r] < child.col+child.colspan) child_width += Math.round(sizes[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
}
+ if (test(MOVED)) child.set(MOVED);
child.resize(child_x, child_y, child_width, child_height);
}
-
- // cleanup
- 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; }
+ clear(MOVED);
for(Box child = getChild(0); child != null; child = child.nextSibling())
- if (test(VISIBLE))
- child.resize_children();
+ if (child.test(VISIBLE) && child.treeSize() > 0)
+ child.place();
}
int globaly = parenty + (parent == null ? 0 : y);
// intersect the x,y,w,h rendering window with ourselves; quit if it's empty
-
if (test(CLIP)) {
- cx1 = max(cx1, parent == null ? 0 : globalx);
- cy1 = max(cy1, parent == null ? 0 : globaly);
+ cx1 = max(cx1, globalx);
+ cy1 = max(cy1, globaly);
cx2 = min(cx2, globalx + width);
cy2 = min(cy2, globaly + height);
if (cx2 <= cx1 || cy2 <= cy1) return;
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)
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, this);
+
+ if (text != null && !text.equals("") && font != null) {
+ int gap_x = width - font.textwidth(text);
+ 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);
+ font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2);
+ }
for(Box b = getChild(0); b != null; b = b.nextSibling())
b.render(globalx, globaly, cx1, cy1, cx2, cy2, buf, null);
// 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());
- }
-
- public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); }
+ 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;
- 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;
+ //#end
+ }
}
-
- return true;
+ return super.callMethod(method, a0, a1, a2, rest, nargs);
}
public Object get(Object name) throws JSExn {
//#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);
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);
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);
}
}
- void setMaxWidth(Object value) {
- do { CHECKSET_INT(maxwidth); MARK_RESIZE; } while(false);
- if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth;
+ public void setMaxWidth(Object value) {
+ do { CHECKSET_INT(maxwidth); RECONSTRAIN(); } while(false);
+ if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth;
}
- void setMaxHeight(Object value) {
- do { CHECKSET_INT(maxheight); MARK_RESIZE; } while(false);
+ public void setMaxHeight(Object value) {
+ do { CHECKSET_INT(maxheight); RECONSTRAIN(); } while(false);
if (parent == null && getSurface() != null) getSurface().pendingHeight = maxheight;
}
+ private void setMinWidth(int m) { if (this.minwidth != m) { RECONSTRAIN(); this.minwidth = m; } }
+ private void setMinHeight(int m) { if (this.minheight != m) { RECONSTRAIN(); this.minheight = m; } }
+
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 "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": 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 "hshrink": CHECKSET_FLAG(HSHRINK); RECONSTRAIN();
+ case "vshrink": CHECKSET_FLAG(VSHRINK); RECONSTRAIN();
+ case "width": put("maxwidth", value); put("minwidth", value);
+ case "height": put("maxheight", value); put("minheight", value);
case "maxwidth": setMaxWidth(value);
- case "minwidth": CHECKSET_INT(minwidth); MARK_RESIZE;
+ case "minwidth": CHECKSET_INT(minwidth); RECONSTRAIN();
if (parent == null && getSurface() != null)
getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
case "maxheight": setMaxHeight(value);
- case "minheight": CHECKSET_INT(minheight); MARK_RESIZE;
+ case "minheight": CHECKSET_INT(minheight); RECONSTRAIN();
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 "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 "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 "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();
+ case "clip": CHECKSET_FLAG(CLIP); if (parent == null) DIRTY; else parent.DIRTY;
+ case "visible": CHECKSET_FLAG(VISIBLE); RECONSTRAIN(); DIRTY;
+ case "packed": CHECKSET_FLAG(PACKED); if (parent != null) parent.REPACK();
+ case "align": clear(ALIGNS); setAlign(value == null ? "center" : value); REPLACE();
case "cursor": setCursor(value);
case "fill": setFill(value);
case "mouse":
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 "redirect": if (redirect == this) redirect = (Box)value; else Log.info(this, "redirect can only be set once");
+ 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;
+ }
+ JS.error("redirect can only be set to a descendant of its current value");
case "font":
if(!(value instanceof Stream)) throw new JSExn("You can only put streams to the font property");
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();
- case "x": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(x); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(x); dirty(); MARK_RESIZE; dirty(); }
- case "y": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(y); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(y); dirty(); MARK_RESIZE; dirty(); }
+ RECONSTRAIN();
+ DIRTY;
+ case "fontsize": font = Font.getFont(font == null ? null : font.stream, toInt(value)); RECONSTRAIN(); DIRTY;
+ case "x": if (parent==null && Surface.fromBox(this)!=null) {
+ CHECKSET_INT(x);
+ } else {
+ if (test(PACKED) && parent != null) return;
+ CHECKSET_INT(ax);
+ REPLACE();
+ }
+ case "y": if (parent==null && Surface.fromBox(this)!=null) {
+ CHECKSET_INT(y);
+ } else {
+ if (test(PACKED) && parent != null) return;
+ CHECKSET_INT(ay);
+ REPLACE();
+ }
case "titlebar":
if (getSurface() != null && value != null) getSurface().setTitleBarText(JS.toString(value));
super.put(name,value);
-
+
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);
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 "Enter": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
- case "Leave": 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)
+ parent.putAndTriggerTraps(name, N(((Number)value).floatValue() * ((float)parent.fontSize()) / ((float)fontSize())));
case "_Move": propagateDownward(name, value, false);
case "_Press1": propagateDownward(name, value, false);
case "_DoubleClick3": propagateDownward(name, value, false);
case "_KeyPressed": propagateDownward(name, value, false);
case "_KeyReleased": propagateDownward(name, value, false);
+ case "_HScroll": propagateDownward(name, value, false);
+ case "_VScroll": propagateDownward(name, value, false);
- case "PosChange": return;
case "SizeChange": return;
- case "childadded": return;
- case "childremoved": return;
+ case "ChildChange": return;
+ case "Enter": return;
+ case "Leave": return;
case "thisbox": if (value == null) removeSelf();
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());
}
- dirty();
+ DIRTY;
}
// FIXME: mouse move/release still needs to propagate to boxen in which the mouse was pressed and is still held down
if (wasinside || isinside)
for(Box child = getChild(treeSize() - 1); child != null; child = child.prevSibling()) {
boolean save_stop = child.test(STOP_UPWARD_PROPAGATION);
+ Object value2 = value;
+ if (name.equals("_HScroll") || name.equals("_VScroll"))
+ value2 = N(((Number)value).floatValue() * ((float)child.fontSize()) / (float)fontSize());
if (obscured || !child.inside(x - child.x, y - child.y)) {
- child.propagateDownward(name, value, true);
+ child.propagateDownward(name, value2, true);
} else try {
found = true;
child.clear(STOP_UPWARD_PROPAGATION);
- if (name != null) child.putAndTriggerTrapsAndCatchExceptions(name, value);
- else child.propagateDownward(name, value, obscured);
+ if (name != null) child.putAndTriggerTrapsAndCatchExceptions(name, value2);
+ else child.propagateDownward(name, value2, obscured);
} finally {
if (save_stop) child.set(STOP_UPWARD_PROPAGATION); else child.clear(STOP_UPWARD_PROPAGATION);
}
}
if (!obscured && !found)
- if ("_Move".equals(name) || wasinside)
+ if ("_Move".equals(name) || name.startsWith("_Release") || wasinside)
if (name != null)
putAndTriggerTrapsAndCatchExceptions(name.substring(1), value);
}
// Trivial Helper Methods (should be inlined) /////////////////////////////////////////
+ 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()); }
+ 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<b) return a; else return b; }
static int min(int a, int b) { if (a<b) return a; else return b; }
static float min(float a, float b) { if (a<b) return a; else return b; }
/** 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;
- putAndTriggerTrapsAndCatchExceptions("childremoved", b);
+ RECONSTRAIN();
+ putAndTriggerTrapsAndCatchExceptions("ChildChange", b);
}
public void put(int i, Object value) throws JSExn {
}
if (redirect == null) {
- if (value == null) putAndTriggerTrapsAndCatchExceptions("childremoved", getChild(i));
+ if (value == null) putAndTriggerTrapsAndCatchExceptions("ChildChange", getChild(i));
else JS.warn("attempt to add/remove children to/from a node with a null redirect");
} else if (redirect != this) {
- if (value != null) putAndTriggerTrapsAndCatchExceptions("childadded", value);
+ if (value != null) putAndTriggerTrapsAndCatchExceptions("ChildChange", value);
redirect.put(i, value);
if (value == null) {
Box b = (Box)redirect.get(new Integer(i));
- if (b != null) putAndTriggerTrapsAndCatchExceptions("childremoved", b);
+ if (b != null) putAndTriggerTrapsAndCatchExceptions("ChildChange", b);
}
} else if (value == null) {
if (i < 0 || i > treeSize()) return;
Box b = getChild(i);
removeChild(i);
- putAndTriggerTrapsAndCatchExceptions("childremoved", b);
+ putAndTriggerTrapsAndCatchExceptions("ChildChange", b);
} else {
Box b = (Box)value;
b.parent = this;
// need both of these in case child was already uncalc'ed
- MARK_REFLOW_b;
- MARK_REFLOW;
+ b.RECONSTRAIN();
+ RECONSTRAIN();
- b.dirty();
- putAndTriggerTrapsAndCatchExceptions("childadded", b);
+ b.DIRTY;
+ putAndTriggerTrapsAndCatchExceptions("ChildChange", b);
}
}