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.*;
27 * Encapsulates the data for a single XWT box as well as all layout
32 * This is the real meat of XWT. Part of its monolithic design is for
33 * performance reasons: deep inheritance heirarchies are slow, and
34 * neither javago nor GCJ can inline across class boundaries.
37 * <p>The rendering process consists of three phases; each requires
38 * one DFS pass over the tree</p>
40 * <ol><li> <b>repacking</b>: children of a box are packed into columns
41 * and rows according to their colspan/rowspan attributes and
42 * ordering. Minimum and maximum sizes of columns are computed.
44 * <li> <b>resizing</b>: width/height and x/y positions of children
45 * are assigned. If a PosChange or SizeChange is triggered,
46 * <tt>Surface.abort</tt> will be set and the resizing process will
49 * <li> <b>repainting</b>: children draw their content onto the
53 * The first two passes together are called the <i>reflow</i> phase.
55 * Reflowing is done in a seperate pass since PosChanges and
56 * SizeChanges trigger an Surface.abort; if rendering were done in the same
57 * pass, rendering work done prior to the Surface.abort would be wasted.
59 * Repacking is seperate from resizing since a box's size depends on
60 * both the box's parent's size (so the traversal must be preorder)
61 * contentwidths of siblings both before and after the box, which in
62 * turn depend on all descendents of the siblings. FIXME
64 * A note on coordinates: the Box class represents regions
65 * internally as x,y,w,h tuples, even though the DoubleBuffer class
66 * uses x1,y1,x2,y2 tuples.
68 public final class Box extends JS.Scope {
70 public Box() { super(null); }
73 // Misc instance data ////////////////////////////////////////////////////////////////
75 private static int sizePosChangesSinceLastRender = 0;
78 // Misc instance data ////////////////////////////////////////////////////////////////
80 boolean needs_reflow = true;
81 //#define MARK_FOR_REFLOW_this for(Box b2 = this; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
82 //#define MARK_FOR_REFLOW_b for(Box b2 = b; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
83 //#define MARK_FOR_REFLOW_b_parent for(Box b2 = b.parent; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
85 private boolean mouseinside = false;
87 Surface surface = null; // null on all non-root boxen
91 // Geometry ////////////////////////////////////////////////////////////////////////////
93 // xwt can be compiled with 16-bit lengths to save memory on small devices
95 //#define MAX_LENGTH Integer.MAX_VALUE
96 //#define MIN_LENGTH Integer.MIN_VALUE
98 // always correct (set directly by user)
100 LENGTH minheight = 0;
101 LENGTH maxwidth = MAX_LENGTH;
102 LENGTH maxheight = MAX_LENGTH;
103 private LENGTH hpad = 0;
104 private LENGTH vpad = 0;
105 private String text = null;
106 private String font = null;
107 private LENGTH textwidth = 0;
108 private LENGTH textheight = 0;
111 private int rows = 1;
112 private int cols = 0;
113 private int rowspan = 1;
114 private int colspan = 1;
116 // computed during reflow
119 public LENGTH width = 0;
120 public LENGTH height = 0;
121 private int row = 0; // FIXME use a short
122 private int col = 0; // FIXME use a short
123 private LENGTH contentwidth = 0; // == max(minwidth, textwidth+pad, sum(child.contentwidth) + pad)
124 private LENGTH contentheight = 0;
127 // Rendering Properties ///////////////////////////////////////////////////////////
129 //private SVG.VP path = null;
130 //private SVG.Paint fill = null;
131 //private SVG.Paint stroke = null;
133 private Picture image; // will disappear
134 private int fillcolor = 0x00000000; // will become SVG.Paint
135 private int strokecolor = 0xFF000000; // will become SVG.Paint
137 private String cursor = null; // the cursor for this box
140 public boolean invisible = false; // true iff the Box is invisible
141 private boolean absolute = false; // If true, the box will be positioned absolutely
142 private boolean vshrink = false; // If true, the box will shrink to the smallest vertical size possible
143 private boolean hshrink = false; // If true, the box will shrink to the smallest horizontal size possible
144 private boolean tile = false; // FIXME: drop this?
147 // Instance Methods /////////////////////////////////////////////////////////////////////
150 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
151 public final void dirty() { dirty(0, 0, width, height); }
152 public final void dirty(int x, int y, int w, int h) {
153 for(Box cur = this; cur != null; cur = cur.parent) {
154 w = min(x + w, cur.width) - max(x, 0);
155 h = min(y + h, cur.height) - max(y, 0);
158 if (w <= 0 || h <= 0) return;
159 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
166 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
167 * to see if this node requires any Enter, Leave, or Move notifications.
169 * @param forceleave set to true by the box's parent if the mouse is inside an older
170 * sibling, which is covering up this box.
172 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
173 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
175 boolean wasinside = mouseinside;
176 boolean isinside = !invisible && inside(mousex, mousey) && !forceleave;
177 mouseinside = isinside;
179 if (!wasinside && !isinside) return;
181 if (traps == null) { }
182 else if (!wasinside && isinside && traps.get("Enter") != null) put("Enter", Boolean.TRUE);
183 else if (wasinside && !isinside && traps.get("Leave") != null) put("Leave", Boolean.TRUE);
184 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && traps.get("Move") != null) put("Move", Boolean.TRUE);
186 if (isinside && cursor != null) getRoot().cursor = cursor;
188 // if the mouse has moved into our padding region, it is considered 'outside' all our children
189 if (!(mousex >= hpad && mousey >= vpad && mousex < width - hpad && mousey < height + vpad)) forceleave = true;
191 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
192 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
193 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
198 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
202 if (Surface.abort) return;
203 resize(x, y, width, height);
206 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
208 if (!needs_reflow) return;
209 if (numChildren() == 0) {
210 contentwidth = max(textwidth + 2 * hpad, minwidth);
211 contentheight = max(textheight + 2 * vpad, minheight);
215 // --- Phase 0 ----------------------------------------------------------------------
217 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
219 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
222 // --- Phase 1 ----------------------------------------------------------------------
223 // assign children to their row/column positions (assuming constrained columns)
224 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
225 //#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
227 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
228 Box child = getChild(0);
229 for(; child != null && (child.absolute || child.invisible); child = child.nextSibling());
230 OUTER: for(int row=0; child != null; row++) {
231 for(int col=0; child != null && col < cols;) {
232 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
233 while(col < cols && numRowsInCol[col] > row) col++;
234 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
235 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
238 if (col + min(cols, child.colspan) > cols) break;
239 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
242 col += min(cols, child.colspan);
243 child = child.nextSibling();
244 for(; child != null && (child.absolute || child.invisible); child = child.nextSibling());
250 // --- Phase 2 ----------------------------------------------------------------------
251 // compute the min/max sizes of the columns and rows and set our contentwidth
252 //#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
253 contentwidth = 2 * hpad;
256 for(Box child = getChild(0); child != null; child = child.nextSibling())
257 numCols = max(numCols, child.col + child.colspan);
258 LENGTH[] colWidth = new LENGTH[numCols];
259 for(Box child = getChild(0); child != null; child = child.nextSibling())
260 if (!(child.absolute || child.invisible))
261 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
262 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
263 contentwidth = max(textwidth + 2 * hpad, contentwidth);
264 contentwidth = bound(minwidth, contentwidth, maxwidth);
269 void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
271 // --- Phase 1 ----------------------------------------------------------------------
272 // run PosChange/SizeChange, dirty as needed
273 if (x != this.x || y != this.y || width != this.width || height != this.height) {
274 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
275 boolean sizechange = false, poschange = false;
276 if (traps != null && (this.width != width || this.height != height) && traps.get("SizeChange") != null) sizechange = true;
277 if (traps != null && (this.x != x || this.y != y) && traps.get("PosChange") != null) poschange = true;
278 this.width = width; this.height = height; this.x = x; this.y = y;
280 if (sizechange || poschange)
281 if (sizePosChangesSinceLastRender == 500) {
282 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
284 sizePosChangesSinceLastRender++;
285 if (sizechange) put("SizeChange", Boolean.TRUE);
286 if (poschange) put("PosChange", Boolean.TRUE);
287 Surface.abort = true;
293 // --- short circuit ----------------------------------------------------------------
294 if (!needs_reflow) return;
295 needs_reflow = false;
296 if (numChildren() == 0) return;
298 // --- Phase 2 ----------------------------------------------------------------------
299 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
301 //#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
304 for(Box child = getChild(0); child != null; child = child.nextSibling())
305 numCols = max(numCols, child.col + child.colspan);
306 LENGTH[] colWidth = new LENGTH[numCols];
307 LENGTH[] colMaxWidth = new LENGTH[numCols];
308 int marginWidth = width;
309 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
312 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
313 if (child.absolute || child.invisible) continue;
314 //#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
315 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
316 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
317 colMaxWidth[i] = max(colMaxWidth[i], (child.hshrink ? child.contentwidth : child.maxwidth) / child.colspan);
321 //#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
322 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
324 for(int i=0; i<colMaxWidth.length; i++) {
325 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
326 marginWidth -= colMaxWidth[i];
327 if (marginWidth < 0) { marginWidth = 0; break; }
332 // --- Phase 3 ----------------------------------------------------------------------
333 // hand out the slack
335 //#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
337 for(int i=0; i<numCols; i++) slack -= colWidth[i];
338 if (numChildren() > 0)
340 // FEATURE: inefficient
341 int startslack = slack;
342 int increment = max(1, slack / numCols);
343 for(int col=0; col < numCols && slack > 0; col++) {
344 slack += colWidth[col];
345 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
346 slack -= colWidth[col];
348 if (slack == startslack) break;
353 // --- Phase 4 ----------------------------------------------------------------------
354 // assign children's new sizes and positions and recurse
355 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
356 if (child.invisible) continue;
357 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
358 if (child.absolute) {
361 child_width = child.hshrink ? child.contentwidth : min(child.maxwidth, width - child.x - hpad);
362 child_height = child.vshrink ? child.contentheight : min(child.maxheight, height - child.y - vpad);
365 //#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
366 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
367 diff = bound(child.contentwidth, child_width, child.hshrink ? child.contentwidth : child.maxwidth) - child_width;
368 child_x = max(hpad, marginWidth / 2); for(int i=0; i<child.col; i++) child_x += colWidth[i];
369 if (diff < 0) child_x += -1 * (diff / 2);
373 child.resize(child_x, child_y, child_width, child_height);
380 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
382 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
383 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
384 if (Surface.abort || invisible) return;
385 int globalx = parentx + (parent == null ? 0 : x);
386 int globaly = parenty + (parent == null ? 0 : y);
388 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
389 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw, (parent == null ? 0 : globalx) + width) - globalx;
390 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph, (parent == null ? 0 : globaly) + height) - globaly;
391 clipx = max(clipx, parent == null ? 0 : globalx);
392 clipy = max(clipy, parent == null ? 0 : globaly);
393 if (clipw <= 0 || cliph <= 0) return;
395 if ((fillcolor & 0xFF000000) != 0x00000000 || parent == null)
396 buf.fillRect(clipx, clipy, clipx + clipw, clipy + cliph, (fillcolor & 0xFF000000) != 0 ? fillcolor : 0xFF777777);
399 if (tile) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
400 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
402 if (text != null && !text.equals("")) renderText(x, y, clipx, clipy, clipw, cliph, buf);
404 // now subtract the pad region from the clip region before proceeding
405 clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
406 cliph = min(max(clipy, globaly + vpad) + cliph, globaly + height - vpad) - clipy;
407 clipx = max(clipx, globalx + hpad);
408 clipy = max(clipy, globaly + vpad);
410 for(Box b = getChild(0); b != null; b = b.nextSibling())
411 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf);
414 void renderStretchedImage(int globalx, int globaly, int x, int y, int w, int h, DoubleBuffer buf) {
415 buf.setClip(x, y, w + x, h + y);
419 int hstretch = width / image.getWidth();
420 if (hstretch == 0) hstretch = -1 * image.getWidth() / width;
421 int vstretch = height / image.getHeight();
422 if (vstretch == 0) vstretch = -1 * image.getHeight() / height;
423 if (hstretch < vstretch) height = image.getHeight() * width / image.getWidth();
424 else width = image.getWidth() * height / image.getHeight();
428 buf.drawPicture(image, globalx, globaly, globalx + width, globaly + height, 0, 0, image.getWidth(), image.getHeight());
429 buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
432 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, DoubleBuffer buf) {
433 int iw = image.getWidth();
434 int ih = image.getHeight();
436 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
437 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
439 int dx1 = max(i * iw + x, x);
440 int dy1 = max(j * ih + y, y);
441 int dx2 = min((i+1) * iw + x, x + w);
442 int dy2 = min((j+1) * ih + y, y + h);
444 int sx1 = dx1 - (i*iw) - x;
445 int sy1 = dy1 - (j*ih) - y;
446 int sx2 = dx2 - (i*iw) - x;
447 int sy2 = dy2 - (j*ih) - y;
449 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
450 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
455 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
456 //buf.setClip(clipx, clipy, clipw, cliph);
459 ImageDecoder id = org.xwt.imp.Font.render(new FileInputStream("COMIC.TTF"), 24, text, false);
460 Picture p = Platform.createPicture(id);
462 // FIXME: underline (dotted?)
463 buf.drawPicture(p, x + hpad, y + vpad);
464 buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
465 } catch (Exception e) {
469 buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
473 // Methods to implement org.xwt.js.JS //////////////////////////////////////
475 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
476 if ("indexof".equals(method)) {
477 if (checkOnly) return Boolean.TRUE;
478 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
479 Box b = (Box)args.elementAt(0);
480 if (b.parent != Box.this) {
481 if (redirect == null || redirect == Box.this) return new Integer(-1);
482 return redirect.callMethod(method, args, checkOnly);
484 return new Integer(b.getIndexInParent());
486 } else if ("apply".equals(method)) {
487 if (checkOnly) return Boolean.TRUE;
488 if (args.elementAt(0) instanceof String) {
489 String templatename = (String)args.elementAt(0);
490 Template t = Template.getTemplate(templatename, null);
492 if (Log.on) Log.logJS(this, "template " + templatename + " not found");
494 if (ThreadMessage.suspendThread()) try {
495 JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
496 t.apply(this, null, null, callback, 0, t.numUnits());
498 ThreadMessage.resumeThread();
501 } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
502 JS s = (JS)args.elementAt(0);
503 Object[] keys = s.keys();
504 for(int j=0; j<keys.length; j++) put(keys[j].toString(), s.get(keys[j]));
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 } else if (redirect != this) {
532 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
533 redirect.put(i, value);
535 } else if (value == null) {
536 if (i >= 0 && i < numChildren()) {
541 } else if (value instanceof RootProxy) {
542 if (Log.on) Log.logJS(this, "attempt to reparent a box via its proxy object");
544 Box newnode = (Box)value;
546 // check if box being moved is currently target of a redirect
547 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
548 if (cur.redirect == newnode) {
549 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
553 // check for recursive ancestor violation
554 for(Box cur = this; cur != null; cur = cur.parent)
555 if (cur == newnode) {
556 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
557 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
561 if (numKids > 15 && children == null) convert_to_array();
563 newnode.parent = this;
565 if (children == null) {
566 if (firstKid == null) {
568 newnode.prevSibling = newnode;
569 newnode.nextSibling = newnode;
570 } else if (i >= numKids) {
571 newnode.prevSibling = firstKid.prevSibling;
572 newnode.nextSibling = firstKid;
573 firstKid.prevSibling.nextSibling = newnode;
574 firstKid.prevSibling = newnode;
577 for(int j=0; j<i; j++) cur = cur.nextSibling;
578 newnode.prevSibling = cur.prevSibling;
579 newnode.nextSibling = cur;
580 cur.prevSibling.nextSibling = newnode;
581 cur.prevSibling = newnode;
582 if (i == 0) firstKid = newnode;
587 if (i >= children.size()) {
588 newnode.indexInParent = children.size();
589 children.addElement(newnode);
591 children.insertElementAt(newnode, i);
592 for(int j=i; j<children.size(); j++)
593 getChild(j).indexInParent = j;
597 // need both of these in case child was already uncalc'ed
600 MARK_FOR_REFLOW_this;
604 // note that JavaScript box[0] will invoke put(int i), not put(String s)
609 public Object get(Object name) { return get(name, false); }
610 public Object get(Object name_, boolean ignoretraps) {
611 if (name_ instanceof Number) return get(((Number)name_).intValue());
613 if (!(name_ instanceof String)) return null;
614 String name = (String)name_;
615 if (name.equals("")) return null;
617 // See if we're reading back the function value of a trap
618 if (name.charAt(0) == '_') {
619 if (name.charAt(1) == '_') name = name.substring(2);
620 else name = name.substring(1);
621 Trap t = Trap.getTrap(this, name);
622 return t == null ? null : t.f;
625 // See if we're triggering a trap
626 Trap t = traps == null || ignoretraps ? null : (Trap)traps.get(name);
627 if (t != null && t.isreadtrap) return t.perform(Trap.emptyargs);
629 // Check for a special handler
630 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
631 if (gph != null) return gph.get(this);
633 Object ret = super.get(name);
634 if (name.startsWith("$") && ret == null)
635 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" + name.substring(1) + "\" found");
639 public Object[] keys() {
640 Object[] ret = new Object[numChildren()];
641 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
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) { put(name, value, false, null); }
651 public void put(Object name, Object value, boolean ignoretraps) { put(name, value, ignoretraps, null); }
652 public void put(Object name_, Object value, boolean ignoretraps, RootProxy rp) {
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();
656 if (!ignoretraps && traps != null) {
657 Trap t = (Trap)traps.get(name);
659 JS.Array arg = new JS.Array();
660 arg.addElement(value);
662 arg.setElementAt(null, 0);
667 // don't want to really cascade down to the box on this one
668 if (name.equals("0")) return;
670 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
671 if (gph != null) { gph.put(name, this, value); return; }
673 if (name.charAt(0) == '_') {
674 if (value != null && !(value instanceof JS.Callable)) {
675 if (Log.on) Log.logJS(this, "attempt to put a non function value (" + value + ") to " + name);
676 } else if (value != null && !(value instanceof JS.CompiledFunction)) {
677 if (Log.on) Log.logJS(this, "attempt to put a non-compiled function value (" + value + ") to " + name);
678 } else if (name.charAt(1) == '_') {
679 name = name.substring(2).intern();
680 Trap t = Trap.getTrap(this, name);
681 if (t != null) t.delete();
682 if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), true, rp);
684 name = name.substring(1).intern();
685 Trap t = Trap.getTrap(this, name);
686 if (t != null) t.delete();
687 if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), false, rp);
692 super.put(name, value);
696 // Tree Manipulation /////////////////////////////////////////////////////////////////////
698 /** The parent of this node */
699 private Box parent = null;
701 // Variables used in Vector mode */
702 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
703 private int indexInParent;
704 private Vec children = null;
706 // Variables used in linked-list mode
707 private int numKids = 0;
708 private Box nextSibling = null;
709 private Box prevSibling = null;
710 private Box firstKid = null;
712 // when we get more than 15 children, we switch to array-mode
713 private void convert_to_array() {
714 children = new Vec(numKids);
717 children.addElement(cur);
718 cur.indexInParent = children.size() - 1;
719 cur = cur.nextSibling;
720 } while (cur != firstKid);
723 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
724 public void remove() {
725 if (parent == null) {
726 if (surface != null) surface.dispose(true);
729 Box oldparent = parent;
730 if (oldparent == null) return;
731 MARK_FOR_REFLOW_this;
735 if (parent.children != null) {
736 parent.children.removeElementAt(indexInParent);
737 for(int j=indexInParent; j<parent.children.size(); j++)
738 (parent.getChild(j)).indexInParent = j;
741 if (parent.firstKid == this) {
742 if (nextSibling == this) parent.firstKid = null;
743 else parent.firstKid = nextSibling;
746 prevSibling.nextSibling = nextSibling;
747 nextSibling.prevSibling = prevSibling;
753 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
755 // note that JavaScript box[0] will invoke put(int i), not put(String s)
756 if (oldparent != null) oldparent.put("0", this);
759 /** returns our next sibling (parent[ourindex + 1]) */
760 public final Box nextSibling() {
761 if (parent == null) return null;
762 if (parent.children == null) {
763 if (nextSibling == parent.firstKid) return null;
766 if (indexInParent >= parent.children.size() - 1) return null;
767 return (Box)parent.children.elementAt(indexInParent + 1);
771 /** returns our next sibling (parent[ourindex + 1]) */
772 public final Box prevSibling() {
773 if (parent == null) return null;
774 if (parent.children == null) {
775 if (this == parent.firstKid) return null;
778 if (indexInParent == 0) return null;
779 return (Box)parent.children.elementAt(indexInParent - 1);
783 /** Returns the parent of this node */
784 public Box getParent() { return parent; }
786 /** Returns ith child */
787 public Box getChild(int i) {
788 if (children == null) {
789 if (firstKid == null) return null;
790 if (i >= numKids) return null;
791 if (i == numKids - 1) return firstKid.prevSibling;
793 for(int j=0; j<i; j++) cur = cur.nextSibling;
796 if (i >= children.size() || i < 0) return null;
797 return (Box)children.elementAt(i);
801 /** Returns the number of children */
802 public int numChildren() {
803 if (children == null) {
804 if (firstKid == null) return 0;
806 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
809 return children.size();
813 /** Returns our index in our parent */
814 public int getIndexInParent() {
815 if (parent == null) return 0;
816 if (parent.children == null) {
818 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
821 return indexInParent;
825 /** returns the root of the surface that this box belongs to */
826 public final Box getRoot() {
827 if (parent == null && surface != null) return this;
828 if (parent == null) return null;
829 return parent.getRoot();
833 // Root Proxy ///////////////////////////////////////////////////////////////////////////////
835 // FEATURE: use xwt.graft() here
836 RootProxy myproxy = null;
837 public JS getRootProxy() {
838 if (myproxy == null) myproxy = new RootProxy(this);
842 private static class RootProxy extends JS {
844 RootProxy(Box b) { this.box = b; }
845 public Object get(Object name) { return box.get(name); }
846 public void put(Object name, Object value) { box.put(name, value, false, this); }
847 public Object[] keys() { return box.keys(); }
848 public Object callMethod(Object method, JS.Array args, boolean justChecking) {
849 return ((Box)box).callMethod(method,args,justChecking);
854 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
856 static final int min(int a, int b) { if (a<b) return a; else return b; }
857 static final double min(double a, double b) { if (a<b) return a; else return b; }
858 static final int max(int a, int b) { if (a>b) return a; else return b; }
859 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; }
860 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; }
861 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
862 final boolean inside(int x, int y) { return (!invisible && x >= 0 && y >= 0 && x < width && y < height); }
864 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
865 public static Box whoIs(Box cur, int x, int y) {
867 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
871 // WARNING: this method is called from the event-queueing
872 // thread -- it may run concurrently with ANY part of XWT, and
873 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
876 if (cur.invisible) return null;
877 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
879 for(int i=cur.numChildren() - 1; i>=0; i--) {
880 Box child = cur.getChild(i);
881 if (child == null) continue; // since this method is unsynchronized, we have to double-check
884 if (!child.invisible && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
894 * A helper class for properties of Box which require special
897 * To avoid excessive use of String.equals(), the Box.get() and
898 * Box.put() methods employ a Hash keyed on property names that
899 * require special handling. The value stored in the Hash is an
900 * instance of an anonymous subclass of SpecialBoxProperty, which knows
901 * how to handle get()s and put()s for that property name. There
902 * should be one anonymous subclass of SpecialBoxProperty for each
903 * specially-handled property on Box.
905 static class SpecialBoxProperty {
907 SpecialBoxProperty() { }
909 /** stores instances of SpecialBoxProperty; keyed on property name */
910 static Hash specialBoxProperties = new Hash(200, 3);
912 /** this method defines the behavior when the property is get()ed from b */
913 Object get(Box b) { return null; }
915 /** this method defines the behavior when the property is put() to b */
916 void put(Box b, Object value) { }
918 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
919 void put(String name, Box b, Object value) { put(b, value); }
922 //#repeat fillcolor/strokecolor
923 specialBoxProperties.put("fillcolor", new SpecialBoxProperty() {
924 public Object get(Box b) {
925 if ((b.fillcolor & 0xFF000000) == 0) return null;
926 String red = Integer.toHexString((b.fillcolor & 0x00FF0000) >> 16);
927 String green = Integer.toHexString((b.fillcolor & 0x0000FF00) >> 8);
928 String blue = Integer.toHexString(b.fillcolor & 0x000000FF);
929 if (red.length() < 2) red = "0" + red;
930 if (blue.length() < 2) blue = "0" + blue;
931 if (green.length() < 2) green = "0" + green;
932 return "#" + red + green + blue;
934 public void put(Box b, Object value) {
935 int newcolor = b.fillcolor;
936 String s = value == null ? null : value.toString();
937 if (value == null) newcolor = 0x00000000;
938 else if (s.length() > 0 && s.charAt(0) == '#')
940 newcolor = 0xFF000000 |
941 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
942 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
943 Integer.parseInt(s.substring(5, 7), 16);
944 } catch (NumberFormatException e) {
945 Log.log(this, "invalid color " + s);
948 else if (SVG.colors.get(s) != null)
949 newcolor = 0xFF000000 | ((Integer)SVG.colors.get(s)).intValue();
950 if (newcolor == b.fillcolor) return;
951 b.fillcolor = newcolor;
957 specialBoxProperties.put("color", new SpecialBoxProperty() {
958 public Object get(Box b) { return b.get("fillcolor"); }
959 public void put(Box b, Object value) { b.put("fillcolor", value); }
962 specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
963 public Object get(Box b) { return b.get("strokecolor"); }
964 public void put(Box b, Object value) { b.put("strokecolor", value); }
967 specialBoxProperties.put("text", new SpecialBoxProperty() {
968 public Object get(Box b) { return b.text; }
969 public void put(Box b, Object value) {
970 String t = value == null ? "null" : value.toString();
971 if (t.equals(b.text)) return;
974 if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
975 b.textwidth = b.textheight = 0;
978 ImageDecoder id = org.xwt.imp.Font.render(new FileInputStream("COMIC.TTF"), 24, b.text, true);
979 if (id.getWidth() != b.textwidth || id.getHeight() != b.textheight) MARK_FOR_REFLOW_b;
980 b.textwidth = id.getWidth();
981 b.textheight = id.getHeight();
982 } catch (Exception e) {
989 specialBoxProperties.put("font", new SpecialBoxProperty() {
990 public Object get(Box b) { return b.font; }
991 public void put(Box b, Object value) {
992 b.font = value == null ? null : value.toString();
993 // FIXME: need a resource stream to hand off to MIPS
994 // FIXME: MARK_FOR_REFLOW here
998 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
999 public Object get(Box b) { return b; }
1000 public void put(Box b, Object value) {
1001 if (value == null) b.remove();
1002 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1003 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1007 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1008 public Object get(Box b) {
1009 Log.log(this, "warning: the orient property is deprecated");
1010 if (b.redirect == null) return "horizontal";
1011 else if (b.redirect != b) return get(b.redirect);
1012 else if (b.cols == 1) return "vertical";
1013 else if (b.rows == 1) return "horizontal";
1016 public void put(Box b, Object value) {
1017 Log.log(this, "warning: the orient property is deprecated");
1018 if (value == null) return;
1019 if (b.redirect == null) return;
1020 if (b.redirect != b) { put(b.redirect, value); return; }
1021 if (value.equals("vertical")) {
1022 if (b.rows == 0) return;
1023 b.rows = 0; b.cols = 1;
1024 } else if (value.equals("horizontal")) {
1025 if (b.cols == 0) return;
1026 b.cols = 0; b.rows = 1;
1028 Log.log(this, "invalid value put to orient property: " + value);
1032 specialBoxProperties.put("static", new SpecialBoxProperty() {
1033 public Object get(Box b) {
1035 JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
1036 for(int i=0; i<cfsn.length() - 1; i++)
1037 if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
1038 cfsn = cfsn.substring(0, i);
1041 return Static.getStatic(cfsn);
1045 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1046 public Object get(Box b) { return (b.vshrink && b.hshrink) ? Boolean.TRUE : Boolean.FALSE; }
1047 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1050 //#repeat hshrink/vshrink
1051 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1052 public Object get(Box b) { return new Boolean(b.hshrink); }
1053 public void put(Box b, Object value) {
1054 boolean newshrink = stob(value);
1055 if (b.hshrink == newshrink) return;
1056 b.hshrink = newshrink;
1063 specialBoxProperties.put("x", new SpecialBoxProperty() {
1064 public Object get(Box b) {
1065 if (b.surface == null) return new Integer(0);
1066 if (b.invisible) return new Integer(0);
1067 return new Integer(b.x);
1069 public void put(Box b, Object value) {
1070 if (!b.absolute) return;
1071 int x = stoi(value);
1072 if (x == b.x) return;
1075 if (b.parent == null && b.surface != null) {
1076 b.surface.setLocation();
1077 b.surface.centerSurfaceOnRender = false;
1085 //#repeat width/height minwidth/minheight maxwidth/maxheight
1086 specialBoxProperties.put("width", new SpecialBoxProperty() {
1087 public Object get(Box b) { return new Integer(b.width); }
1088 public void put(Box b, Object value) {
1089 b.width = stoi(value);
1090 if (b.parent == null && b.surface != null) {
1091 b.surface.setSize();
1094 if (b.minwidth == b.width && b.maxwidth == b.width) return;
1095 b.minwidth = b.maxwidth = b.width;
1101 //#repeat cols/rows rows/cols
1102 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1103 public Object get(Box b) { return new Double(b.cols); }
1104 public void put(Box b, Object value) {
1105 if (b.cols == stoi(value)) return;
1106 b.cols = stoi(value);
1107 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1108 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1113 //#repeat colspan/rowspan
1114 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1115 public Object get(Box b) { return new Double(b.colspan); }
1116 public void put(Box b, Object value) {
1117 if (b.colspan == stoi(value)) return;
1118 b.colspan = stoi(value);
1124 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1125 public Object get(Box b) { return b.tile ? Boolean.TRUE : Boolean.FALSE; }
1126 public void put(Box b, Object value) {
1127 if (b.tile == stob(value)) return;
1128 b.tile = stob(value);
1132 specialBoxProperties.put("invisible", new SpecialBoxProperty() {
1133 public Object get(Box b) {
1134 for (Box cur = b; cur != null; cur = cur.parent) { if (cur.invisible) return Boolean.TRUE; }
1135 return Boolean.FALSE;
1137 public void put(Box b, Object value) {
1138 if (stob(value) == b.invisible) return;
1139 b.invisible = stob(value);
1140 if (b.parent == null) {
1141 if (b.surface != null) b.surface.setInvisible(b.invisible);
1144 MARK_FOR_REFLOW_b_parent;
1145 b.parent.dirty(b.x, b.y, b.width, b.height);
1149 specialBoxProperties.put("absolute", new SpecialBoxProperty() {
1150 public Object get(Box b) { return b.absolute ? Boolean.TRUE : Boolean.FALSE; }
1151 public void put(Box b, Object value) {
1152 if (stob(value) == b.absolute) return;
1153 b.absolute = stob(value);
1154 if (b.absolute) { b.x = 0; b.y = 0; }
1155 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1158 specialBoxProperties.put("image", new SpecialBoxProperty() {
1159 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1160 public void put(Box b, Object value) {
1161 if ((value == null && b.image == null) ||
1162 (value != null && b.image != null && value.equals(ImageDecoder.imageToNameMap.get(b.image)))) return;
1163 String s = value == null ? null : value.toString();
1164 if (s == null || s.equals("")) b.image = null;
1166 if ((b.image = ImageDecoder.getPicture(s)) == null) {
1167 if (Log.on) Log.logJS(Box.class, "unable to load image " + s);
1169 b.minwidth = b.maxwidth = b.image.getWidth();
1170 b.minheight = b.maxheight = b.image.getHeight();
1178 //#repeat globalx/globaly x/y
1179 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1180 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1181 public void put(Box b, Object value) {
1182 if (b.surface == null || b.parent == null) return;
1183 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1189 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1190 public Object get(Box b) { return b.cursor; }
1191 public void put(Box b, Object value) {
1192 b.cursor = (String)value;
1193 if (b.surface == null) return;
1195 // see if we need to update the surface cursor
1196 Surface surface = b.getRoot().surface;
1197 String tempcursor = surface.cursor;
1198 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1199 if (surface.cursor != tempcursor) surface.syncCursor();
1203 //#repeat mousex/mousey x/y
1204 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1205 public Object get(Box b) {
1206 Surface surface = b.getRoot().surface;
1207 if (surface == null) return new Integer(0);
1208 int mousex = surface.mousex;
1209 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1210 return new Integer(mousex);
1215 specialBoxProperties.put("xwt", new SpecialBoxProperty() {
1216 public Object get(Box b) { return XWT.singleton; }
1219 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1220 public Object get(Box b) { return b.mouseinside ? Boolean.TRUE : Boolean.FALSE; }
1223 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1224 public Object get(Box b) {
1225 if (b.redirect == null) return new Integer(0);
1226 if (b.redirect != b) return get(b.redirect);
1227 return new Integer(b.numChildren());
1230 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1231 public void put(String name, Box b, Object value) {
1232 Surface surface = b.getRoot().surface;
1233 if (surface == null) return;
1234 int mousex = surface.mousex;
1235 int mousey = surface.mousey;
1236 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1240 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1241 if (c.inside(mousex - c.x, mousey - c.y)) {
1246 if (b.parent != null) b.parent.put(name, value);
1249 specialBoxProperties.put("Press1", mouseEventHandler);
1250 specialBoxProperties.put("Press2", mouseEventHandler);
1251 specialBoxProperties.put("Press3", mouseEventHandler);
1252 specialBoxProperties.put("Release1", mouseEventHandler);
1253 specialBoxProperties.put("Release2", mouseEventHandler);
1254 specialBoxProperties.put("Release3", mouseEventHandler);
1255 specialBoxProperties.put("Click1", mouseEventHandler);
1256 specialBoxProperties.put("Click2", mouseEventHandler);
1257 specialBoxProperties.put("Click3", mouseEventHandler);
1258 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1259 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1260 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1262 specialBoxProperties.put("root", new SpecialBoxProperty() {
1263 public Object get(Box b) {
1264 if (b.getRoot() == null) return null;
1265 else if (b.parent == null) return b;
1266 else return b.getRoot().getRootProxy();
1269 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1270 public Object get(Box b) {
1271 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1274 public void put(Box b, Object value) {
1275 if (b.surface == null) return;
1276 boolean val = stob(value);
1277 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1281 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1282 public Object get(Box b) {
1283 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1286 public void put(Box b, Object value) {
1287 if (b.surface == null) return;
1288 boolean val = stob(value);
1289 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1293 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1294 public void put(Box b, Object value) {
1295 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1299 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1300 public void put(Box b, Object value) {
1301 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1305 //#repeat hscar/vscar
1306 specialBoxProperties.put("hscar", new SpecialBoxProperty() {
1307 public void put(Box b, Object value) {
1308 if (b.parent == null && b.surface != null) {
1309 b.surface.hscar = stoi(value);
1310 b.surface.dirty(0, 0, b.width, b.height);
1311 b.surface.Refresh();
1317 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1318 public void put(Box b, Object value) {
1319 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1323 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1324 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty()); // FIXME should cascade
1325 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty()); // FIXME should cascade
1326 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1327 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1330 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
1331 public Object get(Box b) {
1332 if (b.redirect == null) return new Integer(0);
1333 if (b.redirect != b) return get(b.redirect);
1334 return new Integer(b.hpad);
1336 public void put(Box b, Object value) {
1337 if (b.redirect == null) return;
1338 if (b.redirect != b) { put(b.redirect, value); return; }
1339 int newval = stoi(value);
1340 if (newval == b.hpad) return;
1347 //#repeat minwidth/minheight maxwidth/maxheight
1348 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1349 public Object get(Box b) { return new Integer(b.minwidth); }
1350 public void put(Box b, Object value) {
1351 if (stoi(value) == b.minwidth) return;
1352 b.minwidth = stoi(value);
1356 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1357 public Object get(Box b) { return new Integer(b.maxwidth); }
1358 public void put(Box b, Object value) {
1359 if (stoi(value) == b.maxwidth) return;
1360 b.maxwidth = stoi(value);
1366 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1367 public void put(Box b, Object value) { }
1368 public Object get(Box b) {
1369 if (b.redirect == null) return null;
1370 if (b.redirect == b) return Boolean.TRUE;
1371 return get(b.redirect);
1376 // FIXME: need to be able to read this back
1377 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1378 public void put(Box b, Object value) { surface.setTitleBarText(value.toString()); }
1379 public Object get(Box b) { return b.ti; }
1382 // FIXME: need to be able to read this back
1383 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1384 public void put(Box b, Object value) {
1385 Picture pic = Box.getPicture(value.toString());
1386 if (pic != null) surface.setIcon(pic);
1387 else if (Log.on) Log.log(this, "unable to load icon " + value);
1389 public Object get(Box b) { return b.id; }
1395 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1396 public static boolean stob(Object o) {
1397 if (o == null) return false;
1398 return Boolean.TRUE.equals(o) || "true".equals(o);
1401 /** helper that converts a String to an int according to JavaScript coercion rules */
1402 public static int stoi(Object o) {
1403 if (o == null) return 0;
1404 if (o instanceof Integer) return ((Integer)o).intValue();
1407 if (!(o instanceof String)) s = o.toString();
1410 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1411 catch (NumberFormatException e) { return 0; }