X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FBox.java;h=fb365945250afd48ff46f7c871ce7438b1311d14;hb=d3a783aafa7fb21b328f42c568cd3302be224f0f;hp=f04c3c2bc07b8d4453723565bf550f7289433f9b;hpb=1da33fe9e7c5104ec5d0b9b312d1114fdf2931be;p=org.ibex.core.git
diff --git a/src/org/xwt/Box.java b/src/org/xwt/Box.java
index f04c3c2..fb36594 100644
--- a/src/org/xwt/Box.java
+++ b/src/org/xwt/Box.java
@@ -1,11 +1,21 @@
// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
package org.xwt;
+// FEATURE: reflow before allowing js to read from width/height
+// FEATURE: fastpath for rows=1/cols=1
+// 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.io.*;
import java.net.*;
import java.util.*;
import org.xwt.js.*;
import org.xwt.util.*;
+import org.xwt.translators.*;
/**
*
@@ -13,1504 +23,1064 @@ import org.xwt.util.*;
* rendering logic.
*
*
- *
- * This is the real meat of XWT. Part of its monolithic design is for
- * performance reasons: deep inheritance heirarchies are slow, and
- * neither javago nor GCJ can inline across class boundaries.
- *
- *
- * The rendering process consists of two phases:
- *
- * - Prerendering pipeline: a set of methods that
- * traverse the tree (DFS, postorder) immediately before
- * rendering. These methods calculate the appropriate size
- * and position for all boxes. If no changes requiring a
- * re-prerender have been made to a box or any of its
- * descendants, needs_prerender will be false, and the box
- * and its descendants will be skipped.
- *
- *
- Rendering pipeline: a second set of methods that
- * traverses the tree (DFS, preorder) and renders each Box
- * onto the associated Surface. The render() method takes a
- * region (x,y,w,h) as an argument, and will only render
- * onto pixels within that region. Boxes which lie
- * completely outside that region will be skipped.
- *
- *
- *
- * Either of these two phases may abort at any time by setting
- * surface.abort to true. Currently, there are two reasons
- * for aborting: a SizeChange or PosChange was
- * triggered in the prerender phase (which ran scripts which modified
- * the boxtree's composition, requiring another prerender pass), or
- * else the user resized the surface. In either case, the
- * [pre]rendering process is halted as soon as possible and
- * re-started from the beginning.
- *
- *
- * Why are these done as seperate passes over the box tree?
- *
- * - Sometimes we need to make several trips through
- * prerender()SizeChange
- * or PosChange. Calling prerender() is
- * cheap; calling render() is expensive.
- *
- *
- Even if no SizeChange or PosChange traps are
- * triggered, updates to the size and position of boxes in
- * the prerender phase can cause additional regions to be
- * added to the dirty list. If the prerender and render
- * passes were integrated, this might cause some screen
- * regions to be painted twice (or more) in a single pass,
- * which hurts performance.
+ *
The rendering process consists of four phases; each requires
+ * one DFS pass over the tree
+ * - repacking: children of a box are packed into columns
+ * and rows according to their colspan/rowspan attributes and
+ * ordering.
+ *
- reconstraining: Minimum and maximum sizes of columns are computed.
+ *
- resizing: width/height and x/y positions of children
+ * are assigned, and PosChange/SizeChanges are triggered.
+ *
- repainting: children draw their content onto the PixelBuffer.
*
*
- *
- * A box's _cmin_* variables hold the minimum possible
- * dimensions of this box, taking into account the size of its
- * children (and their children). The cmin variables must be kept in
- * sync with its other geometric properties and the geometric
- * properties of its children at all times -- they are not
- * periodically recomputed during the [pre]rendering process, as size
- * and pos are. If any data changes which might invalidate the cmin,
- * the method sync_cmin_to_children() must be invoked
- * immediately.
- *
- *
- *
- * A note on coordinates: the Box class represents regions
- * internally as x,y,w,h tuples, even though the DoubleBuffer class
- * uses x1,y1,x2,y2 tuples.
- *
+ * The first three passes together are called the reflow phase.
+ * Reflowing is done in a seperate pass since PosChanges and
+ * 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 final class Box extends JS.Scope {
-
-
- // Static Data //////////////////////////////////////////////////////////////
-
- /** a pool of one-element Object[]'s */
- private static Queue singleObjects = new Queue(100);
-
- /** caches images, keyed on resource name or url */
- public static Hash pictureCache = new Hash();
-
- /** cache of border objects */
- static Hash bordercache = new Hash();
-
- /** stores image names, keyed on image object */
- static Hash imageToNameMap = new Hash();
-
- /** the empty object, used for get-traps */
- private static JS.Array emptyobj = new JS.Array();
-
-
- // Instance Data: Templates ////////////////////////////////////////////////////////
-
- /** the unresolved name of the first template applied to this node [ignoring preapply's], or null if this template was specified anonymously */
- String templatename = null;
-
- /** the importlist which was used to resolve templatename, or null if this template was specified anonymously */
- String[] importlist = Template.defaultImportList;
-
- /** the template instance that resulted from resolving templatename using importlist, or the anonymous template applied */
- Template template = null;
-
-
- // Instance Data: Geometry ////////////////////////////////////////////////////////
-
- /** The number of elements in the geom array */
- public static final int NUMINTS = 10;
-
- /** The maximum defined width and height of this box */
- public static final int dmax = 0;
- private int _dmax_0 = 0;
- private int _dmax_1 = 0;
- public final int dmax(int axis) { return axis == 0 ? _dmax_0 : _dmax_1; }
-
- /** The minimum defined width and height of this box */
- public static final int dmin = 1;
- private int _dmin_0 = 0;
- private int _dmin_1 = 0;
- public final int dmin(int axis) { return axis == 0 ? _dmin_0 : _dmin_1; }
-
- /** The minimum calculated width and height of this box -- unlike dmin, this takes childrens' sizes into account */
- public static final int cmin = 2;
- private int _cmin_0 = 0;
- private int _cmin_1 = 0;
- public final int cmin(int axis) { return axis == 0 ? _cmin_0 : _cmin_1; }
-
- /** The position of this box, relitave to the parent */
- public static final int abs = 3;
- private int _abs_0 = 0;
- private int _abs_1 = 0;
- public final int abs(int axis) { return axis == 0 ? _abs_0 : _abs_1; }
-
- /** The absolute position of this box (ie relitave to the root); set by the parent */
- public static final int pos = 4;
- private int _pos_0 = 0;
- private int _pos_1 = 0;
- public final int pos(int axis) { return axis == 0 ? _pos_0 : _pos_1; }
-
- /** The actual size of this box; set by the parent. */
- public static final int size = 5;
- int _size_0 = 0;
- int _size_1 = 0;
- public final int size(int axis) { return axis == 0 ? _size_0 : _size_1; }
-
- /** The old actual absolute position of this box (ie relitave to the root) */
- public static final int oldpos = 6;
- private int _oldpos_0 = 0;
- private int _oldpos_1 = 0;
- public final int oldpos(int axis) { return axis == 0 ? _oldpos_0 : _oldpos_1; }
-
- /** The old actual size of this box */
- public static final int oldsize = 7;
- private int _oldsize_0 = 0;
- private int _oldsize_1 = 0;
- public final int oldsize(int axis) { return axis == 0 ? _oldsize_0 : _oldsize_1; }
-
- /** The padding along each edge for this box */
- public static final int pad = 8;
- private int _pad_0 = 0;
- private int _pad_1 = 0;
- public final int pad(int axis) { return axis == 0 ? _pad_0 : _pad_1; }
-
- /** The dimensions of the text in this box */
- public static final int textdim = 9;
- private int _textdim_0 = 0;
- private int _textdim_1 = 0;
- public final int textdim(int axis) { return axis == 0 ? _textdim_0 : _textdim_1; }
-
-
- // Instance Data /////////////////////////////////////////////////////////////////
-
- /** This box's mouseinside property, as defined in the reference */
- boolean mouseinside = false;
-
- /** If redirect is enabled, this holds the Box redirected to */
+public final class Box extends JSScope {
+
+ // 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);
+ //#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;
+
+ protected Box() { super(null); }
+
+ static Hash boxToCursor = new Hash(500, 3);
+ public static final int MAX_LENGTH = Integer.MAX_VALUE;
+
+ // Flags //////////////////////////////////////////////////////////////////////
+
+ static final int MOUSEINSIDE = 0x00000001;
+ static final int VISIBLE = 0x00000002;
+ static final int PACKED = 0x00000004;
+ static final int HSHRINK = 0x00000008;
+ static final int VSHRINK = 0x00000010;
+ static final int BLACK = 0x00000020; // for red-black code
+
+ static final int FIXED = 0x00000040;
+ static final boolean ROWS = true;
+ static final boolean COLS = false;
+
+ 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 ALIGN_TOP = 0x00001000;
+ static final int ALIGN_BOTTOM = 0x00002000;
+ static final int ALIGN_LEFT = 0x00004000;
+ static final int ALIGN_RIGHT = 0x00008000;
+ static final int ALIGNS = 0x0000f000;
+ static final int CURSOR = 0x00010000; // if true, this box has a cursor in the cursor hash; FEATURE: GC issues?
+ static final int NOCLIP = 0x00020000;
+
+
+ // Instance Data //////////////////////////////////////////////////////////////////////
+
+ Box parent = null;
Box redirect = this;
+ int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */;
+
+ private String text = null;
+ private Font font = null;
+ private Picture.Holder texture;
+ private short strokewidth = 1;
+ private int fillcolor = 0x00000000;
+ private int strokecolor = 0xFF000000;
+
+ // specified directly by user
+ public LENGTH minwidth = 0;
+ public LENGTH maxwidth = MAX_LENGTH;
+ public LENGTH minheight = 0;
+ public LENGTH maxheight = MAX_LENGTH;
+ private short rows = 1;
+ private short cols = 0;
+ private short rowspan = 1;
+ private short colspan = 1;
+
+ // computed during reflow
+ private short row = 0;
+ private short col = 0;
+ public LENGTH x = 0;
+ public LENGTH y = 0;
+ public LENGTH width = 0;
+ public LENGTH height = 0;
+ private LENGTH contentwidth = 0; // == max(minwidth, textwidth, sum(child.contentwidth))
+ private LENGTH contentheight = 0;
+
+ /*
+ private VectorGraphics.VectorPath path = null;
+ private VectorGraphics.Affine transform = null;
+ private VectorGraphics.RasterPath rpath = null;
+ private VectorGraphics.Affine rtransform = null;
+ */
- /** the Box's font, null inherits from parent -- you must call textupdate() after changing this */
- String font = null;
-
- /** if font == null, this might be a cached copy of the inherited ancestor font */
- String cachedFont = null;
-
- /** The surface for us to render on; null if none; INVARIANT: surface == getParent().surface */
- Surface surface = null;
-
- /** Our alignment: top/left == -1, center == 0, bottom/right == +1 */
- byte align = 0;
-
- /** Our background image; to set this, use setImage() */
- public Picture image;
-
- /** The flex for this box */
- int flex = 1;
-
- /** The orientation, horizontal == 0, vertical == 1 */
- byte o = 0;
-
- /** The opposite of the orientation */
- byte xo = 1;
-
- /** The id of this Box */
- public String id = "";
-
- /** The text of this Box -- you must call textupdate() after changing this */
- String text = "";
-
- /** The color of the text in this Box in 00RRGGBB form -- default is black */
- int textcolor = 0xFF000000;
-
- /** The background color of this box in AARRGGBB form -- default is clear; alpha is all-or-nothing */
- int color = 0x00000000;
-
- /** Holds four "strip images" -- 0=top, 1=bottom, 2=left, 3=right, 4=all */
- Picture[] border = null;
-
- /** true iff the box's background image should be tiled */
- boolean tile = false;
-
- /** True iff the Box is invisible */
- public boolean invisible = false;
-
- /** If true, the Box will force its own size to the natural size of its background image */
- boolean sizetoimage = false;
-
- /** If true and tile is false, the background of this image will never be stretched */
- boolean fixedaspect = false;
-
- /** If true, the box will shrink to the smallest vertical size possible */
- boolean vshrink = false;
-
- /** If true, the box will shrink to the smallest horizontal size possible */
- boolean hshrink = false;
-
- /** If true, the box will be positioned absolutely */
- boolean absolute = false;
-
- /** True iff the Box must be run through the prerender() pipeline before render()ing;
- * INVARIANT: if (needs_prerender) then getParent().needs_prerender. **/
- boolean needs_prerender = true;
-
- /** The cursor for this Box -- only meaningful on the root Box */
- public String cursor = null;
-
- /** Any traps placed on this Box */
- public Hash traps = null;
-
-
- // Instance Data: IndexOf ////////////////////////////////////////////////////////////
+ // Instance Methods /////////////////////////////////////////////////////////////////////
- /** The indexof() Function; created lazily */
- public JS.Callable indexof = null;
- public JS.Callable indexof() { if (indexof == null) indexof = new IndexOf(); return indexof; }
+ public Box getRoot() { return parent == null ? this : parent.getRoot(); }
+ public Surface getSurface() { return Surface.fromBox(getRoot()); }
- /** a trivial private class to serve as the box.indexof function object */
- private class IndexOf extends JS.Callable {
- public IndexOf() { this.setSeal(true); }
- public Object call(JS.Array args) throws JS.Exn {
- if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
- Box b = (Box)args.elementAt(0);
- if (b.getParent() != Box.this) {
- if (redirect == null || redirect == Box.this) return new Integer(-1);
- return Box.this.redirect.indexof().call(args);
+ // 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); }
+ 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);
+ h = min(y + h, cur.height) - max(y, 0);
+ x = max(x, 0);
+ y = max(y, 0);
}
- return new Integer(b.getIndexInParent());
+ if (w <= 0 || h <= 0) return;
+ if (cur.parent == null && cur.getSurface() != null) cur.getSurface().dirty(x, y, w, h);
+ x += cur.x;
+ y += cur.y;
}
}
-
- // Methods which enforce/preserve invariants ////////////////////////////////////////////
-
- /** This method MUST be used to change geometry values -- it ensures that certain invariants are preserved. */
- public final void set(int which, int axis, int newvalue) {
-
- // if this Box is the root of the Surface, notify the Surface of size changes
- if (getParent() == null && surface != null && which == size)
- surface._setSize(axis == 0 ? newvalue : size(0), axis == 1 ? newvalue : size(1));
-
- if (getParent() == null && surface != null && (which == dmin || which == dmax))
- surface.setLimits(dmin(0), dmin(1), dmax(0), dmax(1));
-
- switch(which) {
- case dmin: if (dmin(axis) == newvalue) return; if (axis == 0) _dmin_0 = newvalue; else _dmin_1 = newvalue; break;
- case dmax: if (dmax(axis) == newvalue) return; if (axis == 0) _dmax_0 = newvalue; else _dmax_1 = newvalue; break;
- case cmin: if (cmin(axis) == newvalue) return; if (axis == 0) _cmin_0 = newvalue; else _cmin_1 = newvalue; break;
- case abs: if (abs(axis) == newvalue) return; if (axis == 0) _abs_0 = newvalue; else _abs_1 = newvalue; break;
- case pos: if (pos(axis) == newvalue) return; if (axis == 0) _pos_0 = newvalue; else _pos_1 = newvalue; break;
- case size: if (size(axis) == newvalue) return; if (axis == 0) _size_0 = newvalue; else _size_1 = newvalue; break;
- case oldpos: if (oldpos(axis) == newvalue) return; if (axis == 0) _oldpos_0 = newvalue; else _oldpos_1 = newvalue; break;
- case oldsize: if (oldsize(axis) == newvalue) return; if (axis == 0) _oldsize_0 = newvalue; else _oldsize_1 = newvalue; break;
- case pad: if (pad(axis) == newvalue) return; if (axis == 0) _pad_0 = newvalue; else _pad_1 = newvalue; break;
- case textdim: if (textdim(axis) == newvalue) return; if (axis == 0) _textdim_0 = newvalue; else _textdim_1 = newvalue; break;
- default: return;
- }
-
- // size must always agree with dmin/dmax
- if (which == dmin) set(size, axis, max(size(axis), newvalue));
- if (which == dmax) set(size, axis, min(size(axis), newvalue));
-
- // keep cmin in line with dmin/dmax/textdim
- if (which == dmax || which == dmin || which == textdim || which == pad || which == cmin)
- set(cmin, axis,
- max(
- min(dmax(axis), cmin(axis)),
- dmin(axis),
- min(dmax(axis), textdim(axis) + 2 * pad(axis))
- )
- );
+ /** 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) {
+ boolean wasinside = test(MOUSEINSIDE);
+ boolean isinside = test(VISIBLE) && inside(mousex, mousey) && !forceleave;
+ if (isinside) set(MOUSEINSIDE); else clear(MOUSEINSIDE);
+ if (!wasinside && !isinside) return;
- // if the pad changes, update cmin
- if (which == pad) sync_cmin_to_children();
-
- // needed in the shrink case, since dmin may have been the deciding factor in calculating cmin
- if ((vshrink || hshrink) && (which == dmin || which == textdim || which == pad)) sync_cmin_to_children();
-
- // if the cmin changes, we need to be re-prerendered
- if (which == cmin) mark_for_prerender();
-
- // if the absolute position of a box changes, its parent needs to be re-prerendered (to update the child's position)
- if (which == abs && getParent() != null) getParent().mark_for_prerender();
-
- // if our cmin changes, then our parent's needs to be recalculated
- if (getParent() != null && which == cmin) {
- mark_for_prerender();
- getParent().sync_cmin_to_children();
+ if (isinside && test(CURSOR)) Surface.fromBox(getRoot()).cursor = (String)boxToCursor.get(this);
+ 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)
+ putAndTriggerTraps("Move", T);
+ for(Box b = getChild(numchildren - 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;
}
-
}
- /** Marks this node and all its ancestors so that they will be prerender()ed */
- public final void mark_for_prerender() {
- if (needs_prerender) return;
- needs_prerender = true;
- if (getParent() != null) getParent().mark_for_prerender();
- }
- /** Ensures that cmin is in sync with the cmin's of our children. This should be called whenever a child is added or
- * removed, as well as when our pad is changed. */
- final void sync_cmin_to_children() {
- int co = (int)(2 * pad(o));
- int cxo = (int)(2 * pad(xo));
-
- for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
- if (bt.invisible || bt.absolute) continue;
- co += bt.cmin(o);
- cxo = (int)max(bt.cmin(xo) + 2 * pad(xo), cxo);
- }
-
- set(cmin, o, co);
- set(cmin, xo, cxo);
+ // 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; iimage or border if sizetoimage is true */
- public void syncSizeToImage() {
- set(dmax, 0, (image == null ? 0 : image.getWidth()) + (border == null ? 0 : border[2].getWidth()) * 2);
- set(dmax, 1, (image == null ? 0 : image.getHeight()) + (border == null ? 0 : border[0].getHeight()) * 2);
- set(dmin, 0, (image == null ? 0 : image.getWidth()) + (border == null ? 0 : border[2].getWidth()) * 2);
- set(dmin, 1, (image == null ? 0 : image.getHeight()) + (border == null ? 0 : border[0].getHeight()) * 2);
+ /** 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
+ 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) 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;
+ }
+ }
+ for(int i=0; i 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];
+ x_slack -= diff;
+ colWidth[col] += diff;
+ }
+ }
+ //#end
+
+ // 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_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 {
- set(textdim, 0, xwf.stringWidth(text));
- set(textdim, 1, (xwf.getMaxAscent() + xwf.getMaxDescent()));
+ 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 = 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;
+ //#end
}
+ child.resize(child_x, child_y, child_width, child_height);
}
+
+ // cleanup
+ for(int i=0; imouseinside and check
- * to see if this node requires any Enter, Leave, or Move notifications.
- *
- * @param forceleave set to true by the box's parent if the mouse is inside an older
- * sibling, which is covering up this box.
- */
- 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) {
-
- boolean wasinside = mouseinside;
- boolean isinside = !invisible && inside(mousex, mousey) && !forceleave;
- mouseinside = isinside;
-
- if (!wasinside && !isinside) return;
-
- if (!wasinside && isinside && is_trapped("Enter")) put("Enter", this);
- else if (wasinside && !isinside && is_trapped("Leave")) put("Leave", this);
- else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && is_trapped("Move")) put("Move", this);
-
- if (isinside && cursor != null && surface != null) surface.cursor = cursor;
-
- // if the mouse has moved into our padding region, it is considered 'outside' all our children
- if (!(mousex >= pos(0) + pad(0) && mousey >= pos(1) + pad(1) &&
- mousex < pos(0) + size(0) - pad(0) && mousey < pos(1) + size(1) + pad(1))) forceleave = true;
-
- for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
- b.Move(oldmousex, oldmousey, mousex, mousey, forceleave);
- if (b.inside(mousex, mousey)) forceleave = true;
- }
+ public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); }
+
+ /** to be filled in by the Tree implementation */
+ public int numchildren = 0;
+
+ protected boolean isTrappable() { return true; }
+ public Object get(Object name) {
+ 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 JSExn("cannot read from the path property");
+ case "fill": return colorToString(fillcolor);
+ case "strokecolor": return colorToString(strokecolor);
+ case "textcolor": return colorToString(strokecolor);
+ case "font": return font == null ? null : font.res;
+ case "fontsize": return font == null ? N(10) : N(font.pointsize);
+ case "strokewidth": return N(strokewidth);
+ case "align": return alignToString();
+ case "thisbox": return this;
+ case "shrink": return B(test(HSHRINK) || test(VSHRINK));
+ case "hshrink": return B(test(HSHRINK));
+ case "vshrink": return B(test(VSHRINK));
+ case "x": return (parent == null || !test(VISIBLE)) ? N(0) : N(x);
+ case "y": return (parent == null || !test(VISIBLE)) ? N(0) : N(y);
+ case "width": return N(width);
+ case "height": return N(height);
+ 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 "noclip": return B(test(NOCLIP));
+ case "visible": return B(test(VISIBLE) && (parent == null || (parent.get("visible") == T)));
+ case "packed": return B(test(PACKED));
+ case "globalx": return N(localToGlobalX(0));
+ case "globaly": return N(localToGlobalY(0));
+ case "cursor": return test(CURSOR) ? boxToCursor.get(this) : null;
+ 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 "minwidth": return N(minwidth);
+ case "maxwidth": return N(maxwidth);
+ case "minheight": return N(minheight);
+ case "maxheight": return N(maxheight);
+ case "redirect": return redirect == null ? null : redirect == this ? T : redirect.get("redirect");
+ case "Minimized": if (parent == null && getSurface() != null) return B(getSurface().minimized);
+ default: return super.get(name);
+ //#end
+ throw new Error("unreachable"); // unreachable
}
- /** creates a new box from an anonymous template; ids is passed through to Template.apply() */
- Box(Template anonymous, Vec pboxes, Vec ptemplates, JS.Callable callback, int numerator, int denominator) {
- super(null);
- set(dmax, 0, Integer.MAX_VALUE);
- set(dmax, 1, Integer.MAX_VALUE);
- template = anonymous;
- template.apply(this, pboxes, ptemplates, callback, numerator, denominator);
- templatename = null;
- importlist = null;
+ public void put(Object name, Object value) {
+ 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;
+ 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 "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); 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;
+ 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 "cursor": setCursor(value);
+ case "fill": setFill(value);
+ case "Press1": mouseEvent("Press1", value);
+ case "Press2": mouseEvent("Press2", value);
+ case "Press3": mouseEvent("Press3", value);
+ case "Release1": mouseEvent("Release1", value);
+ case "Release2": mouseEvent("Release2", value);
+ case "Release3": mouseEvent("Release3", value);
+ case "Click1": mouseEvent("Click1", value);
+ case "Click2": mouseEvent("Click2", value);
+ case "Click3": mouseEvent("Click3", value);
+ case "DoubleClick1": mouseEvent("DoubleClick1", value);
+ case "DoubleClick2": mouseEvent("DoubleClick2", value);
+ case "DoubleClick3": mouseEvent("DoubleClick3", value);
+ 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 "toback": if (parent == null && getSurface() != null && toBoolean(value)) { getSurface().toBack(); }
+ case "tofront": if (parent == null && getSurface() != null && toBoolean(value)) { getSurface().toFront(); }
+ 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": 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
+ default: super.put(name, value);
+ //#end
}
- /** creates a new box from an unresolved templatename and an importlist; use "box" for an untemplatized box */
- public Box(String templatename, String[] importlist) { this(templatename, importlist, null); }
- public Box(String templatename, String[] importlist, JS.Callable callback) {
- super(null);
- set(dmax, 0, Integer.MAX_VALUE);
- set(dmax, 1, Integer.MAX_VALUE);
- this.importlist = importlist;
- if (!"box".equals(templatename)) {
- template = Template.getTemplate(templatename, importlist);
- if (template == null)
- if (Log.on) Log.log(this, "couldn't find template \"" + templatename + "\"");
- }
- if (template != null) {
- this.templatename = templatename;
- template.apply(this, null, null, callback, 0, template.numUnits());
- if (redirect == this && !"self".equals(template.redirect)) redirect = null;
+ private String alignToString() {
+ switch(flags & ALIGNS) {
+ case (ALIGN_TOP | ALIGN_LEFT): return "topleft";
+ case (ALIGN_BOTTOM | ALIGN_LEFT): return "bottomleft";
+ case (ALIGN_TOP | ALIGN_RIGHT): return "topright";
+ case (ALIGN_BOTTOM | ALIGN_RIGHT): return "bottomright";
+ case ALIGN_TOP: return "top";
+ case ALIGN_BOTTOM: return "bottom";
+ case ALIGN_LEFT: return "left";
+ case ALIGN_RIGHT: return "right";
+ case 0: return "center";
+ default: throw new Error("invalid alignment flags: " + (flags & ALIGNS));
}
}
-
-
- // Prerendering Pipeline ///////////////////////////////////////////////////////////////
- /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
- void prerender() {
-
- if (invisible) return;
+ private void setAlign(Object value) {
+ //#switch(value)
+ case "center": clear(ALIGNS);
+ case "topleft": set(ALIGN_TOP | ALIGN_LEFT);
+ case "bottomleft": set(ALIGN_BOTTOM | ALIGN_LEFT);
+ case "topright": set(ALIGN_TOP | ALIGN_RIGHT);
+ case "bottomright": set(ALIGN_BOTTOM | ALIGN_RIGHT);
+ case "top": set(ALIGN_TOP);
+ case "bottom": set(ALIGN_BOTTOM);
+ case "left": set(ALIGN_LEFT);
+ case "right": set(ALIGN_RIGHT);
+ default: Log.logJS("invalid alignment \"" + value + "\"");
+ //#end
+ }
+
+ private void setCursor(Object value) {
+ if (value == null) { clear(CURSOR); boxToCursor.remove(this); return; }
+ if (value.equals(boxToCursor.get(this))) return;
+ set(CURSOR);
+ boxToCursor.put(this, value);
+ Surface surface = getSurface();
+ String tempcursor = surface.cursor;
+ Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
+ if (surface.cursor != tempcursor) surface.syncCursor();
+ }
- if (getParent() == null) {
- set(pos, 0, 0);
- set(pos, 1, 0);
+ private void setFill(Object value) {
+ if (value == null) return;
+ if (value instanceof String) {
+ // FIXME check double set
+ int newfillcolor = stringToColor((String)value);
+ if (newfillcolor == fillcolor) return;
+ fillcolor = newfillcolor;
+ dirty();
+ return;
}
-
- if (pos(0) != oldpos(0) || pos(1) != oldpos(1) || size(0) != oldsize(0) || size(1) != oldsize(1)) {
- needs_prerender = true;
- check_geometry_changes();
+ if (!(value instanceof Res)) return;
+ texture = Picture.fromRes((Res)value, null);
+ if (texture != null) {
+ minwidth = texture.picture.getWidth();
+ minheight = texture.picture.getHeight();
+ MARK_REFLOW;
+ dirty();
+ 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();
+ } });
+ }
+
+ private void mouseEvent(String name, Object value) {
+ Surface surface = getSurface();
+ if (surface == null) return;
+ 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.putAndTriggerTraps(name, value); return; }
+ if (parent != null) parent.putAndTriggerTraps(name, value);
+ }
- if (!needs_prerender) return;
- needs_prerender = false;
- if (numChildren() == 0) return;
-
- int sumchildren = sizeChildren();
- positionChildren(pos(o) + pad(o) + max(0, size(o) - 2 * pad(o) - sumchildren) / 2);
-
- for(Box b = getChild(0); b != null; b = b.nextSibling()) {
- b.prerender();
- if (surface.abort) {
- mark_for_prerender();
- return;
- }
+ private static int stringToColor(String s) {
+ if (s == null) return 0x00000000;
+ else if (SVG.colors.get(s) != null) return 0xFF000000 | toInt(SVG.colors.get(s));
+ else if (s.length() > 0 && s.charAt(0) == '#') try {
+ // FEATURE alpha
+ return 0xFF000000 |
+ (Integer.parseInt(s.substring(1, 3), 16) << 16) |
+ (Integer.parseInt(s.substring(3, 5), 16) << 8) |
+ Integer.parseInt(s.substring(5, 7), 16);
+ } catch (NumberFormatException e) {
+ Log.log(Box.class, "invalid color " + s);
+ return 0;
}
+ else return 0; // FEATURE: error?
}
- /** if the size or position of a box has changed, dirty() the appropriate regions and possibly send Enter/Leave/SizeChange/PosChange */
- private void check_geometry_changes() {
-
- // FASTPATH: if we haven't moved position (just changed size), and we're not a stretched image:
- if (oldpos(0) == pos(0) && oldpos(1) == pos(1) && (image == null || tile)) {
-
- // we use the max(border, pad) since because of the pad we might be revealing an abs-pos child
- int bw = max(border == null ? 0 : border[2].getWidth(), pad(0));
- int bh = max(border == null ? 0 : border[0].getHeight(), pad(1));
-
- // dirty only the *change* in the area we cover, both on ourselves and on our parent
- for(Box cur = this; cur != null && (cur == this || cur == this.getParent()); cur = cur.getParent()) {
- cur.dirty(pos(0) + min(oldsize(0) - bw, size(0) - bw),
- pos(1),
- java.lang.Math.abs(oldsize(0) - size(0)) + bw,
- max(oldsize(1), size(1)));
- cur.dirty(pos(0),
- pos(1) + min(oldsize(1) - bh, size(1) - bh),
- max(oldsize(0), size(0)),
- java.lang.Math.abs(oldsize(1) - size(1)) + bh);
- }
-
- // SLOWPATH: dirty ourselves, as well as our former position on our parent
- } else {
- dirty();
- if (getParent() != null) getParent().dirty(oldpos(0), oldpos(1), oldsize(0), oldsize(1));
-
- }
-
- boolean sizechange = false;
- boolean poschange = false;
- if ((oldsize(0) != size(0) || oldsize(1) != size(1)) && is_trapped("SizeChange")) sizechange = true;
- if ((oldpos(0) != pos(0) || oldpos(1) != pos(1)) && is_trapped("PosChange")) poschange = true;
-
- set(oldsize, 0, size(0));
- set(oldsize, 1, size(1));
- set(oldpos, 0, pos(0));
- set(oldpos, 1, pos(1));
-
- if (!sizechange && !poschange) return;
-
- if (++surface.sizePosChangesSinceLastRender >= 500) {
- if (surface.sizePosChangesSinceLastRender == 500) {
- if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
- /*
- try {
- Trap t = sizechange ? Trap.getTrap(this, "SizeChange") : Trap.getTrap(this, "PosChange");
- InterpretedJS.Callable f = (InterpretedJS.Callable)t.f;
- if (Log.on) Log.log(this, "Current trap is at " + f.getSourceName() + ":" + f.getLineNumbers()[0]);
- } catch (Throwable t) { }
- */
- }
- } else {
- if (sizechange) put("SizeChange", Boolean.TRUE);
- if (poschange) put("PosChange", Boolean.TRUE);
- if (sizechange || poschange) {
- surface.abort = true;
- return;
- }
- }
+ private static String colorToString(int argb) {
+ if ((argb & 0xFF000000) == 0) return null;
+ String red = Integer.toHexString((argb & 0x00FF0000) >> 16);
+ String green = Integer.toHexString((argb & 0x0000FF00) >> 8);
+ String blue = Integer.toHexString(argb & 0x000000FF);
+ if (red.length() < 2) red = "0" + red;
+ if (blue.length() < 2) blue = "0" + blue;
+ if (green.length() < 2) green = "0" + green;
+ return "#" + red + green + blue;
}
-
- /** sets our childrens' sizes */
- int sizeChildren() {
-
- // Set sizes along minor axis, as well as sizes for absolute-positioned children
- for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
- if (bt.invisible) continue;
- if (bt.absolute) {
- bt.set(size, 0, bt.hshrink ? bt.cmin(0) : max(bt.cmin(0), min(size(0) - bt.abs(0) - pad(0), bt.dmax(0))));
- bt.set(size, 1, bt.vshrink ? bt.cmin(1) : max(bt.cmin(1), min(size(1) - bt.abs(1) - pad(1), bt.dmax(1))));
- } else if (xo == 0 && bt.hshrink || xo == 1 && bt.vshrink) {
- bt.set(size, xo, bt.cmin(xo));
- } else {
- bt.set(size, xo, bound(bt.cmin(xo), size(xo) - 2 * pad(xo), bt.dmax(xo)));
+ /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
+ public static Box whoIs(Box cur, int x, int y) {
+
+ if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
+ int globalx = 0;
+ int globaly = 0;
+
+ // WARNING: this method is called from the event-queueing thread -- it may run concurrently with
+ // ANY part of XWT, and is UNSYNCHRONIZED for performance reasons. BE CAREFUL HERE.
+
+ 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--) {
+ Box child = cur.getChild(i);
+ if (child == null) continue; // since this method is unsynchronized, we have to double-check
+ globalx += child.x;
+ globaly += child.y;
+ if (child.test(VISIBLE) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
+ globalx -= child.x;
+ globaly -= child.y;
}
+ break;
}
+ return cur;
+ }
- // the ideal size of our children, along our own major axis
- int goal = (o == 0 && hshrink) || (o == 1 && vshrink) ? cmin(o) - 2 * pad(o) : size(o) - 2 * pad(o);
-
- // the current sum of the sizes of all children
- int total = 0;
- // each box is set to bound(box.cmin, box.flex * factor, box.dmax)
- int factor = 0;
+ // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
- // Algorithm: we set the sizes of all boxes to bound(cmin, flex * factor, dmax) for some global value
- // 'factor'. We figure out what 'factor' should be by starting at zero, and slowly increasing
- // it. After each pass, 'factor' is set to the smaller of two values: ((goal - total) /
- // remaining_flex) or the next largest value of factor which will cause some box to exceed its
- // cmin or dmax (thereby changing remaining_flex).
+ static short min(short a, short b) { if (ab) 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; }
- // the sum of the flexes of all boxes which are not pegged at either cmin or dmax
- int remaining_flex = 0;
+ 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; }
- // this is the next largest value of factor at which some box exceeds its cmin/dmax
- int nextjoint = Integer.MAX_VALUE;
-
- for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
- if (bt.absolute || bt.invisible) continue;
+ 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;
- int btmax = (o == 0 && bt.hshrink) || (o == 1 && bt.vshrink) ? bt.cmin(o) : bt.dmax(o);
- bt.set(size, o, bound(bt.cmin(o), factor * bt.flex, btmax));
- total += bt.size(o);
- if (factor * bt.flex < bt.cmin(o) && bt.size(o) == bt.cmin(o)) {
- nextjoint = min(nextjoint, divide_round_up(bt.cmin(o), bt.flex));
+ // Tree Handling //////////////////////////////////////////////////////////////////////
- } else if (bt.size(o) < btmax) {
- remaining_flex += bt.flex;
- nextjoint = min(nextjoint, divide_round_up(btmax, bt.flex));
- }
- }
+ private static boolean REDbool = false;
+ private static boolean BLACKbool = true;
- if (remaining_flex == 0) {
- if (nextjoint <= factor) break;
- factor = nextjoint;
- } else {
- factor = min((goal - total + factor * remaining_flex) / remaining_flex, nextjoint);
- }
+ public final Box peerTree_leftmost() { for (Box p = this; ; p = p.left) if (p.left == null) return p; }
+ public final Box peerTree_rightmost() { for (Box p = this; ; p = p.right) if (p.right == null) return p; }
+ static Box peerTree_parent(Box p) { return (p == null)? null: p.peerTree_parent; }
- if (goal - total <= remaining_flex) break;
- }
+ public void insertBeforeMe(Box cell) { left = cell; cell.peerTree_parent = this; cell.fixAfterInsertion(); }
+ public void insertAfterMe(Box cell) { right = cell; cell.peerTree_parent = this; cell.fixAfterInsertion(); }
- // arbitrarily distribute out any leftovers resulting from rounding errors
- int last = 0;
- while(goal != total && total != last) {
- last = total;
- for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
- int btmax = (o == 0 && bt.hshrink) || (o == 1 && bt.vshrink) ? bt.cmin(o) : bt.dmax(o);
- int newsize = bound(bt.cmin(o), bt.size(o) + (goal > total ? 1 : -1), btmax);
- total += newsize - bt.size(o);
- bt.set(size, o, newsize);
- }
- }
+ static boolean colorOf(Box p) { return (p == null) ? BLACKbool : p.test(BLACK); }
+ static void setColor(Box p, boolean c) { if (p != null) { if (c) p.set(BLACK); else p.clear(BLACK); } }
+ static Box leftOf(Box p) { return (p == null)? null: p.left; }
+ static Box rightOf(Box p) { return (p == null)? null: p.right; }
- return total;
- }
-
- /** positions this Box's children; cur is the starting position along this' major axis */
- void positionChildren(int cur) {
- for(Box bt = getChild(0); bt != null; bt = bt.nextSibling()) {
- if (bt.invisible) continue;
- if (bt.absolute) {
- bt.set(pos, 0, pos(0) + bt.abs(0));
- bt.set(pos, 1, pos(1) + bt.abs(1));
- } else {
- bt.set(pos, xo, pos(xo) + pad(xo) + max(0, ((size(xo) - 2 * pad(xo) - bt.size(xo)) * (bt.align + 1)) / 2));
- bt.set(pos, o, cur);
- bt.set(abs, 0, bt.pos(0) - pos(0));
- bt.set(abs, 1, bt.pos(1) - pos(1));
- cur += bt.size(o);
- }
+ public final Box nextSibling() {
+ if (right != null)
+ return right.peerTree_leftmost();
+ else {
+ Box p = peerTree_parent;
+ Box ch = this;
+ while (p != null && ch == p.right) { ch = p; p = p.peerTree_parent; }
+ return p;
}
}
-
- // Rendering Pipeline /////////////////////////////////////////////////////////////////////
-
- /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
- void render(int xIn, int yIn, int wIn, int hIn, DoubleBuffer buf) {
- if (surface.abort || invisible) return;
-
- // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
- int x = max(xIn, getParent() == null ? 0 : pos(0));
- int y = max(yIn, getParent() == null ? 0 : pos(1));
- int w = min(xIn + wIn, (getParent() == null ? 0 : pos(0)) + size(0)) - x;
- int h = min(yIn + hIn, (getParent() == null ? 0 : pos(1)) + size(1)) - y;
- if (w <= 0 || h <= 0) return;
-
- if (border != null) renderBorder(x, y, w, h, buf);
- if ((color & 0xFF000000) != 0x00000000 || getParent() == null) {
- int bw = border == null ? 0 : border[2].getWidth();
- int bh = border == null ? 0 : border[0].getHeight();
- int x1 = max(x, pos(0) + bw);
- int y1 = max(y, pos(1) + bh);
- int x2 = min(x + w, pos(0) + size(0) - bw);
- int y2 = min(y + h, pos(1) + size(1) - bh);
- if (y2 - y1 > 0 && x2 - x1 > 0)
- buf.fillRect(x1,y1,x2,y2,(color & 0xFF000000) != 0 ? color : SpecialBoxProperty.lightGray);
- }
-
- if (image != null) {
- if (tile) renderTiledImage(x, y, w, h, buf);
- else renderStretchedImage(x, y, w, h, buf);
+ public final Box prevSibling() {
+ if (left != null)
+ return left.peerTree_rightmost();
+ else {
+ Box p = peerTree_parent;
+ Box ch = this;
+ while (p != null && ch == p.left) { ch = p; p = p.peerTree_parent; }
+ return p;
}
-
- if (text != null && !text.equals("")) renderText(x, y, w, h, buf);
-
- // now subtract the pad region from the clip region before proceeding
- int x2 = max(x, pos(0) + pad(0));
- int y2 = max(y, pos(1) + pad(1));
- int w2 = min(x + w, pos(0) + size(0) - pad(0)) - x2;
- int h2 = min(y + h, pos(1) + size(1) - pad(1)) - y2;
-
- for(Box b = getChild(0); b != null; b = b.nextSibling())
- b.render(x2, y2, w2, h2, buf);
}
- private void renderBorder(int x, int y, int w, int h, DoubleBuffer buf) {
- int bw = border[4].getWidth();
- int bh = border[4].getHeight();
- buf.setClip(x, y, w + x, h + y);
+ public void removeNode() {
- if ((color & 0xFF000000) != 0xFF000000) {
- // if the color is null, we have to be very careful about drawing the corners
- //if (Log.verbose) Log.log(this, "WARNING: (color == null && border != null) on box with border " + imageToNameMap.get(border[4]));
+ // handle case where we are only node
+ if (left == null && right == null && peerTree_parent == null) return;
- // upper left corner
- buf.drawPicture(border[4],
- pos(0), pos(1), pos(0) + bw / 2, pos(1) + bh / 2,
- 0, 0, bw / 2, bh / 2);
-
- // upper right corner
- buf.drawPicture(border[4],
- pos(0) + size(0) - bw / 2, pos(1), pos(0) + size(0), pos(1) + bh / 2,
- bw - bw / 2, 0, bw, bh / 2);
+ // if strictly internal, swap places with a successor
+ if (left != null && right != null) {
+ Box s = nextSibling();
+ // To work nicely with arbitrary subclasses of Box, we don't want to
+ // just copy successor's fields. since we don't know what
+ // they are. Instead we swap positions in the tree.
+ swapPosition(this, s);
+ }
- // lower left corner
- buf.drawPicture(border[4],
- pos(0), pos(1) + size(1) - bh / 2, pos(0) + bw / 2, pos(1) + size(1),
- 0, bh - bh / 2, bw / 2, bh);
+ // Start fixup at replacement node (normally a child).
+ // But if no children, fake it by using self
- // lower right corner
- buf.drawPicture(border[4],
- pos(0) + size(0) - bw / 2, pos(1) + size(1) - bh / 2, pos(0) + size(0), pos(1) + size(1),
- bw - bw / 2, bh - bh / 2, bw, bh);
+ if (left == null && right == null) {
+
+ if (test(BLACK)) fixAfterDeletion();
- } else {
- buf.drawPicture(border[4], pos(0), pos(1)); // upper left corner
- buf.drawPicture(border[4], pos(0) + size(0) - bw, pos(1)); // upper right corner
- buf.drawPicture(border[4], pos(0), pos(1) + size(1) - bh); // lower left corner
- buf.drawPicture(border[4], pos(0) + size(0) - bw, pos(1) + size(1) - bh); // lower right corner
+ // Unlink (Couldn't before since fixAfterDeletion needs peerTree_parent ptr)
- }
+ if (peerTree_parent != null) {
+ if (this == peerTree_parent.left)
+ peerTree_parent.left = null;
+ else if (this == peerTree_parent.right)
+ peerTree_parent.right = null;
+ peerTree_parent = null;
+ }
- // top and bottom edges
- buf.setClip(max(x, pos(0) + bw / 2), y, min(x + w, pos(0) + size(0) - bw / 2), h + y);
- for(int i = max(x, pos(0) + bw / 2); i + 100 < min(x + w, pos(0) + size(0) - bw / 2); i += 100) {
- buf.drawPicture(border[0], i, pos(1));
- buf.drawPicture(border[1], i, pos(1) + size(1) - bh / 2);
}
- buf.drawPicture(border[0], min(x + w, pos(0) + size(0) - bw / 2) - 100, pos(1));
- buf.drawPicture(border[1], min(x + w, pos(0) + size(0) - bw / 2) - 100, pos(1) + size(1) - bh / 2);
-
- // left and right edges
- buf.setClip(x, max(y, pos(1) + bh / 2), w + x, min(y + h, pos(1) + size(1) - bh / 2));
- for(int i = max(y, pos(1) + bh / 2); i + 100 < min(y + h, pos(1) + size(1) - bh / 2); i += 100) {
- buf.drawPicture(border[2], pos(0), i);
- buf.drawPicture(border[3], pos(0) + size(0) - bw / 2, i);
+ else {
+ Box replacement = left;
+ if (replacement == null) replacement = right;
+
+ // link replacement to peerTree_parent
+ replacement.peerTree_parent = peerTree_parent;
+
+ if (peerTree_parent == null) parent.rootChild = replacement;
+ else if (this == peerTree_parent.left) peerTree_parent.left = replacement;
+ else peerTree_parent.right = replacement;
+
+ left = null;
+ right = null;
+ peerTree_parent = null;
+
+ // fix replacement
+ if (test(BLACK)) replacement.fixAfterDeletion();
+
}
- buf.drawPicture(border[2], pos(0), min(y + h, pos(1) + size(1) - bh / 2) - 100);
- buf.drawPicture(border[3], pos(0) + size(0) - bw / 2, min(y + h, pos(1) + size(1) - bh / 2) - 100);
-
- buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
}
- void renderStretchedImage(int x, int y, int w, int h, DoubleBuffer buf) {
- buf.setClip(x, y, w + x, h + y);
- int bw = border == null ? 0 : border[4].getHeight();
- int bh = border == null ? 0 : border[4].getWidth();
-
- int width = pos(0) + size(0) - bw / 2 - pos(0) + bw / 2;
- int height = pos(1) + size(1) - bh / 2 - pos(1) + bh / 2;
-
- if (fixedaspect) {
- int hstretch = width / image.getWidth();
- if (hstretch == 0) hstretch = -1 * image.getWidth() / width;
- int vstretch = height / image.getHeight();
- if (vstretch == 0) vstretch = -1 * image.getHeight() / height;
-
- if (hstretch < vstretch) {
- height = image.getHeight() * width / image.getWidth();
- } else {
- width = image.getWidth() * height / image.getHeight();
+ /**
+ * Swap the linkages of two nodes in a tree.
+ * Return new root, in case it changed.
+ **/
+ void swapPosition(Box x, Box y) {
+
+ /* Too messy. TODO: find sequence of assigments that are always OK */
+
+ Box px = x.peerTree_parent;
+ boolean xpl = px != null && x == px.left;
+ Box lx = x.left;
+ Box rx = x.right;
+
+ Box py = y.peerTree_parent;
+ boolean ypl = py != null && y == py.left;
+ Box ly = y.left;
+ Box ry = y.right;
+
+ if (x == py) {
+ y.peerTree_parent = px;
+ if (px != null) if (xpl) px.left = y; else px.right = y;
+ x.peerTree_parent = y;
+ if (ypl) {
+ y.left = x;
+ y.right = rx; if (rx != null) rx.peerTree_parent = y;
}
+ else {
+ y.right = x;
+ y.left = lx; if (lx != null) lx.peerTree_parent = y;
+ }
+ x.left = ly; if (ly != null) ly.peerTree_parent = x;
+ x.right = ry; if (ry != null) ry.peerTree_parent = x;
}
-
- buf.drawPicture(image,
- pos(0) + bw / 2,
- pos(1) + bh / 2,
- pos(0) + bw / 2 + width,
- pos(1) + bh / 2 + height,
- 0, 0, image.getWidth(), image.getHeight());
- buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
- }
-
- void renderTiledImage(int x, int y, int w, int h, DoubleBuffer buf) {
- int iw = image.getWidth();
- int ih = image.getHeight();
- int bh = border == null ? 0 : border[4].getWidth();
- int bw = border == null ? 0 : border[4].getHeight();
-
- for(int i=(x - pos(0) - bw)/iw; i <= (x + w - pos(0) - bw)/iw; i++) {
- for(int j=(y - pos(1) - bh)/ih; j<= (y + h - pos(1) - bh)/ih; j++) {
-
- int dx1 = max(i * iw + pos(0), x);
- int dy1 = max(j * ih + pos(1), y);
- int dx2 = min((i+1) * iw + pos(0), x + w);
- int dy2 = min((j+1) * ih + pos(1), y + h);
-
- int sx1 = dx1 - (i*iw) - pos(0);
- int sy1 = dy1 - (j*ih) - pos(1);
- int sx2 = dx2 - (i*iw) - pos(0);
- int sy2 = dy2 - (j*ih) - pos(1);
-
- if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
- buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
+ else if (y == px) {
+ x.peerTree_parent = py;
+ if (py != null) if (ypl) py.left = x; else py.right = x;
+ y.peerTree_parent = x;
+ if (xpl) {
+ x.left = y;
+ x.right = ry; if (ry != null) ry.peerTree_parent = x;
+ }
+ else {
+ x.right = y;
+ x.left = ly; if (ly != null) ly.peerTree_parent = x;
}
+ y.left = lx; if (lx != null) lx.peerTree_parent = y;
+ y.right = rx; if (rx != null) rx.peerTree_parent = y;
}
-
- }
-
- void renderText(int x, int y, int w, int h, DoubleBuffer buf) {
-
- if ((textcolor & 0xFF000000) == 0x00000000) return;
- buf.setClip(x, y, w + x, h + y);
-
- XWF xwf = XWF.getXWF(font());
- if (xwf != null) {
- xwf.drawString(buf, text,
- pos(0) + pad(0),
- pos(1) + pad(1) + xwf.getMaxAscent() - 1,
- textcolor);
- } else {
- buf.drawString(font(), text,
- pos(0) + pad(0),
- pos(1) + pad(1) + Platform.getMaxAscent(font()) - 1,
- textcolor);
+ else {
+ x.peerTree_parent = py; if (py != null) if (ypl) py.left = x; else py.right = x;
+ x.left = ly; if (ly != null) ly.peerTree_parent = x;
+ x.right = ry; if (ry != null) ry.peerTree_parent = x;
+
+ y.peerTree_parent = px; if (px != null) if (xpl) px.left = y; else px.right = y;
+ y.left = lx; if (lx != null) lx.peerTree_parent = y;
+ y.right = rx; if (rx != null) rx.peerTree_parent = y;
}
- buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
-
- int i=0; while(i i) {
- for(int j = pos(0) + pad(0); j < pos(0) + pad(0) + textdim(0); j += 2)
- buf.fillRect(j, pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2,
- j + 1, pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2 + 1,
- textcolor);
-
- } else if (font().lastIndexOf('u') > i) {
- buf.fillRect(pos(0) + pad(0),
- pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2,
- pos(0) + pad(0) + textdim(0),
- pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2 + 1,
- textcolor);
- }
+ boolean c = x.test(BLACK);
+ if (y.test(BLACK)) x.set(BLACK); else x.clear(BLACK);
+ if (c) y.set(BLACK); else y.clear(BLACK);
+ if (parent.rootChild == x) parent.rootChild = y;
+ else if (parent.rootChild == y) parent.rootChild = x;
}
-
- // Methods to implement org.mozilla.javascript.Scriptable //////////////////////////////////////
-
- /** Returns the i_th child */
- public Object get(int i) {
- if (redirect == null) return null;
- if (redirect != this) return redirect.get(i);
- return i >= numChildren() || i < 0 ? null : getChild(i);
+ void rotateLeft() {
+ Box r = right;
+ right = r.left;
+ if (r.left != null) r.left.peerTree_parent = this;
+ r.peerTree_parent = peerTree_parent;
+ if (peerTree_parent == null) parent.rootChild = r;
+ else if (peerTree_parent.left == this) peerTree_parent.left = r;
+ else peerTree_parent.right = r;
+ r.left = this;
+ peerTree_parent = r;
}
- /**
- * Inserts value as child i; calls remove() if necessary.
- * This method handles "reinserting" one of your children properly.
- * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
- * WARNING: O(n) runtime, unless i == numChildren()
- */
- public void put(int i, Object value) {
- 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 anything other than a box");
- } else if (redirect == null) {
- if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
- } else if (redirect != this) {
- Box b = value == null ? (Box)redirect.get(i) : (Box)value;
- redirect.put(i, value);
- put("0", b);
-
- } else if (value == null) {
- if (i >= 0 && i < numChildren()) {
- Box b = getChild(i);
- b.remove();
- put("0", b);
- }
-
- } else if (value instanceof RootProxy) {
- if (Log.on) Log.logJS(this, "attempt to reparent a box via its proxy object");
-
- } else {
- Box newnode = (Box)value;
+ void rotateRight() {
+ Box l = left;
+ left = l.right;
+ if (l.right != null) l.right.peerTree_parent = this;
+ l.peerTree_parent = peerTree_parent;
+ if (peerTree_parent == null) parent.rootChild = l;
+ else if (peerTree_parent.right == this) peerTree_parent.right = l;
+ else peerTree_parent.left = l;
+ l.right = this;
+ peerTree_parent = l;
+ }
- // check if box being moved is currently target of a redirect
- for(Box cur = newnode.getParent(); cur != null; cur = cur.getParent())
- if (cur.redirect == newnode) {
- if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
- return;
+ void fixAfterInsertion() {
+ clear(BLACK);
+ Box x = this;
+
+ while (x != null && x != parent.rootChild && !x.peerTree_parent.test(BLACK)) {
+ if (peerTree_parent(x) == leftOf(peerTree_parent(peerTree_parent(x)))) {
+ Box y = rightOf(peerTree_parent(peerTree_parent(x)));
+ if (colorOf(y) == REDbool) {
+ setColor(peerTree_parent(x), BLACKbool);
+ setColor(y, BLACKbool);
+ setColor(peerTree_parent(peerTree_parent(x)), REDbool);
+ x = peerTree_parent(peerTree_parent(x));
}
-
- // check for recursive ancestor violation
- for(Box cur = this; cur != null; cur = cur.getParent())
- if (cur == newnode) {
- 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 == " + newnode);
- return;
+ else {
+ if (x == rightOf(peerTree_parent(x))) {
+ x = peerTree_parent(x);
+ x.rotateLeft();
+ }
+ setColor(peerTree_parent(x), BLACKbool);
+ setColor(peerTree_parent(peerTree_parent(x)), REDbool);
+ if (peerTree_parent(peerTree_parent(x)) != null)
+ peerTree_parent(peerTree_parent(x)).rotateRight();
}
-
- if (numKids > 15 && children == null) convert_to_array();
- newnode.remove();
- newnode.parent = this;
-
- if (children == null) {
- if (firstKid == null) {
- firstKid = newnode;
- newnode.prevSibling = newnode;
- newnode.nextSibling = newnode;
- } else if (i >= numKids) {
- newnode.prevSibling = firstKid.prevSibling;
- newnode.nextSibling = firstKid;
- firstKid.prevSibling.nextSibling = newnode;
- firstKid.prevSibling = newnode;
- } else {
- Box cur = firstKid;
- for(int j=0; j= children.size()) {
- newnode.indexInParent = children.size();
- children.addElement(newnode);
- } else {
- children.insertElementAt(newnode, i);
- for(int j=i; j= parent.children.size() - 1) return null;
- return (Box)parent.children.elementAt(indexInParent + 1);
- }
- }
-
- /** returns our next sibling (parent[ourindex + 1]) */
- public final Box prevSibling() {
- if (parent == null) return null;
- if (parent.children == null) {
- if (this == parent.firstKid) return null;
- return prevSibling;
- } else {
- if (indexInParent == 0) return null;
- return (Box)parent.children.elementAt(indexInParent - 1);
- }
- }
-
- /** Returns the parent of this node */
- public Box getParent() { return parent; }
-
/** Returns ith child */
public Box getChild(int i) {
- if (children == null) {
- if (firstKid == null) return null;
- if (i >= numKids) return null;
- if (i == numKids - 1) return firstKid.prevSibling;
- Box cur = firstKid;
- for(int j=0; j= children.size() || i < 0) return null;
- return (Box)children.elementAt(i);
- }
- }
-
- /** Returns the number of children */
- public int numChildren() {
- if (children == null) {
- if (firstKid == null) return 0;
- int i=1;
- for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
- return i;
- } else {
- return children.size();
- }
+ // FIXME: store numleft and numright in the tree
+ Box left = rootChild;
+ if (left == null) return null;
+ while (left.left != null) left = left.left;
+ for(; i > 0; i--) left = left.nextSibling();
+ return left;
}
/** Returns our index in our parent */
public int getIndexInParent() {
- if (parent == null) return 0;
- if (parent.children == null) {
- int i = 0;
- for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
- return i;
- } else {
- return indexInParent;
- }
+ // FIXME: store numleft and numright in the tree
+ if (peerTree_parent == null) return left == null ? 0 : left.numPeerChildren() + 1;
+ else if (peerTree_parent.left == this) return peerTree_parent.getIndexInParent() - 1;
+ else if (peerTree_parent.right == this) return peerTree_parent.getIndexInParent() + 1;
+ else throw new Error("we're not a child of our parent!");
}
- /** returns the root of the surface that this box belongs to */
- public final Box getRoot() {
- if (getParent() == null && surface != null) return this;
- if (getParent() == null) return null;
- return getParent().getRoot();
+ public int numPeerChildren() {
+ return (left == null ? 0 : left.numPeerChildren() + 1) + (right == null ? 0 : right.numPeerChildren() + 1);
}
+ public void put(int i, Object value) {
+ 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");
- // Root Proxy ///////////////////////////////////////////////////////////////////////////////
+ } 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);
+ }
- RootProxy myproxy = null;
- public JS getRootProxy() {
- if (myproxy == null) myproxy = new RootProxy(this);
- return myproxy;
- }
+ } else if (value == null) {
+ if (i < 0 || i > numchildren) return;
+ Box b = getChild(i);
+ b.remove();
+ 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;
+ }
+
+ b.remove();
+ b.parent = this;
+ numchildren++;
- private static class RootProxy extends JS {
- Box box;
- RootProxy(Box b) { this.box = b; }
- public Object get(Object name) { return box.get(name); }
- public void put(Object name, Object value) { box.put(name, value, false, this); }
- public Object[] keys() { return box.keys(); }
- public Object callMethod(Object method, JS.Array args, boolean justChecking) {
- return box.callMethod(method,args,justChecking);
+ Box before = getChild(i);
+ if (before == null) {
+ if (rootChild == null) rootChild = b;
+ else rootChild.peerTree_rightmost().insertAfterMe(b);
+ }
+ else before.insertBeforeMe(b);
+
+ // need both of these in case child was already uncalc'ed
+ MARK_REFLOW_b;
+ MARK_REFLOW;
+
+ b.dirty();
+ putAndTriggerTraps("childadded", b);
}
}
+}
- // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
- /** helper, included in this class so it can be inlined */
- static final int min(int a, int b) {
- if (ab) return a;
- else return b;
- }
-
- /** helper, included in this class so it can be inlined */
- 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;
- }
-
- /** helper, included in this class so it can be inlined */
- 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;
- }
-
- /** helper, included in this class so it can be inlined */
- static final int bound(int a, int b, int c) {
- if (c < b) return c;
- if (a > b) return a;
- return b;
- }
- /** returns numerator/denominator, but rounds up instead of down */
- static final int divide_round_up(int numerator, int denominator) {
- // cope with bozos who use flex==0.0
- if (denominator == 0) return Integer.MAX_VALUE;
- int ret = numerator / denominator;
- if (ret * denominator < numerator) return ret + 1;
- return ret;
- }
- /** Simple helper function to determine if the point x,y falls within this node's actual current geometry */
- boolean inside(int x, int y) {
- if (invisible) return false;
- return (x >= pos(0) && y >= pos(1) && x < pos(0) + size(0) && y < pos(1) + size(1));
- }
-
- /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface
- *
- * IMPORTANT: this method gets called from the event-queueing thread, since we need to determine which box is
- * underneath the mouse as early as possible. Because of this, we have to do some extra checks, as the
- * Box tree may be in flux.
- */
- Box whoIs(int x, int y) {
- if (invisible) return null;
- if (!inside(x,y)) return getParent() == null ? this : null;
-
- // We do this because whoIs is unsynchronized (for
- // speed), yet is sometimes called while the structure of the
- // Box is in flux.
- for(int i=numChildren() - 1; i>=0; i--) {
-
- Box child = getChild(i);
- if (child == null) continue;
-
- Box bt = child.whoIs(x,y);
- if (bt != null) return bt;
+ /*
+ offset_x = 0;
+ if (path != null) {
+ if (rpath == null) rpath = path.realize(transform == null ? VectorGraphics.Affine.identity() : transform);
+ if ((flags & HSHRINK) != 0) contentwidth = max(contentwidth, rpath.boundingBoxWidth());
+ if ((flags & VSHRINK) != 0) contentheight = max(contentheight, rpath.boundingBoxHeight());
+ // FIXME: separate offset_x needed for the path
}
- return this;
- }
-
- // This is the name returned by typeof() by JS
- public String typeName() {
- return "box";
- }
-
-}
+ // #repeat x1/y1 x2/y2 x3/y3 x4/y4 contentwidth/contentheight left/top right/bottom
+ int x1 = transform == null ? 0 : (int)transform.multiply_px(0, 0);
+ int x2 = transform == null ? 0 : (int)transform.multiply_px(contentwidth, 0);
+ int x3 = transform == null ? contentwidth : (int)transform.multiply_px(contentwidth, contentheight);
+ int x4 = transform == null ? contentwidth : (int)transform.multiply_px(0, contentheight);
+ int left = min(min(x1, x2), min(x3, x4));
+ int right = max(max(x1, x2), max(x3, x4));
+ contentwidth = max(contentwidth, right - left);
+ offset_x = -1 * left;
+ // #end
+ */
+
+
+ /*
+ if (path != null) {
+ if (rtransform == null) rpath = null;
+ else if (!rtransform.equalsIgnoringTranslation(a)) rpath = null;
+ else {
+ rpath.translate((int)(a.e - rtransform.e), (int)(a.f - rtransform.f));
+ rtransform = a.copy();
+ }
+ if (rpath == null) rpath = path.realize((rtransform = a) == null ? VectorGraphics.Affine.identity() : a);
+ if ((strokecolor & 0xff000000) != 0) rpath.stroke(buf, 1, strokecolor);
+ if ((fillcolor & 0xff000000) != 0) rpath.fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
+ }
+*/
+/*
+ VectorGraphics.Affine a2 = VectorGraphics.Affine.translate(b.x, b.y);
+ if (transform != null) a2.multiply(transform);
+ a2.multiply(VectorGraphics.Affine.translate(offset_x, offset_y));
+ a2.multiply(a);
+*/