1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
4 // **** This file must be preprocessed before compilation ****
6 // RULE: coordinates on non-static methods are ALWAYS relative to the
7 // upper-left hand corner of <tt>this</tt>
9 // FIXME: font color, italicization, bolding, and underlining
11 // FIXME: fixed aspect
12 // FEATURE: reflow before allowing js to read from width/height
13 // FEATURE: fastpath for rows=1/cols=1
14 // FEATURE: reflow starting with a certain child
15 // FEATURE: separate mark_for_reflow and mark_for_resize
21 import org.xwt.util.*;
22 import org.xwt.translators.*;
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 * The two passes are repack and resize. Together they are known as
59 * reflow. Repacking assigns boxes to the appropriate grid
60 * coordinates within the parents and computes
61 * contentwidth/contentheight. Resize computes actual size and
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;
86 Surface surface = null; // null on all non-root boxen
88 // FEATURE: combine this with the JSObject Hash
92 // Flags ///////////////////////////////////////////////////////////////////////////////
93 static int MOUSEINSIDE_FLAG = 0x00000001;
94 static int INVISIBLE_FLAG = 0x00000002;
95 static int ABSOLUTE_FLAG = 0x00000004;
96 static int HSHRINK_FLAG = 0x00000010;
97 static int VSHRINK_FLAG = 0x00000020;
98 static int TILE_FLAG = 0x00000040;
101 * Set when the font changes, cleared during repack. If set
102 * during repack, all font==null children are marked for reflow
103 * and given the font_changed_flag. We use this flag to avoid
104 * having to iterate over all descendents of a box when its font
107 static int FONT_CHANGED_FLAG = 0x00000100;
109 static int ALIGN_FLAG = 0x00000000;
110 static int FIXEDASPECT_FLAG = 0x00000000;
114 // Geometry ////////////////////////////////////////////////////////////////////////////
116 // xwt can be compiled with 16-bit lengths to save memory on small devices
118 //#define MAX_LENGTH Integer.MAX_VALUE
119 //#define MIN_LENGTH Integer.MIN_VALUE
121 // always correct (set directly by user)
123 LENGTH minheight = 0;
124 LENGTH maxwidth = MAX_LENGTH;
125 LENGTH maxheight = MAX_LENGTH;
126 private LENGTH hpad = 0;
127 private LENGTH vpad = 0;
128 private String text = null;
129 private String font = null;
130 private LENGTH textwidth = 0;
131 private LENGTH textheight = 0;
133 // FEATURE: use shorts
134 private int rows = 1;
135 private int cols = 0;
136 private int rowspan = 1;
137 private int colspan = 1;
139 // computed during reflow
142 public LENGTH width = 0;
143 public LENGTH height = 0;
144 private int row = 0; // FEATURE use a short
145 private int col = 0; // FEATURE use a short
146 private LENGTH contentwidth = 0; // == max(minwidth, textwidth+pad, sum(child.contentwidth) + pad)
147 private LENGTH contentheight = 0;
150 // Rendering Properties ///////////////////////////////////////////////////////////
152 //private SVG.VP path = null;
153 //private SVG.Paint fill = null;
154 //private SVG.Paint stroke = null;
156 private Picture image; // will disappear
157 private int fillcolor = 0x00000000; // will become SVG.Paint
158 private int strokecolor = 0xFF000000; // will become SVG.Paint
160 private String cursor = null; // the cursor for this box
163 // Instance Methods /////////////////////////////////////////////////////////////////////
165 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
166 public final void dirty() { dirty(0, 0, width, height); }
167 public final void dirty(int x, int y, int w, int h) {
169 for(Box cur = this; cur != null; cur = cur.parent) {
170 w = min(x + w, cur.width) - max(x, 0);
171 h = min(y + h, cur.height) - max(y, 0);
174 if (w <= 0 || h <= 0) return;
175 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
181 for(cur = this; cur.parent != null; cur = cur.parent);
182 if (cur.surface != null) cur.surface.dirty(0, 0, cur.width, cur.height);
186 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
187 * to see if this node requires any Enter, Leave, or Move notifications.
189 * @param forceleave set to true by the box's parent if the mouse is inside an older
190 * sibling, which is covering up this box.
192 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
193 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
195 boolean wasinside = (flags & MOUSEINSIDE_FLAG) != 0;
196 boolean isinside = !((flags & INVISIBLE_FLAG) != 0) && inside(mousex, mousey) && !forceleave;
197 if (isinside) flags |= MOUSEINSIDE_FLAG; else flags &= ~MOUSEINSIDE_FLAG;
199 if (!wasinside && !isinside) return;
201 if (traps == null) { }
202 else if (!wasinside && isinside && traps.get("Enter") != null) put("Enter", Boolean.TRUE);
203 else if (wasinside && !isinside && traps.get("Leave") != null) put("Leave", Boolean.TRUE);
204 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && traps.get("Move") != null) put("Move", Boolean.TRUE);
206 if (isinside && cursor != null) getRoot().cursor = cursor;
208 // if the mouse has moved into our padding region, it is considered 'outside' all our children
209 if (!(mousex >= hpad && mousey >= vpad && mousex < width - hpad && mousey < height + vpad)) forceleave = true;
211 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
212 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
213 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
218 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
222 if (Surface.abort) return;
223 resize(x, y, width, height);
226 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
228 if (!needs_reflow) return;
229 if (numChildren() == 0) {
230 contentwidth = max(textwidth + 2 * hpad, minwidth);
231 contentheight = max(textheight + 2 * vpad, minheight);
235 // --- Phase 0 ----------------------------------------------------------------------
237 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
238 if (((flags & FONT_CHANGED_FLAG) != 0) && child.font == null) {
239 child.flags |= FONT_CHANGED_FLAG;
240 child.needs_reflow = true;
243 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
245 flags &= ~FONT_CHANGED_FLAG;
247 // --- Phase 1 ----------------------------------------------------------------------
248 // assign children to their row/column positions (assuming constrained columns)
249 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
250 //#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
252 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
253 Box child = getChild(0);
254 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
255 OUTER: for(int row=0; child != null; row++) {
256 for(int col=0; child != null && col < cols;) {
257 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
258 while(col < cols && numRowsInCol[col] > row) col++;
259 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
260 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
263 if (col + min(cols, child.colspan) > cols) break;
264 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
267 col += min(cols, child.colspan);
268 child = child.nextSibling();
269 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
275 // --- Phase 2 ----------------------------------------------------------------------
276 // compute the min/max sizes of the columns and rows and set our contentwidth
277 //#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
278 contentwidth = 2 * hpad;
281 for(Box child = getChild(0); child != null; child = child.nextSibling())
282 numCols = max(numCols, child.col + child.colspan);
283 LENGTH[] colWidth = new LENGTH[numCols];
284 for(Box child = getChild(0); child != null; child = child.nextSibling())
285 if (!(((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
286 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
287 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
288 contentwidth = max(textwidth + 2 * hpad, contentwidth);
289 contentwidth = bound(minwidth, contentwidth, maxwidth);
294 void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
296 // --- Phase 1 ----------------------------------------------------------------------
297 // run PosChange/SizeChange, dirty as needed
298 if (x != this.x || y != this.y || width != this.width || height != this.height) {
299 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
300 boolean sizechange = false, poschange = false;
301 if (traps != null && (this.width != width || this.height != height) && traps.get("SizeChange") != null) sizechange = true;
302 if (traps != null && (this.x != x || this.y != y) && traps.get("PosChange") != null) poschange = true;
303 this.width = width; this.height = height; this.x = x; this.y = y;
305 if (sizechange || poschange)
306 if (sizePosChangesSinceLastRender == 500) {
307 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
309 sizePosChangesSinceLastRender++;
310 if (sizechange) put("SizeChange", Boolean.TRUE);
311 if (poschange) put("PosChange", Boolean.TRUE);
312 Surface.abort = true;
318 // --- short circuit ----------------------------------------------------------------
319 if (!needs_reflow) return;
320 needs_reflow = false;
321 if (numChildren() == 0) return;
323 // --- Phase 2 ----------------------------------------------------------------------
324 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
326 //#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
329 for(Box child = getChild(0); child != null; child = child.nextSibling())
330 numCols = max(numCols, child.col + child.colspan);
331 LENGTH[] colWidth = new LENGTH[numCols];
332 LENGTH[] colMaxWidth = new LENGTH[numCols];
333 int marginWidth = width;
334 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
337 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
338 if (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)) continue;
339 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight HSHRINK_FLAG/VSHRINK_FLAG numCols/numRows
340 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
341 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
342 colMaxWidth[i] = max(colMaxWidth[i], (((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) / child.colspan);
346 //#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
347 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
349 for(int i=0; i<colMaxWidth.length; i++) {
350 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
351 marginWidth -= colMaxWidth[i];
352 if (marginWidth < 0) { marginWidth = 0; break; }
357 // --- Phase 3 ----------------------------------------------------------------------
358 // hand out the slack
360 //#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
361 slack = width - 2 * hpad;
362 for(int i=0; i<numCols; i++) slack -= colWidth[i];
363 if (numChildren() > 0)
365 // FEATURE: inefficient
366 int startslack = slack;
367 int increment = max(1, slack / numCols);
368 for(int col=0; col < numCols && slack > 0; col++) {
369 slack += colWidth[col];
370 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
371 slack -= colWidth[col];
373 if (slack == startslack) break;
378 // --- Phase 4 ----------------------------------------------------------------------
379 // assign children's new sizes and positions and recurse
380 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
381 if ((child.flags & INVISIBLE_FLAG) != 0) continue;
382 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
383 if ((child.flags & ABSOLUTE_FLAG) != 0) {
386 child_width = ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : min(child.maxwidth, width - child.x - hpad);
387 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y - vpad);
390 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight HSHRINK_FLAG/VSHRINK_FLAG marginWidth/marginHeight hpad/vpad child_x/child_y child_width/child_height
391 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
392 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
393 child_x = max(hpad, marginWidth / 2); for(int i=0; i<child.col; i++) child_x += colWidth[i];
394 if (diff < 0) child_x += -1 * (diff / 2);
398 child.resize(child_x, child_y, child_width, child_height);
405 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
407 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
408 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
409 if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
410 int globalx = parentx + (parent == null ? 0 : x);
411 int globaly = parenty + (parent == null ? 0 : y);
413 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
414 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw,
415 (parent == null ? 0 : globalx) + width) - globalx;
416 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph,
417 (parent == null ? 0 : globaly) + height) - globaly;
418 clipx = max(clipx, parent == null ? 0 : globalx);
419 clipy = max(clipy, parent == null ? 0 : globaly);
420 if (clipw <= 0 || cliph <= 0) return;
422 if ((fillcolor & 0xFF000000) != 0x00000000 || parent == null)
423 buf.fillRect(clipx, clipy, clipx + clipw, clipy + cliph,
424 (fillcolor & 0xFF000000) != 0 ? fillcolor : 0xFF777777);
427 if ((flags & TILE_FLAG) != 0) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
428 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
430 if (text != null && !text.equals(""))
431 renderText(x, y, clipx, clipy, clipw, cliph, buf);
433 // now subtract the pad region from the clip region before proceeding
434 clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
435 cliph = min(max(clipy, globaly + vpad) + cliph, globaly + height - vpad) - clipy;
436 clipx = max(clipx, globalx + hpad);
437 clipy = max(clipy, globaly + vpad);
439 for(Box b = getChild(0); b != null; b = b.nextSibling())
440 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf);
443 void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
444 //buf.setClip(x, y, w + x, h + y);
445 System.out.println("draw " + clipx + " " + clipy + " " + (clipx + clipw) + " " + (clipy + cliph));
446 buf.drawPicture(image,
447 clipx, clipy, clipx + clipw, clipy + cliph,
448 0, 0, image.getWidth(), image.getHeight());
449 //buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
452 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, DoubleBuffer buf) {
453 int iw = image.getWidth();
454 int ih = image.getHeight();
455 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
456 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
458 int dx1 = max(i * iw + x, x);
459 int dy1 = max(j * ih + y, y);
460 int dx2 = min((i+1) * iw + x, x + w);
461 int dy2 = min((j+1) * ih + y, y + h);
463 int sx1 = dx1 - (i*iw) - x;
464 int sy1 = dy1 - (j*ih) - y;
465 int sx2 = dx2 - (i*iw) - x;
466 int sy2 = dy2 - (j*ih) - y;
468 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
469 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
474 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, DoubleBuffer buf) {
476 // hack because (believe it or not) libgcj doesn't support UTF16.
477 byte[] b = new byte[text.length() * 2 + 2];
478 for(int i=0; i<text.length(); i++) {
479 b[i * 2] = (byte)((((short)text.charAt(i)) & 0xff00) >> 8);
480 b[i * 2 + 1] = (byte)(((short)text.charAt(i)) & 0xff);
482 b[text.length()] = 0;
483 b[text.length() + 1] = 0;
487 ImageDecoder id = org.xwt.translators.Font.render(new FileInputStream("COMIC.TTF"), 24, text, false);
488 Picture p = Platform.createPicture(id);
489 // FIXME: clipping (don't use setClip)
492 x + hpad + p.getWidth(), y + vpad + p.getHeight(),
494 p.getWidth(), p.getHeight());
495 } catch (Exception e) {
502 // Methods to implement org.xwt.js.JS //////////////////////////////////////
504 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
505 if ("indexof".equals(method)) {
506 if (checkOnly) return Boolean.TRUE;
507 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
508 Box b = (Box)args.elementAt(0);
509 if (b.parent != Box.this) {
510 if (redirect == null || redirect == Box.this) return new Integer(-1);
511 return redirect.callMethod(method, args, checkOnly);
513 return new Integer(b.getIndexInParent());
515 } else if ("apply".equals(method)) {
516 if (checkOnly) return Boolean.TRUE;
517 if (args.elementAt(0) instanceof String) {
518 String templatename = (String)args.elementAt(0);
519 Template t = Template.getTemplate(templatename, null);
521 if (Log.on) Log.logJS(this, "template " + templatename + " not found");
523 if (ThreadMessage.suspendThread()) try {
524 JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
525 t.apply(this, null, null, callback, 0, t.numUnits());
527 ThreadMessage.resumeThread();
530 } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
531 JS s = (JS)args.elementAt(0);
532 Object[] keys = s.keys();
533 for(int j=0; j<keys.length; j++) put(keys[j].toString(), s.get(keys[j]));
540 /** Returns the i_th child */
541 public Object get(int i) {
542 if (redirect == null) return null;
543 if (redirect != this) return redirect.get(i);
544 return i >= numChildren() || i < 0 ? null : getChild(i);
548 * Inserts value as child i; calls remove() if necessary.
549 * This method handles "reinserting" one of your children properly.
550 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
551 * WARNING: O(n) runtime, unless i == numChildren()
553 public void put(int i, Object value) {
556 if (value != null && !(value instanceof Box)) {
557 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to anything other than a box");
558 } else if (redirect == null) {
559 if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
560 } else if (redirect != this) {
561 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
562 redirect.put(i, value);
564 } else if (value == null) {
565 if (i >= 0 && i < numChildren()) {
570 } else if (value instanceof RootProxy) {
571 if (Log.on) Log.logJS(this, "attempt to reparent a box via its proxy object");
573 Box newnode = (Box)value;
575 // check if box being moved is currently target of a redirect
576 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
577 if (cur.redirect == newnode) {
578 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
582 // check for recursive ancestor violation
583 for(Box cur = this; cur != null; cur = cur.parent)
584 if (cur == newnode) {
585 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
586 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
590 if (numKids > 15 && children == null) convert_to_array();
592 newnode.parent = this;
594 if (children == null) {
595 if (firstKid == null) {
597 newnode.prevSibling = newnode;
598 newnode.nextSibling = newnode;
599 } else if (i >= numKids) {
600 newnode.prevSibling = firstKid.prevSibling;
601 newnode.nextSibling = firstKid;
602 firstKid.prevSibling.nextSibling = newnode;
603 firstKid.prevSibling = newnode;
606 for(int j=0; j<i; j++) cur = cur.nextSibling;
607 newnode.prevSibling = cur.prevSibling;
608 newnode.nextSibling = cur;
609 cur.prevSibling.nextSibling = newnode;
610 cur.prevSibling = newnode;
611 if (i == 0) firstKid = newnode;
616 if (i >= children.size()) {
617 newnode.indexInParent = children.size();
618 children.addElement(newnode);
620 children.insertElementAt(newnode, i);
621 for(int j=i; j<children.size(); j++)
622 getChild(j).indexInParent = j;
626 // need both of these in case child was already uncalc'ed
629 MARK_FOR_REFLOW_this;
633 // note that JavaScript box[0] will invoke put(int i), not put(String s)
638 public Object get_(Object name) { return super.get(name); }
639 public Object get(Object name) { return get(name, false); }
640 public Object get(Object name_, boolean ignoretraps) {
641 if (name_ instanceof Number) return get(((Number)name_).intValue());
643 if (!(name_ instanceof String)) return null;
644 String name = (String)name_;
645 if (name.equals("")) return null;
647 // See if we're reading back the function value of a trap
648 if (name.charAt(0) == '_') {
649 if (name.charAt(1) == '_') name = name.substring(2);
650 else name = name.substring(1);
651 Trap t = Trap.getTrap(this, name);
652 return t == null ? null : t.f;
655 // See if we're triggering a trap
656 Trap t = traps == null || ignoretraps ? null : (Trap)traps.get(name);
657 if (t != null && t.isreadtrap) return t.perform(Trap.emptyargs);
659 // Check for a special handler
660 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
661 if (gph != null) return gph.get(this);
663 Object ret = super.get(name);
664 if (name.startsWith("$") && ret == null)
665 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" + name.substring(1) + "\" found");
669 public Object[] keys() {
670 Object[] ret = new Object[numChildren()];
671 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
677 * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack)
678 * @param rp if this put is being performed via a root proxy, rp is the root proxy.
680 public void put_(Object name, Object value) { super.put(name, value); }
681 public void put(Object name, Object value) { put(name, value, false, null); }
682 public void put(Object name, Object value, boolean ignoretraps) { put(name, value, ignoretraps, null); }
683 public void put(Object name_, Object value, boolean ignoretraps, RootProxy rp) {
684 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
685 if (!(name_ instanceof String)) { super.put(name_,value); return; }
686 String name = name_.toString();
687 if (!ignoretraps && traps != null) {
688 Trap t = (Trap)traps.get(name);
690 JS.Array arg = new JS.Array();
691 arg.addElement(value);
693 arg.setElementAt(null, 0);
698 // don't want to really cascade down to the box on this one
699 if (name.equals("0")) return;
701 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
702 if (gph != null) { gph.put(name, this, value); return; }
704 if (name.charAt(0) == '_') {
705 if (value != null && !(value instanceof JS.Callable)) {
706 if (Log.on) Log.logJS(this, "attempt to put a non function value (" + value + ") to " + name);
707 } else if (value != null && !(value instanceof JS.CompiledFunction)) {
708 if (Log.on) Log.logJS(this, "attempt to put a non-compiled function value (" + value + ") to " + name);
709 } else if (name.charAt(1) == '_') {
710 name = name.substring(2).intern();
711 Trap t = Trap.getTrap(this, name);
712 if (t != null) t.delete();
713 if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), true, rp);
715 name = name.substring(1).intern();
716 Trap t = Trap.getTrap(this, name);
717 if (t != null) t.delete();
718 if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), false, rp);
723 super.put(name, value);
727 // Tree Manipulation /////////////////////////////////////////////////////////////////////
729 /** The parent of this node */
730 private Box parent = null;
732 // Variables used in Vector mode */
733 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
734 private int indexInParent;
735 private Vec children = null;
737 // Variables used in linked-list mode
738 private int numKids = 0;
739 private Box nextSibling = null;
740 private Box prevSibling = null;
741 private Box firstKid = null;
743 // when we get more than 15 children, we switch to array-mode
744 private void convert_to_array() {
745 children = new Vec(numKids);
748 children.addElement(cur);
749 cur.indexInParent = children.size() - 1;
750 cur = cur.nextSibling;
751 } while (cur != firstKid);
754 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
755 public void remove() {
756 if (parent == null) {
757 if (surface != null) surface.dispose(true);
760 Box oldparent = parent;
761 if (oldparent == null) return;
762 MARK_FOR_REFLOW_this;
764 flags &= ~MOUSEINSIDE_FLAG;
766 if (parent.children != null) {
767 parent.children.removeElementAt(indexInParent);
768 for(int j=indexInParent; j<parent.children.size(); j++)
769 (parent.getChild(j)).indexInParent = j;
772 if (parent.firstKid == this) {
773 if (nextSibling == this) parent.firstKid = null;
774 else parent.firstKid = nextSibling;
777 prevSibling.nextSibling = nextSibling;
778 nextSibling.prevSibling = prevSibling;
784 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
786 // note that JavaScript box[0] will invoke put(int i), not put(String s)
787 if (oldparent != null) oldparent.put("0", this);
790 /** returns our next sibling (parent[ourindex + 1]) */
791 public final Box nextSibling() {
792 if (parent == null) return null;
793 if (parent.children == null) {
794 if (nextSibling == parent.firstKid) return null;
797 if (indexInParent >= parent.children.size() - 1) return null;
798 return (Box)parent.children.elementAt(indexInParent + 1);
802 /** returns our next sibling (parent[ourindex + 1]) */
803 public final Box prevSibling() {
804 if (parent == null) return null;
805 if (parent.children == null) {
806 if (this == parent.firstKid) return null;
809 if (indexInParent == 0) return null;
810 return (Box)parent.children.elementAt(indexInParent - 1);
814 /** Returns the parent of this node */
815 public Box getParent() { return parent; }
817 /** Returns ith child */
818 public Box getChild(int i) {
819 if (children == null) {
820 if (firstKid == null) return null;
821 if (i >= numKids) return null;
822 if (i == numKids - 1) return firstKid.prevSibling;
824 for(int j=0; j<i; j++) cur = cur.nextSibling;
827 if (i >= children.size() || i < 0) return null;
828 return (Box)children.elementAt(i);
832 /** Returns the number of children */
833 public int numChildren() {
834 if (children == null) {
835 if (firstKid == null) return 0;
837 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
840 return children.size();
844 /** Returns our index in our parent */
845 public int getIndexInParent() {
846 if (parent == null) return 0;
847 if (parent.children == null) {
849 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
852 return indexInParent;
856 /** returns the root of the surface that this box belongs to */
857 public final Box getRoot() {
858 if (parent == null && surface != null) return this;
859 if (parent == null) return null;
860 return parent.getRoot();
864 // Root Proxy ///////////////////////////////////////////////////////////////////////////////
866 // FEATURE: use xwt.graft() here
867 RootProxy myproxy = null;
868 public JS getRootProxy() {
869 if (myproxy == null) myproxy = new RootProxy(this);
873 private static class RootProxy extends JS {
875 RootProxy(Box b) { this.box = b; }
876 public Object get(Object name) { return box.get(name); }
877 public void put(Object name, Object value) { box.put(name, value, false, this); }
878 public Object[] keys() { return box.keys(); }
879 public Object callMethod(Object method, JS.Array args, boolean justChecking) {
880 return ((Box)box).callMethod(method,args,justChecking);
885 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
887 static final int min(int a, int b) { if (a<b) return a; else return b; }
888 static final double min(double a, double b) { if (a<b) return a; else return b; }
889 static final int max(int a, int b) { if (a>b) return a; else return b; }
890 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; }
891 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; }
892 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
893 final boolean inside(int x, int y) { return (!((flags & INVISIBLE_FLAG) != 0) && x >= 0 && y >= 0 && x < width && y < height); }
895 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
896 public static Box whoIs(Box cur, int x, int y) {
898 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
902 // WARNING: this method is called from the event-queueing
903 // thread -- it may run concurrently with ANY part of XWT, and
904 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
907 if ((cur.flags & INVISIBLE_FLAG) != 0) return null;
908 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
910 for(int i=cur.numChildren() - 1; i>=0; i--) {
911 Box child = cur.getChild(i);
912 if (child == null) continue; // since this method is unsynchronized, we have to double-check
915 if (!((child.flags & INVISIBLE_FLAG) != 0) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
925 * A helper class for properties of Box which require special
928 * To avoid excessive use of String.equals(), the Box.get() and
929 * Box.put() methods employ a Hash keyed on property names that
930 * require special handling. The value stored in the Hash is an
931 * instance of an anonymous subclass of SpecialBoxProperty, which knows
932 * how to handle get()s and put()s for that property name. There
933 * should be one anonymous subclass of SpecialBoxProperty for each
934 * specially-handled property on Box.
936 static class SpecialBoxProperty {
938 SpecialBoxProperty() { }
940 /** stores instances of SpecialBoxProperty; keyed on property name */
941 static Hash specialBoxProperties = new Hash(200, 3);
943 /** this method defines the behavior when the property is get()ed from b */
944 Object get(Box b) { return null; }
946 /** this method defines the behavior when the property is put() to b */
947 void put(Box b, Object value) { }
949 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
950 void put(String name, Box b, Object value) { put(b, value); }
953 //#repeat fillcolor/strokecolor
954 specialBoxProperties.put("fillcolor", new SpecialBoxProperty() {
955 public Object get(Box b) {
956 if ((b.fillcolor & 0xFF000000) == 0) return null;
957 String red = Integer.toHexString((b.fillcolor & 0x00FF0000) >> 16);
958 String green = Integer.toHexString((b.fillcolor & 0x0000FF00) >> 8);
959 String blue = Integer.toHexString(b.fillcolor & 0x000000FF);
960 if (red.length() < 2) red = "0" + red;
961 if (blue.length() < 2) blue = "0" + blue;
962 if (green.length() < 2) green = "0" + green;
963 return "#" + red + green + blue;
965 public void put(Box b, Object value) {
966 int newcolor = b.fillcolor;
967 String s = value == null ? null : value.toString();
968 if (value == null) newcolor = 0x00000000;
969 else if (s.length() > 0 && s.charAt(0) == '#')
971 newcolor = 0xFF000000 |
972 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
973 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
974 Integer.parseInt(s.substring(5, 7), 16);
975 } catch (NumberFormatException e) {
976 Log.log(this, "invalid color " + s);
979 else if (SVG.colors.get(s) != null)
980 newcolor = 0xFF000000 | ((Integer)SVG.colors.get(s)).intValue();
981 if (newcolor == b.fillcolor) return;
982 b.fillcolor = newcolor;
988 specialBoxProperties.put("color", new SpecialBoxProperty() {
989 public Object get(Box b) { return b.get("fillcolor"); }
990 public void put(Box b, Object value) { b.put("fillcolor", value); }
993 specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
994 public Object get(Box b) { return b.get("strokecolor"); }
995 public void put(Box b, Object value) { b.put("strokecolor", value); }
998 specialBoxProperties.put("text", new SpecialBoxProperty() {
999 public Object get(Box b) { return b.text; }
1000 public void put(Box b, Object value) {
1001 String t = value == null ? "null" : value.toString();
1002 if (t.equals(b.text)) return;
1005 if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
1006 b.textwidth = b.textheight = 0;
1009 ImageDecoder id = org.xwt.translators.Font.render(new FileInputStream("COMIC.TTF"), 24, b.text, true);
1010 if (id.getWidth() != b.textwidth || id.getHeight() != b.textheight) MARK_FOR_REFLOW_b;
1011 b.textwidth = id.getWidth();
1012 b.textheight = id.getHeight();
1013 } catch (Exception e) {
1020 specialBoxProperties.put("font", new SpecialBoxProperty() {
1021 public Object get(Box b) { return b.font; }
1022 public void put(Box b, Object value) {
1023 b.font = value == null ? null : value.toString();
1024 // FIXME: translate value into a resource
1026 b.flags |= FONT_CHANGED_FLAG;
1030 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1031 public Object get(Box b) { return b; }
1032 public void put(Box b, Object value) {
1033 if (value == null) b.remove();
1034 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1035 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1039 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1040 public Object get(Box b) {
1041 Log.log(this, "warning: the orient property is deprecated");
1042 if (b.redirect == null) return "horizontal";
1043 else if (b.redirect != b) return get(b.redirect);
1044 else if (b.cols == 1) return "vertical";
1045 else if (b.rows == 1) return "horizontal";
1048 public void put(Box b, Object value) {
1049 Log.log(this, "warning: the orient property is deprecated");
1050 if (value == null) return;
1051 if (b.redirect == null) return;
1052 if (b.redirect != b) { put(b.redirect, value); return; }
1053 if (value.equals("vertical")) {
1054 if (b.rows == 0) return;
1055 b.rows = 0; b.cols = 1;
1056 } else if (value.equals("horizontal")) {
1057 if (b.cols == 0) return;
1058 b.cols = 0; b.rows = 1;
1060 Log.log(this, "invalid value put to orient property: " + value);
1064 specialBoxProperties.put("static", new SpecialBoxProperty() {
1065 public Object get(Box b) {
1067 JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
1068 for(int i=0; i<cfsn.length() - 1; i++)
1069 if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
1070 cfsn = cfsn.substring(0, i);
1073 return Static.getStatic(cfsn);
1077 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1078 public Object get(Box b) { return (((b.flags & HSHRINK_FLAG) != 0) || ((b.flags & VSHRINK_FLAG) != 0)) ? Boolean.TRUE : Boolean.FALSE; }
1079 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1082 //#repeat hshrink/vshrink HSHRINK_FLAG/VSHRINK_FLAG
1083 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1084 public Object get(Box b) { return new Boolean((b.flags & HSHRINK_FLAG) != 0); }
1085 public void put(Box b, Object value) {
1086 boolean newshrink = stob(value);
1087 if (((b.flags & HSHRINK_FLAG) != 0) == newshrink) return;
1088 if (newshrink) b.flags |= HSHRINK_FLAG; else b.flags &= ~HSHRINK_FLAG;
1095 specialBoxProperties.put("x", new SpecialBoxProperty() {
1096 public Object get(Box b) {
1097 if (b.surface == null) return new Integer(0);
1098 if ((b.flags & INVISIBLE_FLAG) != 0) return new Integer(0);
1099 return new Integer(b.x);
1101 public void put(Box b, Object value) {
1102 if (!((b.flags & ABSOLUTE_FLAG) != 0)) return;
1103 int x = stoi(value);
1104 if (x == b.x) return;
1107 if (b.parent == null && b.surface != null) {
1108 b.surface.setLocation();
1109 b.surface.centerSurfaceOnRender = false;
1117 //#repeat width/height minwidth/minheight maxwidth/maxheight
1118 specialBoxProperties.put("width", new SpecialBoxProperty() {
1119 public Object get(Box b) { return new Integer(b.width); }
1120 public void put(Box b, Object value) {
1121 b.width = stoi(value);
1122 if (b.parent == null && b.surface != null) {
1123 b.surface.setSize();
1126 if (b.minwidth == b.width && b.maxwidth == b.width) return;
1127 b.minwidth = b.maxwidth = b.width;
1133 //#repeat cols/rows rows/cols
1134 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1135 public Object get(Box b) { return new Double(b.cols); }
1136 public void put(Box b, Object value) {
1137 if (b.cols == stoi(value)) return;
1138 b.cols = stoi(value);
1139 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1140 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1145 //#repeat colspan/rowspan
1146 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1147 public Object get(Box b) { return new Double(b.colspan); }
1148 public void put(Box b, Object value) {
1149 if (b.colspan == stoi(value)) return;
1150 b.colspan = stoi(value);
1156 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1157 public Object get(Box b) { return ((b.flags & TILE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1158 public void put(Box b, Object value) {
1159 if (((b.flags & TILE_FLAG) != 0) == stob(value)) return;
1160 if (stob(value)) b.flags |= TILE_FLAG; else b.flags &= ~TILE_FLAG;
1164 specialBoxProperties.put("invisible", new SpecialBoxProperty() {
1165 public Object get(Box b) {
1166 for (Box cur = b; cur != null; cur = cur.parent) {
1167 if ((cur.flags & INVISIBLE_FLAG) != 0) return Boolean.TRUE; }
1168 return Boolean.FALSE;
1170 public void put(Box b, Object value) {
1171 if (stob(value) == ((b.flags & INVISIBLE_FLAG) != 0)) return;
1172 if (stob(value)) b.flags |= INVISIBLE_FLAG; else b.flags &= ~INVISIBLE_FLAG;
1173 if (b.parent == null) {
1174 if (b.surface != null) b.surface.setInvisible((b.flags & INVISIBLE_FLAG) != 0);
1177 MARK_FOR_REFLOW_b_parent;
1178 b.parent.dirty(b.x, b.y, b.width, b.height);
1182 specialBoxProperties.put("absolute", new SpecialBoxProperty() {
1183 public Object get(Box b) { return ((b.flags & ABSOLUTE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1184 public void put(Box b, Object value) {
1185 if (stob(value) == ((b.flags & ABSOLUTE_FLAG) != 0)) return;
1186 if (stob(value)) b.flags |= ABSOLUTE_FLAG; else b.flags &= ~ABSOLUTE_FLAG;
1187 if ((b.flags & ABSOLUTE_FLAG) != 0) { b.x = 0; b.y = 0; }
1188 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1191 specialBoxProperties.put("image", new SpecialBoxProperty() {
1192 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1193 public void put(Box b, Object value) {
1194 if ((value == null && b.image == null) ||
1195 (value != null && b.image != null && value.equals(ImageDecoder.imageToNameMap.get(b.image)))) return;
1196 String s = value == null ? null : value.toString();
1197 if (s == null || s.equals("")) b.image = null;
1199 if ((b.image = ImageDecoder.getPicture(s)) == null) {
1200 if (Log.on) Log.logJS(Box.class, "unable to load image " + s);
1202 b.minwidth = b.image.getWidth();
1203 b.minheight = b.image.getHeight();
1211 //#repeat globalx/globaly x/y
1212 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1213 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1214 public void put(Box b, Object value) {
1215 if (b.surface == null || b.parent == null) return;
1216 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1222 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1223 public Object get(Box b) { return b.cursor; }
1224 public void put(Box b, Object value) {
1225 b.cursor = (String)value;
1226 if (b.surface == null) return;
1228 // see if we need to update the surface cursor
1229 Surface surface = b.getRoot().surface;
1230 String tempcursor = surface.cursor;
1231 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1232 if (surface.cursor != tempcursor) surface.syncCursor();
1236 //#repeat mousex/mousey x/y
1237 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1238 public Object get(Box b) {
1239 Surface surface = b.getRoot().surface;
1240 if (surface == null) return new Integer(0);
1241 int mousex = surface.mousex;
1242 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1243 return new Integer(mousex);
1248 specialBoxProperties.put("xwt", new SpecialBoxProperty() {
1249 public Object get(Box b) { return XWT.singleton; }
1252 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1253 public Object get(Box b) {
1254 return ((b.flags & MOUSEINSIDE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1257 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1258 public Object get(Box b) {
1259 if (b.redirect == null) return new Integer(0);
1260 if (b.redirect != b) return get(b.redirect);
1261 return new Integer(b.numChildren());
1264 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1265 public void put(String name, Box b, Object value) {
1266 Surface surface = b.getRoot().surface;
1267 if (surface == null) return;
1268 int mousex = surface.mousex;
1269 int mousey = surface.mousey;
1270 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1274 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1275 if (c.inside(mousex - c.x, mousey - c.y)) {
1280 if (b.parent != null) b.parent.put(name, value);
1283 specialBoxProperties.put("Press1", mouseEventHandler);
1284 specialBoxProperties.put("Press2", mouseEventHandler);
1285 specialBoxProperties.put("Press3", mouseEventHandler);
1286 specialBoxProperties.put("Release1", mouseEventHandler);
1287 specialBoxProperties.put("Release2", mouseEventHandler);
1288 specialBoxProperties.put("Release3", mouseEventHandler);
1289 specialBoxProperties.put("Click1", mouseEventHandler);
1290 specialBoxProperties.put("Click2", mouseEventHandler);
1291 specialBoxProperties.put("Click3", mouseEventHandler);
1292 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1293 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1294 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1296 specialBoxProperties.put("root", new SpecialBoxProperty() {
1297 public Object get(Box b) {
1298 if (b.getRoot() == null) return null;
1299 else if (b.parent == null) return b;
1300 else return b.getRoot().getRootProxy();
1303 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1304 public Object get(Box b) {
1305 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1308 public void put(Box b, Object value) {
1309 if (b.surface == null) return;
1310 boolean val = stob(value);
1311 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1315 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1316 public Object get(Box b) {
1317 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1320 public void put(Box b, Object value) {
1321 if (b.surface == null) return;
1322 boolean val = stob(value);
1323 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1327 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1328 public void put(Box b, Object value) {
1329 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1333 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1334 public void put(Box b, Object value) {
1335 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1339 //#repeat hscar/vscar
1340 specialBoxProperties.put("hscar", new SpecialBoxProperty() {
1341 public void put(Box b, Object value) {
1342 if (b.parent == null && b.surface != null) {
1343 b.surface.hscar = stoi(value);
1344 b.surface.dirty(0, 0, b.width, b.height);
1345 b.surface.Refresh();
1351 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1352 public void put(Box b, Object value) {
1353 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1357 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1358 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
1359 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
1360 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1361 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1364 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
1365 public Object get(Box b) {
1366 if (b.redirect == null) return new Integer(0);
1367 if (b.redirect != b) return get(b.redirect);
1368 return new Integer(b.hpad);
1370 public void put(Box b, Object value) {
1371 if (b.redirect == null) return;
1372 if (b.redirect != b) { put(b.redirect, value); return; }
1373 int newval = stoi(value);
1374 if (newval == b.hpad) return;
1381 //#repeat minwidth/minheight maxwidth/maxheight
1382 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1383 public Object get(Box b) { return new Integer(b.minwidth); }
1384 public void put(Box b, Object value) {
1385 if (stoi(value) == b.minwidth) return;
1386 b.minwidth = stoi(value);
1390 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1391 public Object get(Box b) { return new Integer(b.maxwidth); }
1392 public void put(Box b, Object value) {
1393 if (stoi(value) == b.maxwidth) return;
1394 b.maxwidth = stoi(value);
1400 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1401 public void put(Box b, Object value) { }
1402 public Object get(Box b) {
1403 if (b.redirect == null) return null;
1404 if (b.redirect == b) return Boolean.TRUE;
1405 return get(b.redirect);
1409 // FEATURE: this still isn't totally harmonious; when you createSurface, these aren't checked
1410 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1411 public void put(Box b, Object value) {
1412 if (b.surface != null) b.surface.setTitleBarText(value.toString());
1413 b.put_("titlebar", value);
1415 public Object get(Box b) { return b.get_("titlebar"); }
1418 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1419 public void put(Box b, Object value) {
1420 b.put_("icon", value);
1421 if (b.surface == null) return;
1422 Picture pic = ImageDecoder.getPicture(value.toString());
1423 if (pic != null) b.surface.setIcon(pic);
1424 else if (Log.on) Log.log(this, "unable to load icon " + value);
1426 public Object get(Box b) { return b.get_("icon"); }
1431 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1432 public static boolean stob(Object o) {
1433 if (o == null) return false;
1434 return Boolean.TRUE.equals(o) || "true".equals(o);
1437 /** helper that converts a String to an int according to JavaScript coercion rules */
1438 public static int stoi(Object o) {
1439 if (o == null) return 0;
1440 if (o instanceof Integer) return ((Integer)o).intValue();
1443 if (!(o instanceof String)) s = o.toString();
1446 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1447 catch (NumberFormatException e) { return 0; }