1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
4 // **** This file must be preprocessed before compilation ****
6 // RULE: coordinates on non-static methods are ALWAYS relative to the
7 // upper-left hand corner of <tt>this</tt>
9 // FEATURE: reflow before allowing js to read from width/height
10 // FEATURE: fastpath for rows=1/cols=1
11 // FEATURE: reflow starting with a certain child
12 // FEATURE: separate mark_for_reflow and mark_for_resize
18 import org.xwt.util.*;
19 import org.xwt.translators.*;
23 * Encapsulates the data for a single XWT box as well as all layout
28 * This is the real meat of XWT. Part of its monolithic design is for
29 * performance reasons: deep inheritance heirarchies are slow, and
30 * neither javago nor GCJ can inline across class boundaries.
33 * <p>The rendering process consists of three phases; each requires
34 * one DFS pass over the tree</p>
36 * <ol><li> <b>repacking</b>: children of a box are packed into columns
37 * and rows according to their colspan/rowspan attributes and
38 * ordering. Minimum and maximum sizes of columns are computed.
40 * <li> <b>resizing</b>: width/height and x/y positions of children
41 * are assigned. If a PosChange or SizeChange is triggered,
42 * <tt>Surface.abort</tt> will be set and the resizing process will
45 * <li> <b>repainting</b>: children draw their content onto the
49 * The first two passes together are called the <i>reflow</i> phase.
51 * Reflowing is done in a seperate pass since PosChanges and
52 * SizeChanges trigger an Surface.abort; if rendering were done in the same
53 * pass, rendering work done prior to the Surface.abort would be wasted.
55 * The two passes are repack and resize. Together they are known as
56 * reflow. Repacking assigns boxes to the appropriate grid
57 * coordinates within the parents and computes
58 * contentwidth/contentheight. Resize computes actual size and
61 * A note on coordinates: the Box class represents regions
62 * internally as x,y,w,h tuples, even though the PixelBuffer class
63 * uses x1,y1,x2,y2 tuples.
65 public final class Box extends JS.Scope {
67 public Box() { super(null); }
70 // Misc instance data ////////////////////////////////////////////////////////////////
72 static int sizePosChangesSinceLastRender = 0;
75 // Misc instance data ////////////////////////////////////////////////////////////////
77 boolean needs_reflow = true;
78 //#define MARK_FOR_REFLOW_this for(Box b2 = this; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
79 //#define MARK_FOR_REFLOW_b for(Box b2 = b; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
80 //#define MARK_FOR_REFLOW_b_parent for(Box b2 = b.parent; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
83 Surface surface = null; // null on all non-root boxen
85 // Flags ///////////////////////////////////////////////////////////////////////////////
87 static int MOUSEINSIDE_FLAG = 0x00000001;
88 static int INVISIBLE_FLAG = 0x00000002;
89 static int ABSOLUTE_FLAG = 0x00000004;
90 static int HSHRINK_FLAG = 0x00000008;
91 static int VSHRINK_FLAG = 0x00000010;
92 static int TILE_FLAG = 0x00000020;
93 static int FONT_CHANGED_FLAG = 0x00000040; // set when font changes, cleared during repack
94 static int ISROOT_FLAG = 0x00000080;
95 static int NOCLIP_FLAG = 0x00000100;
98 // Geometry ////////////////////////////////////////////////////////////////////////////
100 // xwt can be compiled with 16-bit lengths to save memory on small devices
102 //#define MAX_LENGTH Integer.MAX_VALUE
103 //#define MIN_LENGTH Integer.MIN_VALUE
105 // always correct (set directly by user)
106 public LENGTH minwidth = 0;
107 public LENGTH minheight = 0;
108 public LENGTH maxwidth = MAX_LENGTH;
109 public LENGTH maxheight = MAX_LENGTH;
110 private LENGTH hpad = 0;
111 private LENGTH vpad = 0;
112 private String text = null;
113 private Res font = null;
114 private int fontsize = 10;
115 private LENGTH textwidth = 0;
116 private LENGTH textheight = 0;
118 // FEATURE: use shorts
119 private int rows = 1;
120 private int cols = 0;
121 private int rowspan = 1;
122 private int colspan = 1;
124 // computed during reflow
127 public LENGTH width = 0;
128 public LENGTH height = 0;
129 private int row = 0; // FEATURE use a short
130 private int col = 0; // FEATURE use a short
131 private LENGTH contentwidth = 0; // == max(minwidth, textwidth+pad, sum(child.contentwidth) + pad)
132 private LENGTH contentheight = 0;
135 // Rendering Properties ///////////////////////////////////////////////////////////
137 private VectorGraphics.VectorPath path = null;
138 private VectorGraphics.Affine transform = null;
139 //private VectorGraphics.Paint fill = null;
140 //private VectorGraphics.Paint stroke = null;
143 public Picture image; // will disappear
144 private int fillcolor = 0x00000000; // will become VectorGraphics.Paint
145 private int strokecolor = 0xFF000000; // will become VectorGraphics.Paint
146 private int textcolor = 0xFF000000; // will become VectorGraphics.Paint
148 private String cursor = null; // the cursor for this box
151 // Instance Methods /////////////////////////////////////////////////////////////////////
153 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
154 public final void dirty() {
155 if ((flags & NOCLIP_FLAG) != 0 && parent != null) parent.dirty();
156 else dirty(0, 0, width, height);
158 public final void dirty(int x, int y, int w, int h) {
159 for(Box cur = this; cur != null; cur = cur.parent) {
160 if ((flags & NOCLIP_FLAG) == 0) {
161 w = min(x + w, cur.width) - max(x, 0);
162 h = min(y + h, cur.height) - max(y, 0);
166 if (w <= 0 || h <= 0) return;
167 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
174 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
175 * to see if this node requires any Enter, Leave, or Move notifications.
177 * @param forceleave set to true by the box's parent if the mouse is inside an older
178 * sibling, which is covering up this box.
180 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
181 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
183 boolean wasinside = (flags & MOUSEINSIDE_FLAG) != 0;
184 boolean isinside = !((flags & INVISIBLE_FLAG) != 0) && inside(mousex, mousey) && !forceleave;
185 if (isinside) flags |= MOUSEINSIDE_FLAG; else flags &= ~MOUSEINSIDE_FLAG;
187 if (!wasinside && !isinside) return;
189 if (!wasinside && isinside && get("Enter", Trap.class) != null) put("Enter", Boolean.TRUE);
190 else if (wasinside && !isinside && get("Leave", Trap.class) != null) put("Leave", Boolean.TRUE);
191 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && get("Move", Trap.class) != null)
192 put("Move", Boolean.TRUE);
194 if (isinside && cursor != null) getRoot().cursor = cursor;
196 // if the mouse has moved into our padding region, it is considered 'outside' all our children
197 if (!(mousex >= hpad && mousey >= vpad && mousex < width - hpad && mousey < height + vpad)) forceleave = true;
199 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
200 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
201 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
206 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
208 void reflow() { reflow(width, height); }
209 void reflow(int new_width, int new_height) {
211 if (Surface.abort) return;
212 resize(x, y, new_width, new_height);
215 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
217 if (!needs_reflow) return;
218 if (numChildren() == 0) {
219 contentwidth = max(textwidth + 2 * hpad, minwidth);
220 contentheight = max(textheight + 2 * vpad, minheight);
222 contentwidth = max(contentwidth, path.boundingBoxWidth());
223 contentheight = max(contentheight, path.boundingBoxHeight());
228 // --- Phase 0 ----------------------------------------------------------------------
230 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
231 if (((flags & FONT_CHANGED_FLAG) != 0) && child.font == null) {
232 child.flags |= FONT_CHANGED_FLAG;
233 child.needs_reflow = true;
236 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
238 flags &= ~FONT_CHANGED_FLAG;
240 // --- Phase 1 ----------------------------------------------------------------------
241 // assign children to their row/column positions (assuming constrained columns)
242 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
243 //#repeat x/y y/x width/height col/row row/col cols/rows rows/cols colspan/rowspan rowspan/colspan colWidth/rowHeight numRowsInCol/numColsInRow INNER/INNER2 maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight OUTER/OUTER2 INNER/INNER2
245 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
246 Box child = getChild(0);
247 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
248 OUTER: for(int row=0; child != null; row++) {
249 for(int col=0; child != null && col < cols;) {
250 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
251 while(col < cols && numRowsInCol[col] > row) col++;
252 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
253 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
256 if (col + min(cols, child.colspan) > cols) break;
257 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
260 col += min(cols, child.colspan);
261 child = child.nextSibling();
262 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
268 // --- Phase 2 ----------------------------------------------------------------------
269 // compute the min/max sizes of the columns and rows and set our contentwidth
270 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight numCols/numRows hpad/vpad
271 contentwidth = 2 * hpad;
274 for(Box child = getChild(0); child != null; child = child.nextSibling())
275 numCols = max(numCols, child.col + child.colspan);
276 LENGTH[] colWidth = new LENGTH[numCols];
277 for(Box child = getChild(0); child != null; child = child.nextSibling())
278 if (!(((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
279 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
280 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
281 contentwidth = max(textwidth + 2 * hpad, contentwidth);
282 contentwidth = bound(minwidth, contentwidth, maxwidth);
284 contentwidth = max(contentwidth, path.boundingBoxWidth());
285 contentheight = max(contentheight, path.boundingBoxHeight());
291 private void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
293 // --- Phase 1 ----------------------------------------------------------------------
294 // run PosChange/SizeChange, dirty as needed
295 if (x != this.x || y != this.y || width != this.width || height != this.height) {
296 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
297 boolean sizechange = false, poschange = false;
298 if ((this.width != width || this.height != height) && get("SizeChange", Trap.class) != null) sizechange = true;
299 if ((this.x != x || this.y != y) && get("PosChange", Trap.class) != null) poschange = true;
300 this.width = width; this.height = height; this.x = x; this.y = y;
302 if (sizechange || poschange)
303 if (sizePosChangesSinceLastRender == 500) {
304 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
306 sizePosChangesSinceLastRender++;
307 try { if (sizechange) put("SizeChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
308 try { if (poschange) put("PosChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
309 Surface.abort = true;
315 // --- short circuit ----------------------------------------------------------------
316 if (!needs_reflow) return;
317 needs_reflow = false;
318 if (numChildren() == 0) return;
320 // --- Phase 2 ----------------------------------------------------------------------
321 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
323 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight marginWidth/marginHeight numCols/numRows
326 for(Box child = getChild(0); child != null; child = child.nextSibling())
327 numCols = max(numCols, child.col + child.colspan);
328 LENGTH[] colWidth = new LENGTH[numCols];
329 LENGTH[] colMaxWidth = new LENGTH[numCols];
330 int marginWidth = width;
331 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
334 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
335 if (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)) continue;
336 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight HSHRINK_FLAG/VSHRINK_FLAG numCols/numRows
337 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
338 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
339 colMaxWidth[i] = max(colMaxWidth[i], (((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) / child.colspan);
343 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight marginWidth/marginHeight
344 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
346 for(int i=0; i<colMaxWidth.length; i++) {
347 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
348 marginWidth -= colMaxWidth[i];
349 if (marginWidth < 0) { marginWidth = 0; break; }
354 // --- Phase 3 ----------------------------------------------------------------------
355 // hand out the slack
357 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight numCols/numRows hpad/vpad
358 slack = width - 2 * hpad;
359 for(int i=0; i<numCols; i++) slack -= colWidth[i];
360 if (numChildren() > 0)
362 // FEATURE: inefficient
363 int startslack = slack;
364 int increment = max(1, slack / numCols);
365 for(int col=0; col < numCols && slack > 0; col++) {
366 slack += colWidth[col];
367 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
368 slack -= colWidth[col];
370 if (slack == startslack) break;
375 // --- Phase 4 ----------------------------------------------------------------------
376 // assign children's new sizes and positions and recurse
377 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
378 if ((child.flags & INVISIBLE_FLAG) != 0) continue;
379 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
380 if ((child.flags & ABSOLUTE_FLAG) != 0) {
383 child_width = ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : min(child.maxwidth, width - child.x - hpad);
384 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y - vpad);
387 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight HSHRINK_FLAG/VSHRINK_FLAG marginWidth/marginHeight hpad/vpad child_x/child_y child_width/child_height
388 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
389 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
390 child_x = max(hpad, marginWidth / 2); for(int i=0; i<child.col; i++) child_x += colWidth[i];
391 if (diff < 0) child_x += -1 * (diff / 2);
395 child.resize(child_x, child_y, child_width, child_height);
402 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
404 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
405 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf, VectorGraphics.Affine a) {
406 if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
407 int globalx = parentx + (parent == null ? 0 : x);
408 int globaly = parenty + (parent == null ? 0 : y);
410 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
411 if ((flags & NOCLIP_FLAG) == 0) {
412 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw, (parent == null ? 0 : globalx) + width) - globalx;
413 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph, (parent == null ? 0 : globaly) + height) - globaly;
414 clipx = max(clipx, parent == null ? 0 : globalx);
415 clipy = max(clipy, parent == null ? 0 : globaly);
417 if (clipw <= 0 || cliph <= 0) return;
419 if ((fillcolor & 0xFF000000) != 0x00000000)
420 buf.fillTrapezoid(clipx, clipx + clipw, clipy, clipx, clipx + clipw, clipy + cliph, fillcolor);
423 if ((flags & TILE_FLAG) != 0) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
424 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
426 if (text != null && !text.equals(""))
427 renderText(globalx, globaly, clipx, clipy, clipw, cliph, buf);
430 VectorGraphics.RasterPath rp = path.realize(a);
431 if ((strokecolor & 0xff000000) != 0) rp.stroke(buf, 1, strokecolor);
432 if ((fillcolor & 0xff000000) != 0) rp.fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
435 // now subtract the pad region from the clip region before proceeding
436 if ((flags & NOCLIP_FLAG) == 0) {
437 clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
438 cliph = min(max(clipy, globaly + vpad) + cliph, globaly + height - vpad) - clipy;
439 clipx = max(clipx, globalx + hpad);
440 clipy = max(clipy, globaly + vpad);
443 for(Box b = getChild(0); b != null; b = b.nextSibling()) {
444 a.translate(b.x, b.y);
445 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf, a);
446 a.translate(-1 * b.x, -1 * b.y);
450 void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
452 buf.drawPicture(image, clipx, clipy, clipx + clipw, clipy + cliph, 0, 0, image.getWidth(), image.getHeight());
455 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, PixelBuffer buf) {
456 int iw = image.getWidth();
457 int ih = image.getHeight();
458 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
459 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
461 int dx1 = max(i * iw + x, x);
462 int dy1 = max(j * ih + y, y);
463 int dx2 = min((i+1) * iw + x, x + w);
464 int dy2 = min((j+1) * ih + y, y + h);
466 int sx1 = dx1 - (i*iw) - x;
467 int sy1 = dy1 - (j*ih) - y;
468 int sx2 = dx2 - (i*iw) - x;
469 int sy2 = dy2 - (j*ih) - y;
471 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
472 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
477 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
478 for(int i=0; i<text.length(); i++) {
480 char c = text.charAt(i);
481 Glyph g = Glyph.getGlyph(font, fontsize, c);
482 buf.drawPictureAlphaOnly(g.p,
484 y + vpad + g.max_ascent - g.baseline,
485 x + hpad + g.p.getWidth(),
486 y + vpad + g.max_ascent - g.baseline + g.p.getHeight(),
488 g.p.getWidth(), g.p.getHeight(),
495 // Methods to implement org.xwt.js.JS //////////////////////////////////////
497 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
498 if ("indexof".equals(method)) {
499 if (checkOnly) return Boolean.TRUE;
500 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
501 Box b = (Box)args.elementAt(0);
502 if (b.parent != Box.this) {
503 if (redirect == null || redirect == Box.this) return new Integer(-1);
504 return redirect.callMethod(method, args, checkOnly);
506 return new Integer(b.getIndexInParent());
511 /** Returns the i_th child */
512 public Object get(int i) {
513 if (redirect == null) return null;
514 if (redirect != this) return redirect.get(i);
515 return i >= numChildren() || i < 0 ? null : getChild(i);
519 * Inserts value as child i; calls remove() if necessary.
520 * This method handles "reinserting" one of your children properly.
521 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
522 * WARNING: O(n) runtime, unless i == numChildren()
524 public void put(int i, Object value) {
527 if (value != null && !(value instanceof Box)) {
528 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to anything other than a box");
529 } else if (redirect == null) {
530 if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
531 put("0", (Box)value);
532 } else if (redirect != this) {
533 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
534 redirect.put(i, value);
536 } else if (value == null) {
537 if (i >= 0 && i < numChildren()) {
543 Box newnode = (Box)value;
545 // check if box being moved is currently target of a redirect
546 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
547 if (cur.redirect == newnode) {
548 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
552 // check for recursive ancestor violation
553 for(Box cur = this; cur != null; cur = cur.parent)
554 if (cur == newnode) {
555 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
556 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
560 if (numKids > 15 && children == null) convert_to_array();
562 newnode.parent = this;
564 if (children == null) {
565 if (firstKid == null) {
567 newnode.prevSibling = newnode;
568 newnode.nextSibling = newnode;
569 } else if (i >= numKids) {
570 newnode.prevSibling = firstKid.prevSibling;
571 newnode.nextSibling = firstKid;
572 firstKid.prevSibling.nextSibling = newnode;
573 firstKid.prevSibling = newnode;
576 for(int j=0; j<i; j++) cur = cur.nextSibling;
577 newnode.prevSibling = cur.prevSibling;
578 newnode.nextSibling = cur;
579 cur.prevSibling.nextSibling = newnode;
580 cur.prevSibling = newnode;
581 if (i == 0) firstKid = newnode;
586 if (i >= children.size()) {
587 newnode.indexInParent = children.size();
588 children.addElement(newnode);
590 children.insertElementAt(newnode, i);
591 for(int j=i; j<children.size(); j++)
592 getChild(j).indexInParent = j;
596 // need both of these in case child was already uncalc'ed
599 MARK_FOR_REFLOW_this;
603 // note that JavaScript box[0] will invoke put(int i), not put(String s)
608 public Object get(Object key, Object key2) { return super.get(key, key2); }
609 public void put(Object key, Object key2, Object val) { super.put(key, key2, val); }
611 public Object get_(Object name) { return super.get(name); }
612 public Object get(Object name) { return get(name, false); }
613 public Object get(Object name_, boolean ignoretraps) {
614 if (name_ instanceof Number) return get(((Number)name_).intValue());
616 if (!(name_ instanceof String)) return null;
617 String name = (String)name_;
618 if (name.equals("")) return null;
620 // See if we're triggering a trap
621 Trap t = ignoretraps ? (Trap)null : (Trap)get(name, Trap.class);
622 if (t != null) return t.perform();
624 // Check for a special handler
625 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
626 if (gph != null) return gph.get(this);
628 Object ret = super.get(name);
629 if (name.startsWith("$") && ret == null)
630 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" +
631 name.substring(1) + "\" found");
635 public Object[] keys() {
636 Object[] ret = new Object[numChildren()];
637 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
641 public void addTrap(Object name, Object value) { Trap.addTrap(this, name, ((JS.CompiledFunction)value)); }
642 public void delTrap(Object name, Object value) { Trap.delTrap(this, name, ((JS.CompiledFunction)value)); }
647 * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack)
648 * @param rp if this put is being performed via a root proxy, rp is the root proxy.
650 public void put_(Object name, Object value) { super.put(name, value); }
651 public void put(Object name, Object value) { put(name, value, false); }
652 public void put(Object name_, Object value, boolean ignoretraps) {
653 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
654 if (!(name_ instanceof String)) { super.put(name_,value); return; }
655 String name = name_.toString();
657 Trap t = (Trap)get(name, Trap.class);
664 // don't want to really cascade down to the box on this one
665 if (name.equals("0")) return;
667 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
668 if (gph != null) { gph.put(name, this, value); return; }
670 super.put(name, value);
674 // Tree Manipulation /////////////////////////////////////////////////////////////////////
676 /** The parent of this node */
677 private Box parent = null;
679 // Variables used in Vector mode */
680 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
681 private int indexInParent;
682 private Vec children = null;
684 // Variables used in linked-list mode
685 private int numKids = 0;
686 private Box nextSibling = null;
687 private Box prevSibling = null;
688 private Box firstKid = null;
690 // when we get more than 15 children, we switch to array-mode
691 private void convert_to_array() {
692 children = new Vec(numKids);
695 children.addElement(cur);
696 cur.indexInParent = children.size() - 1;
697 cur = cur.nextSibling;
698 } while (cur != firstKid);
701 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
702 public void remove() {
703 if (parent == null) {
704 if (surface != null) surface.dispose(true);
707 Box oldparent = parent;
708 if (oldparent == null) return;
709 MARK_FOR_REFLOW_this;
711 flags &= ~MOUSEINSIDE_FLAG;
713 if (parent.children != null) {
714 parent.children.removeElementAt(indexInParent);
715 for(int j=indexInParent; j<parent.children.size(); j++)
716 (parent.getChild(j)).indexInParent = j;
719 if (parent.firstKid == this) {
720 if (nextSibling == this) parent.firstKid = null;
721 else parent.firstKid = nextSibling;
724 prevSibling.nextSibling = nextSibling;
725 nextSibling.prevSibling = prevSibling;
731 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
733 // note that JavaScript box[0] will invoke put(int i), not put(String s)
734 if (oldparent != null) oldparent.put("0", this);
737 /** returns our next sibling (parent[ourindex + 1]) */
738 public final Box nextSibling() {
739 if (parent == null) return null;
740 if (parent.children == null) {
741 if (nextSibling == parent.firstKid) return null;
744 if (indexInParent >= parent.children.size() - 1) return null;
745 return (Box)parent.children.elementAt(indexInParent + 1);
749 /** returns our next sibling (parent[ourindex + 1]) */
750 public final Box prevSibling() {
751 if (parent == null) return null;
752 if (parent.children == null) {
753 if (this == parent.firstKid) return null;
756 if (indexInParent == 0) return null;
757 return (Box)parent.children.elementAt(indexInParent - 1);
761 /** Returns the parent of this node */
762 public Box getParent() { return parent; }
764 /** Returns ith child */
765 public Box getChild(int i) {
766 if (children == null) {
767 if (firstKid == null) return null;
768 if (i >= numKids) return null;
769 if (i == numKids - 1) return firstKid.prevSibling;
771 for(int j=0; j<i; j++) cur = cur.nextSibling;
774 if (i >= children.size() || i < 0) return null;
775 return (Box)children.elementAt(i);
779 /** Returns the number of children */
780 public int numChildren() {
781 if (children == null) {
782 if (firstKid == null) return 0;
784 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
787 return children.size();
791 /** Returns our index in our parent */
792 public int getIndexInParent() {
793 if (parent == null) return 0;
794 if (parent.children == null) {
796 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
799 return indexInParent;
803 /** returns the root of the surface that this box belongs to */
804 public final Box getRoot() {
805 if (parent == null && surface != null) return this;
806 if (parent == null) return null;
807 return parent.getRoot();
811 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
813 static final int min(int a, int b) { if (a<b) return a; else return b; }
814 static final double min(double a, double b) { if (a<b) return a; else return b; }
815 static final int max(int a, int b) { if (a>b) return a; else return b; }
816 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; }
817 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; }
818 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
819 final boolean inside(int x, int y) { return (!((flags & INVISIBLE_FLAG) != 0) && x >= 0 && y >= 0 && x < width && y < height); }
821 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
822 public static Box whoIs(Box cur, int x, int y) {
824 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
828 // WARNING: this method is called from the event-queueing
829 // thread -- it may run concurrently with ANY part of XWT, and
830 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
833 if ((cur.flags & INVISIBLE_FLAG) != 0) return null;
834 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
836 for(int i=cur.numChildren() - 1; i>=0; i--) {
837 Box child = cur.getChild(i);
838 if (child == null) continue; // since this method is unsynchronized, we have to double-check
841 if (!((child.flags & INVISIBLE_FLAG) != 0) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
851 * A helper class for properties of Box which require special
854 * To avoid excessive use of String.equals(), the Box.get() and
855 * Box.put() methods employ a Hash keyed on property names that
856 * require special handling. The value stored in the Hash is an
857 * instance of an anonymous subclass of SpecialBoxProperty, which knows
858 * how to handle get()s and put()s for that property name. There
859 * should be one anonymous subclass of SpecialBoxProperty for each
860 * specially-handled property on Box.
862 static class SpecialBoxProperty {
864 SpecialBoxProperty() { }
866 /** stores instances of SpecialBoxProperty; keyed on property name */
867 static Hash specialBoxProperties = new Hash(200, 3);
869 /** this method defines the behavior when the property is get()ed from b */
870 Object get(Box b) { return null; }
872 /** this method defines the behavior when the property is put() to b */
873 void put(Box b, Object value) { }
875 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
876 void put(String name, Box b, Object value) { put(b, value); }
879 specialBoxProperties.put("fill", new ColorBoxProperty() {
880 public int getColor(Box b) { return b.fillcolor; }
881 public void putColor(Box b, int argb) { b.fillcolor = argb; }
883 specialBoxProperties.put("textcolor", new ColorBoxProperty() {
884 public int getColor(Box b) { return b.textcolor; }
885 public void putColor(Box b, int argb) { b.textcolor = argb; }
887 specialBoxProperties.put("strokecolor", new ColorBoxProperty() {
888 public int getColor(Box b) { return b.strokecolor; }
889 public void putColor(Box b, int argb) { b.strokecolor = argb; }
892 specialBoxProperties.put("text", new SpecialBoxProperty() {
893 public Object get(Box b) { return b.text; }
894 public void put(Box b, Object value) {
895 String t = value == null ? "null" : value.toString();
896 if (t.equals(b.text)) return;
899 if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
900 b.textwidth = b.textheight = 0;
905 for(int i=0; i<b.text.length(); i++) {
906 Glyph g = Glyph.getGlyph(b.font, b.fontsize, b.text.charAt(i));
907 b.textwidth += g.advance;
908 b.textheight = g.max_ascent + g.max_descent;
910 } catch (Exception e) {
917 specialBoxProperties.put("path", new SpecialBoxProperty() {
918 public Object get(Box b) { throw new JS.Exn("cannot read from the path property"); }
919 public void put(Box b, Object value) {
920 String t = value == null ? "null" : value.toString();
921 b.path = value == null ? null : VectorGraphics.parseVectorPath(value.toString());
926 specialBoxProperties.put("transform", new SpecialBoxProperty() {
927 public void put(Box b, Object value) {
928 String t = value.toString().trim();
929 b.transform = VectorGraphics.Affine.identity();
930 while (t.length() > 0) {
931 if (t.startsWith("skewX(")) {
934 } else if (t.startsWith("shear(")) {
935 // FIXME: nonstandard; remove this
936 b.transform.multiply(VectorGraphics.Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
938 } else if (t.startsWith("skewY(")) {
941 } else if (t.startsWith("rotate(")) {
942 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
943 if (sub.indexOf(',') != -1) {
944 float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
945 sub = sub.substring(sub.indexOf(',') + 1);
946 float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
947 sub = sub.substring(sub.indexOf(',') + 1);
948 float cy = Float.parseFloat(sub);
949 b.transform.multiply(VectorGraphics.Affine.translate(cx, cy));
950 b.transform.multiply(VectorGraphics.Affine.rotate(angle));
951 b.transform.multiply(VectorGraphics.Affine.translate(-1 * cx, -1 * cy));
953 b.transform.multiply(VectorGraphics.Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
956 } else if (t.startsWith("translate(")) {
957 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
958 if (sub.indexOf(',') > -1) {
959 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
960 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
962 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
965 } else if (t.startsWith("flip(")) {
966 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
967 b.transform.multiply(VectorGraphics.Affine.flip(which.equals("horizontal"), which.equals("vertical")));
969 } else if (t.startsWith("scale(")) {
970 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
971 if (sub.indexOf(',') > -1) {
972 b.transform.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
973 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
975 b.transform.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
976 Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
979 } else if (t.startsWith("matrix(")) {
980 // FIXME: is this mapped right?
981 float d[] = new float[6];
982 StringTokenizer st = new StringTokenizer(t, ",", false);
983 for(int i=0; i<6; i++)
984 d[i] = Float.parseFloat(st.nextToken());
985 b.transform.multiply(new VectorGraphics.Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
987 t = t.substring(t.indexOf(')') + 1).trim();
991 public Object get(Box b) { return b.transform.toString(); }
994 specialBoxProperties.put("font", new SpecialBoxProperty() {
995 public Object get(Box b) { return b.font; }
996 public void put(Box b, Object value) {
997 // FIXME: translate value into a resource if it is a string
998 b.font = value == null ? null : (Res)value;
1000 b.flags |= FONT_CHANGED_FLAG;
1004 specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
1005 public Object get(Box b) { return b.font; }
1006 public void put(Box b, Object value) {
1007 if (b.fontsize == stoi(value)) return;
1008 b.fontsize = stoi(value);
1010 b.flags |= FONT_CHANGED_FLAG;
1014 specialBoxProperties.put("strokewidth", new SpecialBoxProperty() {
1015 public Object get(Box b) { return new Integer(b.strokewidth); }
1016 public void put(Box b, Object value) {
1017 if (b.strokewidth == stoi(value)) return;
1018 b.strokewidth = stoi(value);
1022 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1023 public Object get(Box b) { return b; }
1024 public void put(Box b, Object value) {
1025 if (value == null) b.remove();
1026 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1027 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1031 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1032 public Object get(Box b) {
1033 Log.log(this, "warning: the orient property is deprecated");
1034 if (b.redirect == null) return "horizontal";
1035 else if (b.redirect != b) return get(b.redirect);
1036 else if (b.cols == 1) return "vertical";
1037 else if (b.rows == 1) return "horizontal";
1040 public void put(Box b, Object value) {
1041 Log.log(this, "warning: the orient property is deprecated");
1042 if (value == null) return;
1043 if (b.redirect == null) return;
1044 if (b.redirect != b) { put(b.redirect, value); return; }
1045 if (value.equals("vertical")) {
1046 if (b.rows == 0) return;
1047 b.rows = 0; b.cols = 1;
1048 } else if (value.equals("horizontal")) {
1049 if (b.cols == 0) return;
1050 b.cols = 0; b.rows = 1;
1052 Log.log(this, "invalid value put to orient property: " + value);
1056 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1057 public Object get(Box b) { return (((b.flags & HSHRINK_FLAG) != 0) || ((b.flags & VSHRINK_FLAG) != 0)) ? Boolean.TRUE : Boolean.FALSE; }
1058 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1061 //#repeat hshrink/vshrink HSHRINK_FLAG/VSHRINK_FLAG
1062 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1063 public Object get(Box b) { return new Boolean((b.flags & HSHRINK_FLAG) != 0); }
1064 public void put(Box b, Object value) {
1065 boolean newshrink = stob(value);
1066 if (((b.flags & HSHRINK_FLAG) != 0) == newshrink) return;
1067 if (newshrink) b.flags |= HSHRINK_FLAG; else b.flags &= ~HSHRINK_FLAG;
1074 specialBoxProperties.put("x", new SpecialBoxProperty() {
1075 public Object get(Box b) {
1076 if (b.surface == null) return new Integer(0);
1077 if ((b.flags & INVISIBLE_FLAG) != 0) return new Integer(0);
1078 return new Integer(b.x);
1080 public void put(Box b, Object value) {
1081 if (!((b.flags & ABSOLUTE_FLAG) != 0)) return;
1082 int x = stoi(value);
1083 if (x == b.x) return;
1086 if (b.parent == null && b.surface != null) b.surface.setLocation();
1093 //#repeat width/height minwidth/minheight maxwidth/maxheight
1094 specialBoxProperties.put("width", new SpecialBoxProperty() {
1095 public Object get(Box b) { return new Integer(b.width); }
1096 public void put(Box b, Object value) {
1097 b.width = stoi(value);
1098 if (b.parent == null && b.surface != null) {
1099 b.surface.setSize();
1102 if (b.minwidth == b.width && b.maxwidth == b.width) return;
1103 b.minwidth = b.maxwidth = b.width;
1109 //#repeat cols/rows rows/cols
1110 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1111 public Object get(Box b) { return new Double(b.cols); }
1112 public void put(Box b, Object value) {
1113 if (b.cols == stoi(value)) return;
1114 b.cols = stoi(value);
1115 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1116 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1121 //#repeat colspan/rowspan
1122 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1123 public Object get(Box b) { return new Double(b.colspan); }
1124 public void put(Box b, Object value) {
1125 if (b.colspan == stoi(value)) return;
1126 b.colspan = stoi(value);
1132 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1133 public Object get(Box b) { return ((b.flags & TILE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1134 public void put(Box b, Object value) {
1135 if (((b.flags & TILE_FLAG) != 0) == stob(value)) return;
1136 if (stob(value)) b.flags |= TILE_FLAG; else b.flags &= ~TILE_FLAG;
1140 specialBoxProperties.put("noclip", new SpecialBoxProperty() {
1141 public Object get(Box b) { return ((b.flags & NOCLIP_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1142 public void put(Box b, Object value) {
1143 if (((b.flags & NOCLIP_FLAG) != 0) == stob(value)) return;
1144 if (stob(value)) b.flags |= NOCLIP_FLAG; else b.flags &= ~NOCLIP_FLAG;
1149 specialBoxProperties.put("visible", new SpecialBoxProperty() {
1150 public Object get(Box b) {
1151 for (Box cur = b; cur != null; cur = cur.parent) {
1152 if ((cur.flags & INVISIBLE_FLAG) != 0) return Boolean.FALSE; }
1153 return Boolean.TRUE;
1155 public void put(Box b, Object value) {
1156 if (!stob(value) == ((b.flags & INVISIBLE_FLAG) != 0)) return;
1157 if (!stob(value)) b.flags |= INVISIBLE_FLAG; else b.flags &= ~INVISIBLE_FLAG;
1158 if (b.parent == null) {
1159 if (b.surface != null) b.surface.setInvisible((b.flags & INVISIBLE_FLAG) != 0);
1162 MARK_FOR_REFLOW_b_parent;
1163 b.parent.dirty(b.x, b.y, b.width, b.height);
1167 specialBoxProperties.put("absolute", new SpecialBoxProperty() {
1168 public Object get(Box b) { return ((b.flags & ABSOLUTE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1169 public void put(Box b, Object value) {
1170 if (stob(value) == ((b.flags & ABSOLUTE_FLAG) != 0)) return;
1171 if (stob(value)) b.flags |= ABSOLUTE_FLAG; else b.flags &= ~ABSOLUTE_FLAG;
1172 if ((b.flags & ABSOLUTE_FLAG) != 0) { b.x = 0; b.y = 0; }
1173 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1176 specialBoxProperties.put("image", new SpecialBoxProperty() {
1178 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1180 public void put(Box b, Object value) {
1181 //if (image != null) System.out.println("hit");
1182 if (value == null) {
1184 } else if (value instanceof Res) {
1185 b.image = Picture.fromRes((Res)value);
1189 b.minwidth = min(b.maxwidth, max(b.minwidth, b.image == null ? 0 : b.image.getWidth()));
1190 b.minheight = min(b.maxheight, max(b.minheight, b.image == null ? 0 : b.image.getHeight()));
1196 //#repeat globalx/globaly x/y
1197 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1198 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1199 public void put(Box b, Object value) {
1200 if (b.surface == null || b.parent == null) return;
1201 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1207 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1208 public Object get(Box b) { return b.cursor; }
1209 public void put(Box b, Object value) {
1210 b.cursor = (String)value;
1211 if (b.surface == null) return;
1213 // see if we need to update the surface cursor
1214 Surface surface = b.getRoot().surface;
1215 String tempcursor = surface.cursor;
1216 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1217 if (surface.cursor != tempcursor) surface.syncCursor();
1221 //#repeat mousex/mousey x/y
1222 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1223 public Object get(Box b) {
1224 Surface surface = b.getRoot().surface;
1225 if (surface == null) return new Integer(0);
1226 int mousex = surface.mousex;
1227 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1228 return new Integer(mousex);
1233 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1234 public Object get(Box b) {
1235 return ((b.flags & MOUSEINSIDE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1238 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1239 public Object get(Box b) {
1240 if (b.redirect == null) return new Integer(0);
1241 if (b.redirect != b) return get(b.redirect);
1242 return new Integer(b.numChildren());
1245 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1246 public void put(String name, Box b, Object value) {
1247 Surface surface = b.getRoot().surface;
1248 if (surface == null) return;
1249 int mousex = surface.mousex;
1250 int mousey = surface.mousey;
1251 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1255 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1256 if (c.inside(mousex - c.x, mousey - c.y)) {
1261 if (b.parent != null) b.parent.put(name, value);
1264 specialBoxProperties.put("Press1", mouseEventHandler);
1265 specialBoxProperties.put("Press2", mouseEventHandler);
1266 specialBoxProperties.put("Press3", mouseEventHandler);
1267 specialBoxProperties.put("Release1", mouseEventHandler);
1268 specialBoxProperties.put("Release2", mouseEventHandler);
1269 specialBoxProperties.put("Release3", mouseEventHandler);
1270 specialBoxProperties.put("Click1", mouseEventHandler);
1271 specialBoxProperties.put("Click2", mouseEventHandler);
1272 specialBoxProperties.put("Click3", mouseEventHandler);
1273 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1274 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1275 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1277 specialBoxProperties.put("root", new SpecialBoxProperty() {
1278 public void put(Box b, Object value) {
1279 if (stob(value)) b.flags |= ISROOT_FLAG;
1280 else b.flags &= ~ISROOT_FLAG;
1282 public Object get(Box b) {
1283 if (b.parent == null || ((b.flags & ISROOT_FLAG) != 0)) return b;
1284 else return get(b.parent);
1287 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1288 public Object get(Box b) {
1289 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1292 public void put(Box b, Object value) {
1293 if (b.surface == null) return;
1294 boolean val = stob(value);
1295 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1299 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1300 public Object get(Box b) {
1301 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1304 public void put(Box b, Object value) {
1305 if (b.surface == null) return;
1306 boolean val = stob(value);
1307 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1311 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1312 public void put(Box b, Object value) {
1313 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1317 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1318 public void put(Box b, Object value) {
1319 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1323 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1324 public void put(Box b, Object value) {
1325 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1329 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1330 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
1331 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
1332 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1333 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1336 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
1337 public Object get(Box b) {
1338 if (b.redirect == null) return new Integer(0);
1339 if (b.redirect != b) return get(b.redirect);
1340 return new Integer(b.hpad);
1342 public void put(Box b, Object value) {
1343 if (b.redirect == null) return;
1344 if (b.redirect != b) { put(b.redirect, value); return; }
1345 int newval = stoi(value);
1346 if (newval == b.hpad) return;
1353 //#repeat minwidth/minheight maxwidth/maxheight
1354 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1355 public Object get(Box b) { return new Integer(b.minwidth); }
1356 public void put(Box b, Object value) {
1357 if (stoi(value) == b.minwidth) return;
1358 b.minwidth = stoi(value);
1362 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1363 public Object get(Box b) { return new Integer(b.maxwidth); }
1364 public void put(Box b, Object value) {
1365 if (stoi(value) == b.maxwidth) return;
1366 b.maxwidth = stoi(value);
1372 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1373 public void put(Box b, Object value) {
1374 if (b.redirect != b) Log.log(this, "cannot change the redirect of a box once it is set");
1375 else if (value == null) b.redirect = null;
1377 Box b2 = (Box)value;
1378 while(b2 != b && b2 != null) b2 = b2.parent;
1379 if (b2 == null) Log.log(this, "a box's redirect must be one of its descendants");
1380 b.redirect = (Box)value;
1383 public Object get(Box b) {
1384 if (b.redirect == null) return null;
1385 if (b.redirect == b) return Boolean.TRUE;
1386 return get(b.redirect);
1390 // FEATURE: this still isn't totally harmonious; when you createSurface, these aren't checked
1391 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1392 public void put(Box b, Object value) {
1393 if (b.surface != null) b.surface.setTitleBarText(value.toString());
1394 b.put_("titlebar", value);
1396 public Object get(Box b) { return b.get_("titlebar"); }
1399 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1402 public void put(Box b, Object value) {
1403 b.put_("icon", value);
1404 if (b.surface == null) return;
1405 Picture pic = ImageDecoder.getPicture(value.toString());
1406 if (pic != null) b.surface.setIcon(pic);
1407 else if (Log.on) Log.log(this, "unable to load icon " + value);
1409 public Object get(Box b) { return b.get_("icon"); }
1414 private static abstract class ColorBoxProperty extends SpecialBoxProperty {
1415 public abstract int getColor(Box b);
1416 public abstract void putColor(Box b, int argb);
1417 public Object get(Box b) {
1418 if ((getColor(b) & 0xFF000000) == 0) return null;
1419 String red = Integer.toHexString((getColor(b) & 0x00FF0000) >> 16);
1420 String green = Integer.toHexString((getColor(b) & 0x0000FF00) >> 8);
1421 String blue = Integer.toHexString(getColor(b) & 0x000000FF);
1422 if (red.length() < 2) red = "0" + red;
1423 if (blue.length() < 2) blue = "0" + blue;
1424 if (green.length() < 2) green = "0" + green;
1425 return "#" + red + green + blue;
1427 public void put(Box b, Object value) {
1428 int newcolor = getColor(b);
1429 String s = value == null ? null : value.toString();
1430 if (value == null) newcolor = 0x00000000;
1431 else if (s.length() > 0 && s.charAt(0) == '#')
1433 newcolor = 0xFF000000 |
1434 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
1435 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
1436 Integer.parseInt(s.substring(5, 7), 16);
1437 } catch (NumberFormatException e) {
1438 Log.log(this, "invalid color " + s);
1441 else if (org.xwt.translators.SVG.colors.get(s) != null)
1442 newcolor = 0xFF000000 | ((Integer)org.xwt.translators.SVG.colors.get(s)).intValue();
1443 if (newcolor == getColor(b)) return;
1444 putColor(b, newcolor);
1450 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1451 public static boolean stob(Object o) {
1452 if (o == null) return false;
1453 return Boolean.TRUE.equals(o) || "true".equals(o);
1456 /** helper that converts a String to an int according to JavaScript coercion rules */
1457 public static int stoi(Object o) {
1458 if (o == null) return 0;
1459 if (o instanceof Integer) return ((Integer)o).intValue();
1462 if (!(o instanceof String)) s = o.toString();
1465 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1466 catch (NumberFormatException e) { return 0; }