X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FBox.java;h=8c36f81bd56f47a3657845347a1b12f35338c37c;hb=4bc540de0c5679607067b4598e99acee2cc66356;hp=3ac48d42f00f940982be3c05a3d4990b1531592f;hpb=a7bc8922080f56017b141a3835c722344fa4cffc;p=org.ibex.core.git diff --git a/src/org/xwt/Box.java b/src/org/xwt/Box.java index 3ac48d4..8c36f81 100644 --- a/src/org/xwt/Box.java +++ b/src/org/xwt/Box.java @@ -39,7 +39,7 @@ import org.xwt.translators.*; * SizeChanges trigger an Surface.abort; if rendering were done in the same * pass, rendering work done prior to the Surface.abort would be wasted. */ -public abstract class Box extends JSScope implements JSTrap.JSTrappable { +public final class Box extends JSScope { // Macros ////////////////////////////////////////////////////////////////////// @@ -97,7 +97,7 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { private String text = null; private Font font = null; - private Picture texture; + private Picture.Holder texture; private short strokewidth = 1; private int fillcolor = 0x00000000; private int strokecolor = 0xFF000000; @@ -136,8 +136,8 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { // 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 final void dirty() { dirty(0, 0, width, height); } - public final void dirty(int x, int y, int w, int h) { + public void dirty() { dirty(0, 0, width, height); } + public void dirty(int x, int y, int w, int h) { for(Box cur = this; cur != null; cur = cur.parent) { if (!cur.test(NOCLIP)) { w = min(x + w, cur.width) - max(x, 0); @@ -152,11 +152,6 @@ 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 */ void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); } void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) { @@ -166,11 +161,11 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { if (!wasinside && !isinside) return; 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); + if (!wasinside && isinside && getTrap("Enter") != null) putAndTriggerTraps("Enter", T); + else if (wasinside && !isinside && getTrap("Leave") != null) putAndTriggerTraps("Leave", T); else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && getTrap("Move")!= null) - putAndTriggerJSTraps("Move", T); - for(Box b = getChild(numchildren - 1); b != null; b = b.prevSibling()) { + putAndTriggerTraps("Move", T); + for(Box b = getChild(treeSize() - 1); b != null; b = b.prevSibling()) { b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave); if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true; } @@ -187,8 +182,8 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { private static LENGTH[] rowMaxHeight = new LENGTH[65535]; static { for(int i=0; i r) continue; - if (col != 0 && col + min(cols, child.colspan) > cols) break; + 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=col - numclear + 1; i <= col; i++) numRowsInCol[i] += child.rowspan; - child.col = col; child.row = r; + 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; } } for(int i=0; i unbounded) child_x -= (child_width - unbounded) / 2; @@ -334,10 +329,10 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { if ((fillcolor & 0xFF000000) != 0x00000000) 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 (texture != null && texture.picture != null) + for(int x = globalx; x < cx2; x += texture.picture.getWidth()) + for(int y = globaly; y < cy2; y += texture.picture.getHeight()) + buf.drawPicture(texture.picture, 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) @@ -356,31 +351,27 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { 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 call(Object method, JSArray args) throws JS.Exn { - if (!"indexof".equals(method)) return null; - Box b = (Box)args.elementAt(0); - if (b.parent != this) return (redirect == null || redirect == this) ? N(-1) : redirect.call(method, args); + 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()); } - /** to be filled in by the Tree implementation */ - abstract void put(int i, Object value); - public int numchildren = 0; - abstract public int getIndexInParent(); - abstract public Box getChild(int i); - abstract public Box nextSibling(); - abstract public Box prevSibling(); - abstract public void remove(); - abstract Box swapPosition(Box x, Box y); - - public Object get(Object name) { return get(name, false); } - public Object get(Object name, boolean ignoretraps) { + public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); } + + protected boolean isTrappable() { return true; } + public Object get(Object name) throws JSExn { if (name instanceof Number) return redirect == null ? null : redirect == this ? getChild(toInt(name)) : redirect.get(name); //#switch(name) + case "indexof": return METHOD; case "text": return text; - case "path": throw new JS.Exn("cannot read from the path property"); + case "path": throw new JSExn("cannot read from the path property"); case "fill": return colorToString(fillcolor); case "strokecolor": return colorToString(strokecolor); case "textcolor": return colorToString(strokecolor); @@ -409,7 +400,7 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { case "mousex": { Surface s = getSurface(); return N(s == null ? 0 : globalToLocalX(s.mousex)); } case "mousey": { Surface s = getSurface(); return N(s == null ? 0 : globalToLocalY(s.mousey)); } case "mouseinside": return B(test(MOUSEINSIDE)); - case "numchildren": return redirect == null ? N(0) : redirect == this ? N(numchildren) : redirect.get("numchildren"); + case "numchildren": return redirect == null ? N(0) : redirect == this ? N(treeSize()) : redirect.get("numchildren"); case "minwidth": return N(minwidth); case "maxwidth": return N(maxwidth); case "minheight": return N(minheight); @@ -418,20 +409,17 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { case "Minimized": if (parent == null && getSurface() != null) return B(getSurface().minimized); default: return super.get(name); //#end - return null; + throw new Error("unreachable"); // unreachable } - public void put(Object name, Object value) { put(name, value, false); } - public void put(Object name, Object value, boolean ignoretraps) { + 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 "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; @@ -442,7 +430,7 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { 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 "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 "noclip": CHECKSET_FLAG(NOCLIP); if (parent == null) dirty(); else parent.dirty(); @@ -473,14 +461,21 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { case "redirect": if (redirect == this) redirect = (Box)value; else Log.log(this, "redirect can only be set once"); case "font": font = value == null ? null : Font.getFont((Res)value, font == null ? 10 : font.pointsize); MARK_RESIZE; dirty(); case "fontsize": font = Font.getFont(font == null ? null : font.res, toInt(value)); MARK_RESIZE; dirty(); - case "x": if (test(PACKED) && parent != null) return; CHECKSET_INT(x); dirty(); MARK_RESIZE; dirty(); - case "y": if (test(PACKED) && parent != null) return; CHECKSET_INT(y); dirty(); MARK_RESIZE; dirty(); - case "KeyPressed": // prevent stuff from hitting the Hash - case "KeyReleased": // prevent stuff from hitting the Hash - case "PosChange": // prevent stuff from hitting the Hash - case "SizeChange": // prevent stuff from hitting the Hash - case "childadded": // prevent stuff from hitting the Hash - case "childremoved": // prevent stuff from hitting the Hash + case "x": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(x); } else { if (test(PACKED) && parent != null) return; 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; CHECKSET_INT(y); dirty(); MARK_RESIZE; dirty(); } + case "KeyPressed": return; // prevent stuff from hitting the Hash + case "KeyReleased": return; // prevent stuff from hitting the Hash + case "PosChange": return; // prevent stuff from hitting the Hash + case "SizeChange": return; // prevent stuff from hitting the Hash + case "childadded": return; // prevent stuff from hitting the Hash + case "childremoved": return; // prevent stuff from hitting the Hash + case "thisbox": { + if (value != null) break; + if (parent != null) { parent.removeChild(parent.indexNode(this)); return; } + Surface surface = Surface.fromBox(this); + if (surface != null) surface.dispose(true); + } + default: super.put(name, value); //#end } @@ -529,23 +524,26 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { if (value == null) return; if (value instanceof String) { // FIXME check double set - fillcolor = stringToColor((String)value); + int newfillcolor = stringToColor((String)value); + if (newfillcolor == fillcolor) return; + fillcolor = newfillcolor; + dirty(); + return; } if (!(value instanceof Res)) return; - Picture pic = Picture.fromRes((Res)value, null); - if (pic != null) { - texture = pic; - minwidth = texture.getWidth(); - minheight = texture.getHeight(); + texture = Picture.fromRes((Res)value, null); + if (texture != null) { + minwidth = texture.picture.getWidth(); + minheight = texture.picture.getHeight(); MARK_REFLOW; dirty(); - } else Picture.fromRes((Res)value, new Callback() { public Object call(Object arg) { - texture = (Picture)arg; - minwidth = texture.getWidth(); - minheight = texture.getHeight(); + return; + } + texture = Picture.fromRes((Res)value, new Scheduler.Task() { public void perform() { + minwidth = texture.picture.getWidth(); + minheight = texture.picture.getHeight(); Box b = Box.this; MARK_REFLOW_b; dirty(); - return null; } }); } @@ -555,8 +553,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)) { c.putAndTriggerJSTraps(name, value); return; } - if (parent != null) parent.putAndTriggerJSTraps(name, value); + if (c.inside(mousex - c.x, mousey - c.y)) { c.putAndTriggerTraps(name, value); return; } + if (parent != null) parent.putAndTriggerTraps(name, value); } private static int stringToColor(String s) { @@ -599,7 +597,7 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { if (!cur.test(VISIBLE)) return null; if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null; OUTER: while(true) { - for(int i=cur.numchildren - 1; i>=0; i--) { + for(int i=cur.treeSize() - 1; i>=0; i--) { Box child = cur.getChild(i); if (child == null) continue; // since this method is unsynchronized, we have to double-check globalx += child.x; @@ -616,37 +614,126 @@ public abstract class Box extends JSScope implements JSTrap.JSTrappable { // Trivial Helper Methods (should be inlined) ///////////////////////////////////////// - static final short min(short a, short b) { if (ab) return a; else return b; } - static final int max(int a, int b) { if (a>b) return a; else return b; } - static final float max(float a, float b) { if (a>b) return a; else return b; } + static short max(short a, short b) { if (a>b) return a; else return b; } + static int max(int a, int b) { if (a>b) return a; else return b; } + static float max(float a, float b) { if (a>b) return a; else return b; } - static final int min(int a, int b, int c) { if (a<=b && a<=c) return a; else if (b<=c && b<=a) return b; else return c; } - static final int max(int a, int b, int c) { if (a>=b && a>=c) return a; else if (b>=c && b>=a) return b; else return c; } - static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; } + static int min(int a, int b, int c) { if (a<=b && a<=c) return a; else if (b<=c && b<=a) return b; else return c; } + static int max(int a, int b, int c) { if (a>=b && a>=c) return a; else if (b>=c && b>=a) return b; else return c; } + static int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; } 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); } + void set(int mask) { flags |= mask; } + void set(int mask, boolean setclear) { if (setclear) set(mask); else clear(mask); } + void clear(int mask) { flags &= ~mask; } + boolean test(int mask) { return ((flags & mask) == mask); } - protected Box left = null; - protected Box right = null; - 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 rotateLeft(); - protected abstract Box rotateRight(); - protected abstract int numPeerChildren(); + + // Tree Handling ////////////////////////////////////////////////////////////////////// + + public final int getIndexInParent() { return parent == null ? 0 : parent.indexNode(this); } + public final Box nextSibling() { return parent == null ? null : parent.getChild(parent.indexNode(this) + 1); } + public final Box prevSibling() { return parent == null ? null : parent.getChild(parent.indexNode(this) - 1); } + public final Box getChild(int i) { + if (i < 0) return null; + if (i >= treeSize()) return null; + return (Box)getNode(i); + } + + // Tree Manipulation ///////////////////////////////////////////////////////////////////// + + /** remove the i^th child */ + public void removeChild(int i) { + Box b = getChild(i); + MARK_REFLOW_b; + b.dirty(); + b.clear(MOUSEINSIDE); + deleteNode(i); + b.parent = null; + MARK_REFLOW; + putAndTriggerTraps("childremoved", b); + } + + public void put(int i, Object value) throws JSExn { + if (i < 0) return; + + if (value != null && !(value instanceof Box)) { + if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to a non-box"); + return; + } + + if (redirect == null) { + if (value == null) putAndTriggerTraps("childremoved", getChild(i)); + else Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect"); + + } else if (redirect != this) { + if (value != null) putAndTriggerTraps("childadded", value); + redirect.put(i, value); + if (value == null) { + Box b = (Box)redirect.get(new Integer(i)); + if (b != null) putAndTriggerTraps("childremoved", b); + } + + } else if (value == null) { + if (i < 0 || i > treeSize()) return; + Box b = getChild(i); + removeChild(i); + putAndTriggerTraps("childremoved", b); + + } else { + Box b = (Box)value; + + // check if box being moved is currently target of a redirect + for(Box cur = b.parent; cur != null; cur = cur.parent) + if (cur.redirect == b) { + if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect"); + return; + } + + // check for recursive ancestor violation + for(Box cur = this; cur != null; cur = cur.parent) + if (cur == b) { + if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor"); + if (Log.on) Log.log(this, "box == " + this + " ancestor == " + b); + return; + } + + if (b.parent != null) b.parent.removeChild(b.parent.indexNode(b)); + insertNode(i, b); + b.parent = this; + + // need both of these in case child was already uncalc'ed + MARK_REFLOW_b; + MARK_REFLOW; + + b.dirty(); + putAndTriggerTraps("childadded", b); + } + } + + + public final void putAndTriggerTraps(Object key, Object value) { + try { + super.putAndTriggerTraps(key, value); + } catch (JSExn jse) { + Log.logJS("attempt to put value " + value + " to key " + key + " on a box triggered a trap which threw:"); + Log.logJS(jse); + } + } + + public final Object getAndTriggerTraps(Object key) { + try { + return super.getAndTriggerTraps(key); + } catch (JSExn jse) { + Log.logJS("attempt to get key " + key + " on a box triggered a trap which threw:"); + Log.logJS(jse); + return null; + } + } }