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>
10 // FIXME use bitfields
12 // FIXME: reflow before allowing js to read from width/height
13 // FIXME: due to font inheritance, we must dirty and mark all null-font descendents of a node if its font changes
14 // FEATURE: fastpath for rows=1/cols=1
15 // FEATURE: reflow starting with a certain child
16 // FEATURE: separate mark_for_reflow and mark_for_resize
22 import org.xwt.util.*;
26 * Encapsulates the data for a single XWT box as well as all layout
31 * This is the real meat of XWT. Part of its monolithic design is for
32 * performance reasons: deep inheritance heirarchies are slow, and
33 * neither javago nor GCJ can inline across class boundaries.
36 * <p>The rendering process consists of three phases; each requires
37 * one DFS pass over the tree</p>
39 * <ol><li> <b>repacking</b>: children of a box are packed into columns
40 * and rows according to their colspan/rowspan attributes and
41 * ordering. Minimum and maximum sizes of columns are computed.
43 * <li> <b>resizing</b>: width/height and x/y positions of children
44 * are assigned. If a PosChange or SizeChange is triggered,
45 * <tt>Surface.abort</tt> will be set and the resizing process will
48 * <li> <b>repainting</b>: children draw their content onto the
52 * The first two passes together are called the <i>reflow</i> phase.
54 * Reflowing is done in a seperate pass since PosChanges and
55 * SizeChanges trigger an Surface.abort; if rendering were done in the same
56 * pass, rendering work done prior to the Surface.abort would be wasted.
58 * Repacking is seperate from resizing since a box's size depends on
59 * both the box's parent's size (so the traversal must be preorder)
60 * contentwidths of siblings both before and after the box, which in
61 * turn depend on all descendents of the siblings. FIXME
63 * A note on coordinates: the Box class represents regions
64 * internally as x,y,w,h tuples, even though the DoubleBuffer class
65 * uses x1,y1,x2,y2 tuples.
67 public final class Box extends JS.Scope {
69 public Box() { super(null); }
72 // Misc instance data ////////////////////////////////////////////////////////////////
74 private static int sizePosChangesSinceLastRender = 0;
77 // Misc instance data ////////////////////////////////////////////////////////////////
79 boolean needs_reflow = true;
80 //#define MARK_FOR_REFLOW_this for(Box b2 = this; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
81 //#define MARK_FOR_REFLOW_b for(Box b2 = b; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
82 //#define MARK_FOR_REFLOW_b_parent for(Box b2 = b.parent; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
84 private boolean mouseinside = false;
86 Surface surface = null; // null on all non-root boxen
90 // Geometry ////////////////////////////////////////////////////////////////////////////
92 // xwt can be compiled with 16-bit lengths to save memory on small devices
94 //#define MAX_LENGTH Integer.MAX_VALUE
95 //#define MIN_LENGTH Integer.MIN_VALUE
97 // always correct (set directly by user)
100 LENGTH maxwidth = MAX_LENGTH;
101 LENGTH maxheight = MAX_LENGTH;
102 private LENGTH hpad = 0;
103 private LENGTH vpad = 0;
104 private String text = null;
105 private String font = null;
106 private LENGTH textwidth = 0;
107 private LENGTH textheight = 0;
110 private int rows = 1;
111 private int cols = 0;
112 private int rowspan = 1;
113 private int colspan = 1;
115 // computed during reflow
118 public LENGTH width = 0;
119 public LENGTH height = 0;
120 private int row = 0; // FIXME short
121 private int col = 0; // FIXME short
122 private LENGTH contentwidth = 0; // == max(minwidth, textwidth, sum(child.contentwidth) + pad)
123 private LENGTH contentheight = 0;
126 // Rendering Properties ///////////////////////////////////////////////////////////
128 //private SVG.VP path = null;
129 //private SVG.Paint fill = null;
130 //private SVG.Paint stroke = null;
132 private Picture image; // will disappear
133 private int fillcolor = 0x00000000; // will become SVG.Paint
134 private int strokecolor = 0xFF000000; // will become SVG.Paint
136 private String cursor = null; // the cursor for this box
139 public boolean invisible = false; // true iff the Box is invisible
140 private boolean absolute = false; // If true, the box will be positioned absolutely
141 private boolean vshrink = false; // If true, the box will shrink to the smallest vertical size possible
142 private boolean hshrink = false; // If true, the box will shrink to the smallest horizontal size possible
143 private boolean tile = false; // FIXME: drop this?
145 // Instance Methods /////////////////////////////////////////////////////////////////////
148 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
149 public final void dirty() { dirty(0, 0, width, height); }
150 public final void dirty(int x, int y, int w, int h) {
151 for(Box cur = this; cur != null; cur = cur.parent) {
152 w = min(x + w, cur.width) - max(x, 0);
153 h = min(y + h, cur.height) - max(y, 0);
156 if (w <= 0 || h <= 0) return;
157 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
164 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
165 * to see if this node requires any Enter, Leave, or Move notifications.
167 * @param forceleave set to true by the box's parent if the mouse is inside an older
168 * sibling, which is covering up this box.
170 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
171 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
173 boolean wasinside = mouseinside;
174 boolean isinside = !invisible && inside(mousex, mousey) && !forceleave;
175 mouseinside = isinside;
177 if (!wasinside && !isinside) return;
179 if (traps == null) { }
180 else if (!wasinside && isinside && traps.get("Enter") != null) put("Enter", Boolean.TRUE);
181 else if (wasinside && !isinside && traps.get("Leave") != null) put("Leave", Boolean.TRUE);
182 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && traps.get("Move") != null) put("Move", Boolean.TRUE);
184 if (isinside && cursor != null) getRoot().cursor = cursor;
186 // if the mouse has moved into our padding region, it is considered 'outside' all our children
187 if (!(mousex >= hpad && mousey >= vpad && mousex < width - hpad && mousey < height + vpad)) forceleave = true;
189 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
190 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
191 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
196 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
200 if (Surface.abort) return;
201 resize(x, y, width, height);
204 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
206 if (!needs_reflow) return;
207 if (numChildren() == 0) {
208 contentwidth = max(textwidth, minwidth);
209 contentheight = max(textheight, minheight);
213 // --- Phase 0 ----------------------------------------------------------------------
215 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
217 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
220 // --- Phase 1 ----------------------------------------------------------------------
221 // assign children to their row/column positions (assuming constrained columns)
222 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
223 //#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
225 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
226 Box child = getChild(0);
227 for(; child != null && (child.absolute || child.invisible); child = child.nextSibling());
228 OUTER: for(int row=0; child != null; row++) {
229 for(int col=0; child != null && col < cols;) {
230 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
231 while(col < cols && numRowsInCol[col] > row) col++;
232 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
233 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
236 if (col + min(cols, child.colspan) > cols) break;
237 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
240 col += min(cols, child.colspan);
241 child = child.nextSibling();
242 for(; child != null && (child.absolute || child.invisible); child = child.nextSibling());
248 // --- Phase 2 ----------------------------------------------------------------------
249 // compute the min/max sizes of the columns and rows and set our contentwidth
250 //#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
251 contentwidth = 2 * hpad;
254 for(Box child = getChild(0); child != null; child = child.nextSibling())
255 numCols = max(numCols, child.col + child.colspan);
256 LENGTH[] colWidth = new LENGTH[numCols];
257 for(Box child = getChild(0); child != null; child = child.nextSibling())
258 if (!(child.absolute || child.invisible))
259 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
260 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
261 contentwidth = max(textwidth + 2 * hpad, contentwidth);
262 contentwidth = bound(minwidth, contentwidth, maxwidth);
267 void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
269 // --- Phase 1 ----------------------------------------------------------------------
270 // run PosChange/SizeChange, dirty as needed
271 if (x != this.x || y != this.y || width != this.width || height != this.height) {
272 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
273 boolean sizechange = false, poschange = false;
274 if (traps != null && (this.width != width || this.height != height) && traps.get("SizeChange") != null) sizechange = true;
275 if (traps != null && (this.x != x || this.y != y) && traps.get("PosChange") != null) poschange = true;
276 this.width = width; this.height = height; this.x = x; this.y = y;
278 if (sizechange || poschange)
279 if (sizePosChangesSinceLastRender == 500) {
280 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
282 sizePosChangesSinceLastRender++;
283 if (sizechange) put("SizeChange", Boolean.TRUE);
284 if (poschange) put("PosChange", Boolean.TRUE);
285 Surface.abort = true;
291 // --- short circuit ----------------------------------------------------------------
292 if (!needs_reflow) return;
293 needs_reflow = false;
294 if (numChildren() == 0) return;
296 // --- Phase 2 ----------------------------------------------------------------------
297 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
299 //#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
302 for(Box child = getChild(0); child != null; child = child.nextSibling())
303 numCols = max(numCols, child.col + child.colspan);
304 LENGTH[] colWidth = new LENGTH[numCols];
305 LENGTH[] colMaxWidth = new LENGTH[numCols];
306 int marginWidth = width;
307 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
310 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
311 if (child.absolute || child.invisible) continue;
312 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight hshrink/vshrink numCols/numRows
313 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
314 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
315 colMaxWidth[i] = max(colMaxWidth[i], (child.hshrink ? child.contentwidth : child.maxwidth) / child.colspan);
319 //#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
320 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
322 for(int i=0; i<colMaxWidth.length; i++) {
323 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
324 marginWidth -= colMaxWidth[i];
325 if (marginWidth < 0) { marginWidth = 0; break; }
330 // --- Phase 3 ----------------------------------------------------------------------
331 // hand out the slack
333 //#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
335 for(int i=0; i<numCols; i++) slack -= colWidth[i];
336 if (numChildren() > 0)
338 // FEATURE: inefficient
339 int startslack = slack;
340 int increment = max(1, slack / numCols);
341 for(int col=0; col < numCols && slack > 0; col++) {
342 slack += colWidth[col];
343 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
344 slack -= colWidth[col];
346 if (slack == startslack) break;
351 // --- Phase 4 ----------------------------------------------------------------------
352 // assign children's new sizes and positions and recurse
353 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
354 if (child.invisible) continue;
355 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
356 if (child.absolute) {
359 child_width = child.hshrink ? child.contentwidth : min(child.maxwidth, width - child.x - hpad);
360 child_height = child.vshrink ? child.contentheight : min(child.maxheight, height - child.y - vpad);
363 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight hshrink/vshrink marginWidth/marginHeight hpad/vpad child_x/child_y child_width/child_height
364 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
365 diff = bound(child.contentwidth, child_width, child.hshrink ? child.contentwidth : child.maxwidth) - child_width;
366 child_x = max(hpad, marginWidth / 2); for(int i=0; i<child.col; i++) child_x += colWidth[i];
367 if (diff < 0) child_x += -1 * (diff / 2);
371 child.resize(child_x, child_y, child_width, child_height);
378 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
380 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
381 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
382 if (Surface.abort || invisible) return;
383 int globalx = parentx + (parent == null ? 0 : x);
384 int globaly = parenty + (parent == null ? 0 : y);
386 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
387 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw, (parent == null ? 0 : globalx) + width) - globalx;
388 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph, (parent == null ? 0 : globaly) + height) - globaly;
389 clipx = max(clipx, parent == null ? 0 : globalx);
390 clipy = max(clipy, parent == null ? 0 : globaly);
391 if (clipw <= 0 || cliph <= 0) return;
393 if ((fillcolor & 0xFF000000) != 0x00000000 || parent == null)
394 buf.fillRect(clipx, clipy, clipx + clipw, clipy + cliph, (fillcolor & 0xFF000000) != 0 ? fillcolor : 0xFF777777);
397 if (tile) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
398 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
400 if (text != null && !text.equals("")) renderText(x, y, clipx, clipy, clipw, cliph, buf);
402 // now subtract the pad region from the clip region before proceeding
403 clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
404 cliph = min(max(clipy, globaly + vpad) + cliph, globaly + height - vpad) - clipy;
405 clipx = max(clipx, globalx + hpad);
406 clipy = max(clipy, globaly + vpad);
408 for(Box b = getChild(0); b != null; b = b.nextSibling()) b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf);
411 void renderStretchedImage(int globalx, int globaly, int x, int y, int w, int h, DoubleBuffer buf) {
412 buf.setClip(x, y, w + x, h + y);
416 int hstretch = width / image.getWidth();
417 if (hstretch == 0) hstretch = -1 * image.getWidth() / width;
418 int vstretch = height / image.getHeight();
419 if (vstretch == 0) vstretch = -1 * image.getHeight() / height;
420 if (hstretch < vstretch) height = image.getHeight() * width / image.getWidth();
421 else width = image.getWidth() * height / image.getHeight();
425 buf.drawPicture(image, globalx, globaly, globalx + width, globaly + height, 0, 0, image.getWidth(), image.getHeight());
426 buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
429 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, DoubleBuffer buf) {
430 int iw = image.getWidth();
431 int ih = image.getHeight();
433 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
434 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
436 int dx1 = max(i * iw + x, x);
437 int dy1 = max(j * ih + y, y);
438 int dx2 = min((i+1) * iw + x, x + w);
439 int dy2 = min((j+1) * ih + y, y + h);
441 int sx1 = dx1 - (i*iw) - x;
442 int sy1 = dy1 - (j*ih) - y;
443 int sx2 = dx2 - (i*iw) - x;
444 int sy2 = dy2 - (j*ih) - y;
446 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
447 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
452 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
454 Log.log(this, "rendering text: " + text);
455 //buf.setClip(clipx, clipy, clipw, cliph);
458 ImageDecoder id = org.xwt.imp.Font.render(new FileInputStream("COMIC.TTF"), 24, text);
459 Picture p = Platform.createPicture(id);
460 buf.drawPicture(p, x, y);
461 buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
462 } catch (Exception e) {
467 buf.drawString(font(), text, x + hpad, y + vpad + Platform.getMaxAscent(font()) - 1, textcolor);
468 buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
469 int i=0; while(i<font().length() && !Character.isDigit(font().charAt(i))) i++;
471 if (font().lastIndexOf('d') > i) {
472 for(int j = x + hpad; j < x + hpad + textdim(0); j += 2)
473 buf.fillRect(j, y + vpad + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2,
474 j + 1, y + vpad + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2 + 1,
477 } else if (font().lastIndexOf('u') > i) {
478 buf.fillRect(x + hpad,
479 y + vpad + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2,
480 x + hpad + textdim(0),
481 y + vpad + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2 + 1,
488 // Methods to implement org.xwt.js.JS //////////////////////////////////////
490 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
491 if ("indexof".equals(method)) {
492 if (checkOnly) return Boolean.TRUE;
493 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
494 Box b = (Box)args.elementAt(0);
495 if (b.parent != Box.this) {
496 if (redirect == null || redirect == Box.this) return new Integer(-1);
497 return redirect.callMethod(method, args, checkOnly);
499 return new Integer(b.getIndexInParent());
501 } else if ("apply".equals(method)) {
502 if (checkOnly) return Boolean.TRUE;
503 if (args.elementAt(0) instanceof String) {
504 String templatename = (String)args.elementAt(0);
505 Template t = Template.getTemplate(templatename, null);
507 if (Log.on) Log.logJS(this, "template " + templatename + " not found");
509 if (ThreadMessage.suspendThread()) try {
510 JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
511 t.apply(this, null, null, callback, 0, t.numUnits());
513 ThreadMessage.resumeThread();
516 } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
517 JS s = (JS)args.elementAt(0);
518 Object[] keys = s.keys();
519 for(int j=0; j<keys.length; j++) put(keys[j].toString(), s.get(keys[j]));
526 /** Returns the i_th child */
527 public Object get(int i) {
528 if (redirect == null) return null;
529 if (redirect != this) return redirect.get(i);
530 return i >= numChildren() || i < 0 ? null : getChild(i);
534 * Inserts value as child i; calls remove() if necessary.
535 * This method handles "reinserting" one of your children properly.
536 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
537 * WARNING: O(n) runtime, unless i == numChildren()
539 public void put(int i, Object value) {
542 if (value != null && !(value instanceof Box)) {
543 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to anything other than a box");
544 } else if (redirect == null) {
545 if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
546 } else if (redirect != this) {
547 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
548 redirect.put(i, value);
550 } else if (value == null) {
551 if (i >= 0 && i < numChildren()) {
556 } else if (value instanceof RootProxy) {
557 if (Log.on) Log.logJS(this, "attempt to reparent a box via its proxy object");
559 Box newnode = (Box)value;
561 // check if box being moved is currently target of a redirect
562 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
563 if (cur.redirect == newnode) {
564 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
568 // check for recursive ancestor violation
569 for(Box cur = this; cur != null; cur = cur.parent)
570 if (cur == newnode) {
571 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
572 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
576 if (numKids > 15 && children == null) convert_to_array();
578 newnode.parent = this;
580 if (children == null) {
581 if (firstKid == null) {
583 newnode.prevSibling = newnode;
584 newnode.nextSibling = newnode;
585 } else if (i >= numKids) {
586 newnode.prevSibling = firstKid.prevSibling;
587 newnode.nextSibling = firstKid;
588 firstKid.prevSibling.nextSibling = newnode;
589 firstKid.prevSibling = newnode;
592 for(int j=0; j<i; j++) cur = cur.nextSibling;
593 newnode.prevSibling = cur.prevSibling;
594 newnode.nextSibling = cur;
595 cur.prevSibling.nextSibling = newnode;
596 cur.prevSibling = newnode;
597 if (i == 0) firstKid = newnode;
602 if (i >= children.size()) {
603 newnode.indexInParent = children.size();
604 children.addElement(newnode);
606 children.insertElementAt(newnode, i);
607 for(int j=i; j<children.size(); j++)
608 getChild(j).indexInParent = j;
612 // need both of these in case child was already uncalc'ed
615 MARK_FOR_REFLOW_this;
619 // note that JavaScript box[0] will invoke put(int i), not put(String s)
624 public Object get(Object name) { return get(name, false); }
625 public Object get(Object name_, boolean ignoretraps) {
626 if (name_ instanceof Number) return get(((Number)name_).intValue());
628 if (!(name_ instanceof String)) return null;
629 String name = (String)name_;
630 if (name.equals("")) return null;
632 // See if we're reading back the function value of a trap
633 if (name.charAt(0) == '_') {
634 if (name.charAt(1) == '_') name = name.substring(2);
635 else name = name.substring(1);
636 Trap t = Trap.getTrap(this, name);
637 return t == null ? null : t.f;
640 // See if we're triggering a trap
641 Trap t = traps == null || ignoretraps ? null : (Trap)traps.get(name);
642 if (t != null && t.isreadtrap) return t.perform(Trap.emptyargs);
644 // Check for a special handler
645 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
646 if (gph != null) return gph.get(this);
648 Object ret = super.get(name);
649 if (name.startsWith("$") && ret == null)
650 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" + name.substring(1) + "\" found");
654 public Object[] keys() {
655 Object[] ret = new Object[numChildren()];
656 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
662 * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack)
663 * @param rp if this put is being performed via a root proxy, rp is the root proxy.
665 public void put(Object name, Object value) { put(name, value, false, null); }
666 public void put(Object name, Object value, boolean ignoretraps) { put(name, value, ignoretraps, null); }
667 public void put(Object name_, Object value, boolean ignoretraps, RootProxy rp) {
668 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
669 if (!(name_ instanceof String)) { super.put(name_,value); return; }
670 String name = name_.toString();
671 if (!ignoretraps && traps != null) {
672 Trap t = (Trap)traps.get(name);
674 JS.Array arg = new JS.Array();
675 arg.addElement(value);
677 arg.setElementAt(null, 0);
682 // don't want to really cascade down to the box on this one
683 if (name.equals("0")) return;
685 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
686 if (gph != null) { gph.put(name, this, value); return; }
688 if (name.charAt(0) == '_') {
689 if (value != null && !(value instanceof JS.Callable)) {
690 if (Log.on) Log.logJS(this, "attempt to put a non function value (" + value + ") to " + name);
691 } else if (value != null && !(value instanceof JS.CompiledFunction)) {
692 if (Log.on) Log.logJS(this, "attempt to put a non-compiled function value (" + value + ") to " + name);
693 } else if (name.charAt(1) == '_') {
694 name = name.substring(2).intern();
695 Trap t = Trap.getTrap(this, name);
696 if (t != null) t.delete();
697 if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), true, rp);
699 name = name.substring(1).intern();
700 Trap t = Trap.getTrap(this, name);
701 if (t != null) t.delete();
702 if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), false, rp);
707 super.put(name, value);
711 // Tree Manipulation /////////////////////////////////////////////////////////////////////
713 /** The parent of this node */
714 private Box parent = null;
716 // Variables used in Vector mode */
717 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
718 private int indexInParent;
719 private Vec children = null;
721 // Variables used in linked-list mode
722 private int numKids = 0;
723 private Box nextSibling = null;
724 private Box prevSibling = null;
725 private Box firstKid = null;
727 // when we get more than 15 children, we switch to array-mode
728 private void convert_to_array() {
729 children = new Vec(numKids);
732 children.addElement(cur);
733 cur.indexInParent = children.size() - 1;
734 cur = cur.nextSibling;
735 } while (cur != firstKid);
738 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
739 public void remove() {
740 if (parent == null) {
741 if (surface != null) surface.dispose(true);
744 Box oldparent = parent;
745 if (oldparent == null) return;
746 MARK_FOR_REFLOW_this;
750 if (parent.children != null) {
751 parent.children.removeElementAt(indexInParent);
752 for(int j=indexInParent; j<parent.children.size(); j++)
753 (parent.getChild(j)).indexInParent = j;
756 if (parent.firstKid == this) {
757 if (nextSibling == this) parent.firstKid = null;
758 else parent.firstKid = nextSibling;
761 prevSibling.nextSibling = nextSibling;
762 nextSibling.prevSibling = prevSibling;
768 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
770 // note that JavaScript box[0] will invoke put(int i), not put(String s)
771 if (oldparent != null) oldparent.put("0", this);
774 /** returns our next sibling (parent[ourindex + 1]) */
775 public final Box nextSibling() {
776 if (parent == null) return null;
777 if (parent.children == null) {
778 if (nextSibling == parent.firstKid) return null;
781 if (indexInParent >= parent.children.size() - 1) return null;
782 return (Box)parent.children.elementAt(indexInParent + 1);
786 /** returns our next sibling (parent[ourindex + 1]) */
787 public final Box prevSibling() {
788 if (parent == null) return null;
789 if (parent.children == null) {
790 if (this == parent.firstKid) return null;
793 if (indexInParent == 0) return null;
794 return (Box)parent.children.elementAt(indexInParent - 1);
798 /** Returns the parent of this node */
799 public Box getParent() { return parent; }
801 /** Returns ith child */
802 public Box getChild(int i) {
803 if (children == null) {
804 if (firstKid == null) return null;
805 if (i >= numKids) return null;
806 if (i == numKids - 1) return firstKid.prevSibling;
808 for(int j=0; j<i; j++) cur = cur.nextSibling;
811 if (i >= children.size() || i < 0) return null;
812 return (Box)children.elementAt(i);
816 /** Returns the number of children */
817 public int numChildren() {
818 if (children == null) {
819 if (firstKid == null) return 0;
821 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
824 return children.size();
828 /** Returns our index in our parent */
829 public int getIndexInParent() {
830 if (parent == null) return 0;
831 if (parent.children == null) {
833 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
836 return indexInParent;
840 /** returns the root of the surface that this box belongs to */
841 public final Box getRoot() {
842 if (parent == null && surface != null) return this;
843 if (parent == null) return null;
844 return parent.getRoot();
848 // Root Proxy ///////////////////////////////////////////////////////////////////////////////
850 // FEATURE: use xwt.graft() here
851 RootProxy myproxy = null;
852 public JS getRootProxy() {
853 if (myproxy == null) myproxy = new RootProxy(this);
857 private static class RootProxy extends JS {
859 RootProxy(Box b) { this.box = b; }
860 public Object get(Object name) { return box.get(name); }
861 public void put(Object name, Object value) { box.put(name, value, false, this); }
862 public Object[] keys() { return box.keys(); }
863 public Object callMethod(Object method, JS.Array args, boolean justChecking) {
864 return ((Box)box).callMethod(method,args,justChecking);
869 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
871 static final int min(int a, int b) { if (a<b) return a; else return b; }
872 static final double min(double a, double b) { if (a<b) return a; else return b; }
873 static final int max(int a, int b) { if (a>b) return a; else return b; }
874 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; }
875 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; }
876 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
877 final boolean inside(int x, int y) { return (!invisible && x >= 0 && y >= 0 && x < width && y < height); }
879 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
880 public static Box whoIs(Box cur, int x, int y) {
882 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
886 // WARNING: this method is called from the event-queueing
887 // thread -- it may run concurrently with ANY part of XWT, and
888 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
891 if (cur.invisible) return null;
892 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
894 for(int i=cur.numChildren() - 1; i>=0; i--) {
895 Box child = cur.getChild(i);
896 if (child == null) continue; // since this method is unsynchronized, we have to double-check
899 if (!child.invisible && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
909 * A helper class for properties of Box which require special
912 * To avoid excessive use of String.equals(), the Box.get() and
913 * Box.put() methods employ a Hash keyed on property names that
914 * require special handling. The value stored in the Hash is an
915 * instance of an anonymous subclass of SpecialBoxProperty, which knows
916 * how to handle get()s and put()s for that property name. There
917 * should be one anonymous subclass of SpecialBoxProperty for each
918 * specially-handled property on Box.
920 static class SpecialBoxProperty {
922 SpecialBoxProperty() { }
924 /** stores instances of SpecialBoxProperty; keyed on property name */
925 static Hash specialBoxProperties = new Hash(200, 3);
927 /** this method defines the behavior when the property is get()ed from b */
928 Object get(Box b) { return null; }
930 /** this method defines the behavior when the property is put() to b */
931 void put(Box b, Object value) { }
933 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
934 void put(String name, Box b, Object value) { put(b, value); }
937 //#repeat fillcolor/strokecolor
938 specialBoxProperties.put("fillcolor", new SpecialBoxProperty() {
939 public Object get(Box b) {
940 if ((b.fillcolor & 0xFF000000) == 0) return null;
941 String red = Integer.toHexString((b.fillcolor & 0x00FF0000) >> 16);
942 String green = Integer.toHexString((b.fillcolor & 0x0000FF00) >> 8);
943 String blue = Integer.toHexString(b.fillcolor & 0x000000FF);
944 if (red.length() < 2) red = "0" + red;
945 if (blue.length() < 2) blue = "0" + blue;
946 if (green.length() < 2) green = "0" + green;
947 return "#" + red + green + blue;
949 public void put(Box b, Object value) {
950 int newcolor = b.fillcolor;
951 String s = value == null ? null : value.toString();
952 if (value == null) newcolor = 0x00000000;
953 else if (s.length() > 0 && s.charAt(0) == '#')
955 newcolor = 0xFF000000 |
956 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
957 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
958 Integer.parseInt(s.substring(5, 7), 16);
959 } catch (NumberFormatException e) {
960 Log.log(this, "invalid color " + s);
963 else if (SVG.colors.get(s) != null)
964 newcolor = 0xFF000000 | ((Integer)SVG.colors.get(s)).intValue();
965 // FIXME put named colors back in
966 if (newcolor == b.fillcolor) return;
967 b.fillcolor = newcolor;
973 specialBoxProperties.put("color", new SpecialBoxProperty() {
974 public Object get(Box b) { return b.get("fillcolor"); }
975 public void put(Box b, Object value) { b.put("fillcolor", value); }
977 specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
978 public Object get(Box b) { return b.get("strokecolor"); }
979 public void put(Box b, Object value) { b.put("strokecolor", value); }
982 specialBoxProperties.put("text", new SpecialBoxProperty() {
983 public Object get(Box b) { return b.text; }
984 public void put(Box b, Object value) {
985 String t = value == null ? "null" : value.toString();
986 if (t.equals(b.text)) return;
989 b.textwidth = b.textheight = 0;
992 ImageDecoder id = org.xwt.imp.Font.render(new FileInputStream("COMIC.TTF"), 24, b.text);
993 b.textwidth = id.getWidth();
994 b.textheight = id.getHeight();
995 } catch (Exception e) {
1002 specialBoxProperties.put("font", new SpecialBoxProperty() {
1003 public Object get(Box b) { return b.font; }
1004 public void put(Box b, Object value) {
1005 b.font = value == null ? null : value.toString();
1010 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1011 public Object get(Box b) { return b; }
1012 public void put(Box b, Object value) {
1013 if (value == null) b.remove();
1014 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1015 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1019 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1020 public Object get(Box b) {
1021 if (b.redirect == null) return "horizontal";
1022 else if (b.redirect != b) return get(b.redirect);
1023 else if (b.cols == 1) return "vertical";
1024 else if (b.rows == 1) return "horizontal";
1027 public void put(Box b, Object value) {
1028 if (value == null) return;
1029 if (b.redirect == null) return;
1030 if (b.redirect != b) { put(b.redirect, value); return; }
1031 if (value.equals("vertical")) {
1032 if (b.rows == 0) return;
1033 b.rows = 0; b.cols = 1;
1034 } else if (value.equals("horizontal")) {
1035 if (b.cols == 0) return;
1036 b.cols = 0; b.rows = 1;
1038 Log.log(this, "invalid value put to orient property: " + value);
1042 specialBoxProperties.put("static", new SpecialBoxProperty() {
1043 public Object get(Box b) {
1045 JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
1046 for(int i=0; i<cfsn.length() - 1; i++)
1047 if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
1048 cfsn = cfsn.substring(0, i);
1051 return Static.getStatic(cfsn);
1055 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1056 public Object get(Box b) { return (b.vshrink && b.hshrink) ? Boolean.TRUE : Boolean.FALSE; }
1057 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1060 //#repeat hshrink/vshrink
1061 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1062 public Object get(Box b) { return new Boolean(b.hshrink); }
1063 public void put(Box b, Object value) {
1064 boolean newshrink = stob(value);
1065 if (b.hshrink == newshrink) return;
1066 b.hshrink = newshrink;
1073 specialBoxProperties.put("x", new SpecialBoxProperty() {
1074 public Object get(Box b) {
1075 if (b.surface == null) return new Integer(0);
1076 if (b.invisible) return new Integer(0);
1077 return new Integer(b.x);
1079 public void put(Box b, Object value) {
1080 if (!b.absolute) return;
1081 int x = stoi(value);
1082 if (x == b.x) return;
1085 if (b.parent == null && b.surface != null) {
1086 // FIXME this gets hosed by the #repeat
1087 //b.surface.setLocation(b.x, b.y);
1088 b.surface.centerSurfaceOnRender = false;
1096 //#repeat width/height minwidth/minheight maxwidth/maxheight
1097 specialBoxProperties.put("width", new SpecialBoxProperty() {
1098 public Object get(Box b) { return new Integer(b.width); }
1099 public void put(Box b, Object value) {
1100 b.width = stoi(value);
1101 if (b.parent == null && b.surface != null) {
1102 b.surface.setSize();
1105 b.minwidth = b.maxwidth = b.width;
1111 //#repeat cols/rows rows/cols
1112 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1113 public Object get(Box b) { return new Double(b.cols); }
1114 public void put(Box b, Object value) {
1115 b.cols = stoi(value);
1116 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1117 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1122 //#repeat colspan/rowspan
1123 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1124 public Object get(Box b) { return new Double(b.colspan); }
1125 public void put(Box b, Object value) { b.colspan = stoi(value); MARK_FOR_REFLOW_b; }
1129 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1130 public Object get(Box b) { return b.tile ? Boolean.TRUE : Boolean.FALSE; }
1131 public void put(Box b, Object value) {
1132 boolean newtile = stob(value);
1133 if (newtile == b.tile) return;
1138 specialBoxProperties.put("invisible", new SpecialBoxProperty() {
1139 public Object get(Box b) {
1140 for (Box cur = b; cur != null; cur = cur.parent) { if (cur.invisible) return Boolean.TRUE; }
1141 return Boolean.FALSE;
1143 public void put(Box b, Object value) {
1144 boolean newinvisible = stob(value);
1145 if (newinvisible == b.invisible) return;
1146 b.invisible = newinvisible;
1147 if (b.parent == null) {
1148 if (b.surface != null) b.surface.setInvisible(newinvisible);
1151 MARK_FOR_REFLOW_b_parent;
1152 b.parent.dirty(b.x, b.y, b.width, b.height);
1156 specialBoxProperties.put("absolute", new SpecialBoxProperty() {
1157 public Object get(Box b) { return b.absolute ? Boolean.TRUE : Boolean.FALSE; }
1158 public void put(Box b, Object value) {
1159 boolean newabsolute = stob(value);
1160 if (newabsolute == b.absolute) return;
1161 b.absolute = newabsolute;
1162 if (b.absolute) { b.x = 0; b.y = 0; }
1163 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1166 specialBoxProperties.put("image", new SpecialBoxProperty() {
1167 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1168 public void put(Box b, Object value) {
1169 if ((value == null && b.image == null) ||
1170 (value != null && b.image != null && value.equals(ImageDecoder.imageToNameMap.get(b.image)))) return;
1171 String s = value == null ? null : value.toString();
1172 if (s == null || s.equals("")) b.image = null;
1174 if ((b.image = ImageDecoder.getPicture(s)) == null) {
1175 if (Log.on) Log.logJS(Box.class, "unable to load image " + s);
1177 b.minwidth = b.maxwidth = b.image.getWidth();
1178 b.minheight = b.maxheight = b.image.getHeight();
1186 //#repeat globalx/globaly x/y
1187 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1188 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1189 public void put(Box b, Object value) {
1190 if (b.surface == null || b.parent == null) return;
1191 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1197 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1198 public Object get(Box b) { return b.cursor; }
1199 public void put(Box b, Object value) {
1200 b.cursor = (String)value;
1201 if (b.surface == null) return;
1203 // see if we need to update the surface cursor
1204 Surface surface = b.getRoot().surface;
1205 String tempcursor = surface.cursor;
1206 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1207 if (surface.cursor != tempcursor) surface.syncCursor();
1211 //#repeat mousex/mousey x/y
1212 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1213 public Object get(Box b) {
1214 Surface surface = b.getRoot().surface;
1215 if (surface == null) return new Integer(0);
1216 int mousex = surface.mousex;
1217 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1218 return new Integer(mousex);
1223 specialBoxProperties.put("xwt", new SpecialBoxProperty() {
1224 public Object get(Box b) { return XWT.singleton; }
1227 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1228 public Object get(Box b) { return b.mouseinside ? Boolean.TRUE : Boolean.FALSE; }
1231 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1232 public Object get(Box b) {
1233 if (b.redirect == null) return new Integer(0);
1234 if (b.redirect != b) return get(b.redirect);
1235 return new Integer(b.numChildren());
1238 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1239 public void put(String name, Box b, Object value) {
1240 Surface surface = b.getRoot().surface;
1241 if (surface == null) return;
1242 int mousex = surface.mousex;
1243 int mousey = surface.mousey;
1244 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1248 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1249 if (c.inside(mousex - c.x, mousey - c.y)) {
1254 if (b.parent != null) b.parent.put(name, value);
1257 specialBoxProperties.put("Press1", mouseEventHandler);
1258 specialBoxProperties.put("Press2", mouseEventHandler);
1259 specialBoxProperties.put("Press3", mouseEventHandler);
1260 specialBoxProperties.put("Release1", mouseEventHandler);
1261 specialBoxProperties.put("Release2", mouseEventHandler);
1262 specialBoxProperties.put("Release3", mouseEventHandler);
1263 specialBoxProperties.put("Click1", mouseEventHandler);
1264 specialBoxProperties.put("Click2", mouseEventHandler);
1265 specialBoxProperties.put("Click3", mouseEventHandler);
1266 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1267 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1268 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1270 specialBoxProperties.put("root", new SpecialBoxProperty() {
1271 public Object get(Box b) {
1272 if (b.getRoot() == null) return null;
1273 else if (b.parent == null) return b;
1274 else return b.getRoot().getRootProxy();
1277 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1278 public Object get(Box b) {
1279 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1282 public void put(Box b, Object value) {
1283 if (b.surface == null) return;
1284 boolean val = stob(value);
1285 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1289 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1290 public Object get(Box b) {
1291 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1294 public void put(Box b, Object value) {
1295 if (b.surface == null) return;
1296 boolean val = stob(value);
1297 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1301 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1302 public void put(Box b, Object value) {
1303 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1307 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1308 public void put(Box b, Object value) {
1309 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1313 //#repeat hscar/vscar
1314 specialBoxProperties.put("hscar", new SpecialBoxProperty() {
1315 public void put(Box b, Object value) {
1316 if (b.parent == null && b.surface != null) {
1317 b.surface.hscar = stoi(value);
1318 b.surface.dirty(0, 0, b.width, b.height);
1319 b.surface.Refresh();
1325 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1326 public void put(Box b, Object value) {
1327 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1331 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1332 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty()); // FIXME should cascade
1333 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty()); // FIXME should cascade
1334 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1335 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1338 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
1339 public Object get(Box b) {
1340 if (b.redirect == null) return new Integer(0);
1341 if (b.redirect != b) return get(b.redirect);
1342 return new Integer(b.hpad);
1344 public void put(Box b, Object value) {
1345 if (b.redirect == null) return;
1346 if (b.redirect != b) { put(b.redirect, value); return; }
1347 int newval = stoi(value);
1348 if (newval == b.hpad) return;
1355 //#repeat minwidth/minheight maxwidth/maxheight
1356 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1357 public Object get(Box b) { return new Integer(b.minwidth); }
1358 public void put(Box b, Object value) {
1359 if (stoi(value) == b.minwidth) return;
1360 b.minwidth = stoi(value);
1364 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1365 public Object get(Box b) { return new Integer(b.maxwidth); }
1366 public void put(Box b, Object value) {
1367 if (stoi(value) == b.maxwidth) return;
1368 b.maxwidth = stoi(value);
1374 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1375 public void put(Box b, Object value) { }
1376 public Object get(Box b) {
1377 if (b.redirect == null) return null;
1378 if (b.redirect == b) return Boolean.TRUE;
1379 return get(b.redirect);
1384 // FIXME: need to be able to read this back
1385 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1386 public void put(Box b, Object value) { surface.setTitleBarText(value.toString()); }
1387 public Object get(Box b) { return b.ti; }
1390 // FIXME: need to be able to read this back
1391 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1392 public void put(Box b, Object value) {
1393 Picture pic = Box.getPicture(value.toString());
1394 if (pic != null) surface.setIcon(pic);
1395 else if (Log.on) Log.log(this, "unable to load icon " + value);
1397 public Object get(Box b) { return b.id; }
1403 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1404 public static boolean stob(Object o) {
1405 if (o == null) return false;
1406 return Boolean.TRUE.equals(o) || "true".equals(o);
1411 /** helper that converts a String to an int according to JavaScript coercion rules */
1412 public static int stoi(Object o) {
1413 if (o == null) return 0;
1414 if (o instanceof Integer) return ((Integer)o).intValue();
1417 if (!(o instanceof String)) s = o.toString();
1420 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1421 catch (NumberFormatException e) { return 0; }