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 PixelBuffer 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 Res font = null;
130 private int fontsize = 10;
131 private LENGTH textwidth = 0;
132 private LENGTH textheight = 0;
134 // FEATURE: use shorts
135 private int rows = 1;
136 private int cols = 0;
137 private int rowspan = 1;
138 private int colspan = 1;
140 // computed during reflow
143 public LENGTH width = 0;
144 public LENGTH height = 0;
145 private int row = 0; // FEATURE use a short
146 private int col = 0; // FEATURE use a short
147 private LENGTH contentwidth = 0; // == max(minwidth, textwidth+pad, sum(child.contentwidth) + pad)
148 private LENGTH contentheight = 0;
151 // Rendering Properties ///////////////////////////////////////////////////////////
153 //private SVG.VP path = null;
154 //private SVG.Paint fill = null;
155 //private SVG.Paint stroke = null;
157 private Picture image; // will disappear
158 private int fillcolor = 0x00000000; // will become SVG.Paint
159 private int strokecolor = 0xFF000000; // will become SVG.Paint
161 private String cursor = null; // the cursor for this box
164 // Instance Methods /////////////////////////////////////////////////////////////////////
166 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
167 public final void dirty() { dirty(0, 0, width, height); }
168 public final void dirty(int x, int y, int w, int h) {
170 for(Box cur = this; cur != null; cur = cur.parent) {
171 w = min(x + w, cur.width) - max(x, 0);
172 h = min(y + h, cur.height) - max(y, 0);
175 if (w <= 0 || h <= 0) return;
176 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
182 for(cur = this; cur.parent != null; cur = cur.parent);
183 if (cur.surface != null) cur.surface.dirty(0, 0, cur.width, cur.height);
187 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
188 * to see if this node requires any Enter, Leave, or Move notifications.
190 * @param forceleave set to true by the box's parent if the mouse is inside an older
191 * sibling, which is covering up this box.
193 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
194 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
196 boolean wasinside = (flags & MOUSEINSIDE_FLAG) != 0;
197 boolean isinside = !((flags & INVISIBLE_FLAG) != 0) && inside(mousex, mousey) && !forceleave;
198 if (isinside) flags |= MOUSEINSIDE_FLAG; else flags &= ~MOUSEINSIDE_FLAG;
200 if (!wasinside && !isinside) return;
202 if (traps == null) { }
203 else if (!wasinside && isinside && traps.get("Enter") != null) put("Enter", Boolean.TRUE);
204 else if (wasinside && !isinside && traps.get("Leave") != null) put("Leave", Boolean.TRUE);
205 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && traps.get("Move") != null) put("Move", Boolean.TRUE);
207 if (isinside && cursor != null) getRoot().cursor = cursor;
209 // if the mouse has moved into our padding region, it is considered 'outside' all our children
210 if (!(mousex >= hpad && mousey >= vpad && mousex < width - hpad && mousey < height + vpad)) forceleave = true;
212 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
213 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
214 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
219 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
223 if (Surface.abort) return;
224 resize(x, y, width, height);
227 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
229 if (!needs_reflow) return;
230 if (numChildren() == 0) {
231 contentwidth = max(textwidth + 2 * hpad, minwidth);
232 contentheight = max(textheight + 2 * vpad, minheight);
236 // --- Phase 0 ----------------------------------------------------------------------
238 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
239 if (((flags & FONT_CHANGED_FLAG) != 0) && child.font == null) {
240 child.flags |= FONT_CHANGED_FLAG;
241 child.needs_reflow = true;
244 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
246 flags &= ~FONT_CHANGED_FLAG;
248 // --- Phase 1 ----------------------------------------------------------------------
249 // assign children to their row/column positions (assuming constrained columns)
250 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
251 //#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
253 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
254 Box child = getChild(0);
255 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
256 OUTER: for(int row=0; child != null; row++) {
257 for(int col=0; child != null && col < cols;) {
258 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
259 while(col < cols && numRowsInCol[col] > row) col++;
260 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
261 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
264 if (col + min(cols, child.colspan) > cols) break;
265 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
268 col += min(cols, child.colspan);
269 child = child.nextSibling();
270 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
276 // --- Phase 2 ----------------------------------------------------------------------
277 // compute the min/max sizes of the columns and rows and set our contentwidth
278 //#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
279 contentwidth = 2 * hpad;
282 for(Box child = getChild(0); child != null; child = child.nextSibling())
283 numCols = max(numCols, child.col + child.colspan);
284 LENGTH[] colWidth = new LENGTH[numCols];
285 for(Box child = getChild(0); child != null; child = child.nextSibling())
286 if (!(((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
287 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
288 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
289 contentwidth = max(textwidth + 2 * hpad, contentwidth);
290 contentwidth = bound(minwidth, contentwidth, maxwidth);
295 void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
297 // --- Phase 1 ----------------------------------------------------------------------
298 // run PosChange/SizeChange, dirty as needed
299 if (x != this.x || y != this.y || width != this.width || height != this.height) {
300 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
301 boolean sizechange = false, poschange = false;
302 if (traps != null && (this.width != width || this.height != height) && traps.get("SizeChange") != null) sizechange = true;
303 if (traps != null && (this.x != x || this.y != y) && traps.get("PosChange") != null) poschange = true;
304 this.width = width; this.height = height; this.x = x; this.y = y;
306 if (sizechange || poschange)
307 if (sizePosChangesSinceLastRender == 500) {
308 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
310 sizePosChangesSinceLastRender++;
311 if (sizechange) put("SizeChange", Boolean.TRUE);
312 if (poschange) put("PosChange", Boolean.TRUE);
313 Surface.abort = true;
319 // --- short circuit ----------------------------------------------------------------
320 if (!needs_reflow) return;
321 needs_reflow = false;
322 if (numChildren() == 0) return;
324 // --- Phase 2 ----------------------------------------------------------------------
325 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
327 //#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
330 for(Box child = getChild(0); child != null; child = child.nextSibling())
331 numCols = max(numCols, child.col + child.colspan);
332 LENGTH[] colWidth = new LENGTH[numCols];
333 LENGTH[] colMaxWidth = new LENGTH[numCols];
334 int marginWidth = width;
335 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
338 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
339 if (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)) continue;
340 //#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
341 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
342 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
343 colMaxWidth[i] = max(colMaxWidth[i], (((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) / child.colspan);
347 //#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
348 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
350 for(int i=0; i<colMaxWidth.length; i++) {
351 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
352 marginWidth -= colMaxWidth[i];
353 if (marginWidth < 0) { marginWidth = 0; break; }
358 // --- Phase 3 ----------------------------------------------------------------------
359 // hand out the slack
361 //#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
362 slack = width - 2 * hpad;
363 for(int i=0; i<numCols; i++) slack -= colWidth[i];
364 if (numChildren() > 0)
366 // FEATURE: inefficient
367 int startslack = slack;
368 int increment = max(1, slack / numCols);
369 for(int col=0; col < numCols && slack > 0; col++) {
370 slack += colWidth[col];
371 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
372 slack -= colWidth[col];
374 if (slack == startslack) break;
379 // --- Phase 4 ----------------------------------------------------------------------
380 // assign children's new sizes and positions and recurse
381 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
382 if ((child.flags & INVISIBLE_FLAG) != 0) continue;
383 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
384 if ((child.flags & ABSOLUTE_FLAG) != 0) {
387 child_width = ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : min(child.maxwidth, width - child.x - hpad);
388 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y - vpad);
391 //#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
392 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
393 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
394 child_x = max(hpad, marginWidth / 2); for(int i=0; i<child.col; i++) child_x += colWidth[i];
395 if (diff < 0) child_x += -1 * (diff / 2);
399 child.resize(child_x, child_y, child_width, child_height);
406 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
408 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
409 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
410 if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
411 int globalx = parentx + (parent == null ? 0 : x);
412 int globaly = parenty + (parent == null ? 0 : y);
414 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
415 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw,
416 (parent == null ? 0 : globalx) + width) - globalx;
417 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph,
418 (parent == null ? 0 : globaly) + height) - globaly;
419 clipx = max(clipx, parent == null ? 0 : globalx);
420 clipy = max(clipy, parent == null ? 0 : globaly);
421 if (clipw <= 0 || cliph <= 0) return;
423 if ((fillcolor & 0xFF000000) != 0x00000000 || parent == null)
424 buf.fillRect(clipx, clipy, clipx + clipw, clipy + cliph,
425 (fillcolor & 0xFF000000) != 0 ? fillcolor : 0xFF777777);
428 if ((flags & TILE_FLAG) != 0) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
429 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
431 if (text != null && !text.equals(""))
432 renderText(globalx, globaly, clipx, clipy, clipw, cliph, buf);
434 // now subtract the pad region from the clip region before proceeding
435 clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
436 cliph = min(max(clipy, globaly + vpad) + cliph, globaly + height - vpad) - clipy;
437 clipx = max(clipx, globalx + hpad);
438 clipy = max(clipy, globaly + vpad);
440 for(Box b = getChild(0); b != null; b = b.nextSibling())
441 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf);
444 void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
445 //buf.setClip(x, y, w + x, h + y);
446 System.out.println("draw " + clipx + " " + clipy + " " + (clipx + clipw) + " " + (clipy + cliph));
447 buf.drawPicture(image,
448 clipx, clipy, clipx + clipw, clipy + cliph,
449 0, 0, image.getWidth(), image.getHeight());
450 //buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
453 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, PixelBuffer buf) {
454 int iw = image.getWidth();
455 int ih = image.getHeight();
456 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
457 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
459 int dx1 = max(i * iw + x, x);
460 int dy1 = max(j * ih + y, y);
461 int dx2 = min((i+1) * iw + x, x + w);
462 int dy2 = min((j+1) * ih + y, y + h);
464 int sx1 = dx1 - (i*iw) - x;
465 int sy1 = dy1 - (j*ih) - y;
466 int sx2 = dx2 - (i*iw) - x;
467 int sy2 = dy2 - (j*ih) - y;
469 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
470 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
475 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
476 for(int i=0; i<text.length(); i++) {
478 char c = text.charAt(i);
479 Glyph g = Glyph.getGlyph(font, fontsize, c);
480 System.out.println("rendering glyph for " + c + " as " + g + " @ " + (x+hpad) + ", " + (y+vpad));
483 y + vpad + g.max_ascent - g.baseline,
484 x + hpad + g.p.getWidth(),
485 y + vpad + g.max_ascent - g.baseline + g.p.getHeight(),
487 g.p.getWidth(), g.p.getHeight());
493 // Methods to implement org.xwt.js.JS //////////////////////////////////////
495 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
496 if ("indexof".equals(method)) {
497 if (checkOnly) return Boolean.TRUE;
498 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
499 Box b = (Box)args.elementAt(0);
500 if (b.parent != Box.this) {
501 if (redirect == null || redirect == Box.this) return new Integer(-1);
502 return redirect.callMethod(method, args, checkOnly);
504 return new Integer(b.getIndexInParent());
506 } else if ("apply".equals(method)) {
507 if (checkOnly) return Boolean.TRUE;
508 if (args.elementAt(0) instanceof Res) {
510 Res res = (Res)args.elementAt(0);
511 // res = res.addExtension(".xwt");
512 Template t = Template.buildTemplate(res.getInputStream(), "fromResource");
513 if (ThreadMessage.suspendThread()) try {
514 JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
515 t.apply(this, null, null, callback, 0, t.numUnits());
517 ThreadMessage.resumeThread();
519 } catch (IOException e) {
522 } else if (args.elementAt(0) instanceof String) {
523 String templatename = (String)args.elementAt(0);
524 Template t = Template.getTemplate(templatename, null);
526 if (Log.on) Log.logJS(this, "template " + templatename + " not found");
528 if (ThreadMessage.suspendThread()) try {
529 JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
530 t.apply(this, null, null, callback, 0, t.numUnits());
532 ThreadMessage.resumeThread();
535 } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
536 JS s = (JS)args.elementAt(0);
537 Object[] keys = s.keys();
538 for(int j=0; j<keys.length; j++) put(keys[j].toString(), s.get(keys[j]));
545 /** Returns the i_th child */
546 public Object get(int i) {
547 if (redirect == null) return null;
548 if (redirect != this) return redirect.get(i);
549 return i >= numChildren() || i < 0 ? null : getChild(i);
553 * Inserts value as child i; calls remove() if necessary.
554 * This method handles "reinserting" one of your children properly.
555 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
556 * WARNING: O(n) runtime, unless i == numChildren()
558 public void put(int i, Object value) {
561 if (value != null && !(value instanceof Box)) {
562 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to anything other than a box");
563 } else if (redirect == null) {
564 if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
565 } else if (redirect != this) {
566 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
567 redirect.put(i, value);
569 } else if (value == null) {
570 if (i >= 0 && i < numChildren()) {
575 } else if (value instanceof RootProxy) {
576 if (Log.on) Log.logJS(this, "attempt to reparent a box via its proxy object");
578 Box newnode = (Box)value;
580 // check if box being moved is currently target of a redirect
581 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
582 if (cur.redirect == newnode) {
583 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
587 // check for recursive ancestor violation
588 for(Box cur = this; cur != null; cur = cur.parent)
589 if (cur == newnode) {
590 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
591 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
595 if (numKids > 15 && children == null) convert_to_array();
597 newnode.parent = this;
599 if (children == null) {
600 if (firstKid == null) {
602 newnode.prevSibling = newnode;
603 newnode.nextSibling = newnode;
604 } else if (i >= numKids) {
605 newnode.prevSibling = firstKid.prevSibling;
606 newnode.nextSibling = firstKid;
607 firstKid.prevSibling.nextSibling = newnode;
608 firstKid.prevSibling = newnode;
611 for(int j=0; j<i; j++) cur = cur.nextSibling;
612 newnode.prevSibling = cur.prevSibling;
613 newnode.nextSibling = cur;
614 cur.prevSibling.nextSibling = newnode;
615 cur.prevSibling = newnode;
616 if (i == 0) firstKid = newnode;
621 if (i >= children.size()) {
622 newnode.indexInParent = children.size();
623 children.addElement(newnode);
625 children.insertElementAt(newnode, i);
626 for(int j=i; j<children.size(); j++)
627 getChild(j).indexInParent = j;
631 // need both of these in case child was already uncalc'ed
634 MARK_FOR_REFLOW_this;
638 // note that JavaScript box[0] will invoke put(int i), not put(String s)
643 public Object get_(Object name) { return super.get(name); }
644 public Object get(Object name) { return get(name, false); }
645 public Object get(Object name_, boolean ignoretraps) {
646 if (name_ instanceof Number) return get(((Number)name_).intValue());
648 if (!(name_ instanceof String)) return null;
649 String name = (String)name_;
650 if (name.equals("")) return null;
652 // See if we're reading back the function value of a trap
653 if (name.charAt(0) == '_') {
654 if (name.charAt(1) == '_') name = name.substring(2);
655 else name = name.substring(1);
656 Trap t = Trap.getTrap(this, name);
657 return t == null ? null : t.f;
660 // See if we're triggering a trap
661 Trap t = traps == null || ignoretraps ? null : (Trap)traps.get(name);
662 if (t != null && t.isreadtrap) return t.perform(Trap.emptyargs);
664 // Check for a special handler
665 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
666 if (gph != null) return gph.get(this);
668 Object ret = super.get(name);
669 if (name.startsWith("$") && ret == null)
670 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" + name.substring(1) + "\" found");
674 public Object[] keys() {
675 Object[] ret = new Object[numChildren()];
676 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
682 * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack)
683 * @param rp if this put is being performed via a root proxy, rp is the root proxy.
685 public void put_(Object name, Object value) { super.put(name, value); }
686 public void put(Object name, Object value) { put(name, value, false, null); }
687 public void put(Object name, Object value, boolean ignoretraps) { put(name, value, ignoretraps, null); }
688 public void put(Object name_, Object value, boolean ignoretraps, RootProxy rp) {
689 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
690 if (!(name_ instanceof String)) { super.put(name_,value); return; }
691 String name = name_.toString();
692 if (!ignoretraps && traps != null) {
693 Trap t = (Trap)traps.get(name);
695 JS.Array arg = new JS.Array();
696 arg.addElement(value);
698 arg.setElementAt(null, 0);
703 // don't want to really cascade down to the box on this one
704 if (name.equals("0")) return;
706 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
707 if (gph != null) { gph.put(name, this, value); return; }
709 if (name.charAt(0) == '_') {
710 if (value != null && !(value instanceof JS.Callable)) {
711 if (Log.on) Log.logJS(this, "attempt to put a non function value (" + value + ") to " + name);
712 } else if (value != null && !(value instanceof JS.CompiledFunction)) {
713 if (Log.on) Log.logJS(this, "attempt to put a non-compiled function value (" + value + ") to " + name);
714 } else if (name.charAt(1) == '_') {
715 name = name.substring(2).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), true, rp);
720 name = name.substring(1).intern();
721 Trap t = Trap.getTrap(this, name);
722 if (t != null) t.delete();
723 if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), false, rp);
728 super.put(name, value);
732 // Tree Manipulation /////////////////////////////////////////////////////////////////////
734 /** The parent of this node */
735 private Box parent = null;
737 // Variables used in Vector mode */
738 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
739 private int indexInParent;
740 private Vec children = null;
742 // Variables used in linked-list mode
743 private int numKids = 0;
744 private Box nextSibling = null;
745 private Box prevSibling = null;
746 private Box firstKid = null;
748 // when we get more than 15 children, we switch to array-mode
749 private void convert_to_array() {
750 children = new Vec(numKids);
753 children.addElement(cur);
754 cur.indexInParent = children.size() - 1;
755 cur = cur.nextSibling;
756 } while (cur != firstKid);
759 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
760 public void remove() {
761 if (parent == null) {
762 if (surface != null) surface.dispose(true);
765 Box oldparent = parent;
766 if (oldparent == null) return;
767 MARK_FOR_REFLOW_this;
769 flags &= ~MOUSEINSIDE_FLAG;
771 if (parent.children != null) {
772 parent.children.removeElementAt(indexInParent);
773 for(int j=indexInParent; j<parent.children.size(); j++)
774 (parent.getChild(j)).indexInParent = j;
777 if (parent.firstKid == this) {
778 if (nextSibling == this) parent.firstKid = null;
779 else parent.firstKid = nextSibling;
782 prevSibling.nextSibling = nextSibling;
783 nextSibling.prevSibling = prevSibling;
789 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
791 // note that JavaScript box[0] will invoke put(int i), not put(String s)
792 if (oldparent != null) oldparent.put("0", this);
795 /** returns our next sibling (parent[ourindex + 1]) */
796 public final Box nextSibling() {
797 if (parent == null) return null;
798 if (parent.children == null) {
799 if (nextSibling == parent.firstKid) return null;
802 if (indexInParent >= parent.children.size() - 1) return null;
803 return (Box)parent.children.elementAt(indexInParent + 1);
807 /** returns our next sibling (parent[ourindex + 1]) */
808 public final Box prevSibling() {
809 if (parent == null) return null;
810 if (parent.children == null) {
811 if (this == parent.firstKid) return null;
814 if (indexInParent == 0) return null;
815 return (Box)parent.children.elementAt(indexInParent - 1);
819 /** Returns the parent of this node */
820 public Box getParent() { return parent; }
822 /** Returns ith child */
823 public Box getChild(int i) {
824 if (children == null) {
825 if (firstKid == null) return null;
826 if (i >= numKids) return null;
827 if (i == numKids - 1) return firstKid.prevSibling;
829 for(int j=0; j<i; j++) cur = cur.nextSibling;
832 if (i >= children.size() || i < 0) return null;
833 return (Box)children.elementAt(i);
837 /** Returns the number of children */
838 public int numChildren() {
839 if (children == null) {
840 if (firstKid == null) return 0;
842 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
845 return children.size();
849 /** Returns our index in our parent */
850 public int getIndexInParent() {
851 if (parent == null) return 0;
852 if (parent.children == null) {
854 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
857 return indexInParent;
861 /** returns the root of the surface that this box belongs to */
862 public final Box getRoot() {
863 if (parent == null && surface != null) return this;
864 if (parent == null) return null;
865 return parent.getRoot();
869 // Root Proxy ///////////////////////////////////////////////////////////////////////////////
871 // FEATURE: use xwt.graft() here
872 RootProxy myproxy = null;
873 public JS getRootProxy() {
874 if (myproxy == null) myproxy = new RootProxy(this);
878 private static class RootProxy extends JS {
880 RootProxy(Box b) { this.box = b; }
881 public Object get(Object name) { return box.get(name); }
882 public void put(Object name, Object value) { box.put(name, value, false, this); }
883 public Object[] keys() { return box.keys(); }
884 public Object callMethod(Object method, JS.Array args, boolean justChecking) {
885 return ((Box)box).callMethod(method,args,justChecking);
890 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
892 static final int min(int a, int b) { if (a<b) return a; else return b; }
893 static final double min(double a, double b) { if (a<b) return a; else return b; }
894 static final int max(int a, int b) { if (a>b) return a; else return b; }
895 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; }
896 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; }
897 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
898 final boolean inside(int x, int y) { return (!((flags & INVISIBLE_FLAG) != 0) && x >= 0 && y >= 0 && x < width && y < height); }
900 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
901 public static Box whoIs(Box cur, int x, int y) {
903 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
907 // WARNING: this method is called from the event-queueing
908 // thread -- it may run concurrently with ANY part of XWT, and
909 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
912 if ((cur.flags & INVISIBLE_FLAG) != 0) return null;
913 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
915 for(int i=cur.numChildren() - 1; i>=0; i--) {
916 Box child = cur.getChild(i);
917 if (child == null) continue; // since this method is unsynchronized, we have to double-check
920 if (!((child.flags & INVISIBLE_FLAG) != 0) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
930 * A helper class for properties of Box which require special
933 * To avoid excessive use of String.equals(), the Box.get() and
934 * Box.put() methods employ a Hash keyed on property names that
935 * require special handling. The value stored in the Hash is an
936 * instance of an anonymous subclass of SpecialBoxProperty, which knows
937 * how to handle get()s and put()s for that property name. There
938 * should be one anonymous subclass of SpecialBoxProperty for each
939 * specially-handled property on Box.
941 static class SpecialBoxProperty {
943 SpecialBoxProperty() { }
945 /** stores instances of SpecialBoxProperty; keyed on property name */
946 static Hash specialBoxProperties = new Hash(200, 3);
948 /** this method defines the behavior when the property is get()ed from b */
949 Object get(Box b) { return null; }
951 /** this method defines the behavior when the property is put() to b */
952 void put(Box b, Object value) { }
954 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
955 void put(String name, Box b, Object value) { put(b, value); }
958 //#repeat fillcolor/strokecolor
959 specialBoxProperties.put("fillcolor", new SpecialBoxProperty() {
960 public Object get(Box b) {
961 if ((b.fillcolor & 0xFF000000) == 0) return null;
962 String red = Integer.toHexString((b.fillcolor & 0x00FF0000) >> 16);
963 String green = Integer.toHexString((b.fillcolor & 0x0000FF00) >> 8);
964 String blue = Integer.toHexString(b.fillcolor & 0x000000FF);
965 if (red.length() < 2) red = "0" + red;
966 if (blue.length() < 2) blue = "0" + blue;
967 if (green.length() < 2) green = "0" + green;
968 return "#" + red + green + blue;
970 public void put(Box b, Object value) {
971 int newcolor = b.fillcolor;
972 String s = value == null ? null : value.toString();
973 if (value == null) newcolor = 0x00000000;
974 else if (s.length() > 0 && s.charAt(0) == '#')
976 newcolor = 0xFF000000 |
977 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
978 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
979 Integer.parseInt(s.substring(5, 7), 16);
980 } catch (NumberFormatException e) {
981 Log.log(this, "invalid color " + s);
984 else if (org.xwt.translators.SVG.colors.get(s) != null)
985 newcolor = 0xFF000000 | ((Integer)org.xwt.translators.SVG.colors.get(s)).intValue();
986 if (newcolor == b.fillcolor) return;
987 b.fillcolor = newcolor;
993 specialBoxProperties.put("color", new SpecialBoxProperty() {
994 public Object get(Box b) { return b.get("fillcolor"); }
995 public void put(Box b, Object value) { b.put("fillcolor", value); }
998 specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
999 public Object get(Box b) { return b.get("strokecolor"); }
1000 public void put(Box b, Object value) { b.put("strokecolor", value); }
1003 specialBoxProperties.put("text", new SpecialBoxProperty() {
1004 public Object get(Box b) { return b.text; }
1005 public void put(Box b, Object value) {
1006 String t = value == null ? "null" : value.toString();
1007 if (t.equals(b.text)) return;
1010 if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
1011 b.textwidth = b.textheight = 0;
1016 for(int i=0; i<b.text.length(); i++) {
1017 Glyph g = Glyph.getGlyph(b.font, b.fontsize, b.text.charAt(i));
1018 b.textwidth += g.advance;
1019 b.textheight = g.max_ascent + g.max_descent;
1021 } catch (Exception e) {
1028 specialBoxProperties.put("font", new SpecialBoxProperty() {
1029 public Object get(Box b) { return b.font; }
1030 public void put(Box b, Object value) {
1031 // FIXME: translate value into a resource if it is a string
1032 b.font = value == null ? null : (Res)value;
1034 b.flags |= FONT_CHANGED_FLAG;
1038 specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
1039 public Object get(Box b) { return b.font; }
1040 public void put(Box b, Object value) {
1041 if (b.fontsize == stoi(value)) return;
1042 b.fontsize = stoi(value);
1044 b.flags |= FONT_CHANGED_FLAG;
1048 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1049 public Object get(Box b) { return b; }
1050 public void put(Box b, Object value) {
1051 if (value == null) b.remove();
1052 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1053 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1057 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1058 public Object get(Box b) {
1059 Log.log(this, "warning: the orient property is deprecated");
1060 if (b.redirect == null) return "horizontal";
1061 else if (b.redirect != b) return get(b.redirect);
1062 else if (b.cols == 1) return "vertical";
1063 else if (b.rows == 1) return "horizontal";
1066 public void put(Box b, Object value) {
1067 Log.log(this, "warning: the orient property is deprecated");
1068 if (value == null) return;
1069 if (b.redirect == null) return;
1070 if (b.redirect != b) { put(b.redirect, value); return; }
1071 if (value.equals("vertical")) {
1072 if (b.rows == 0) return;
1073 b.rows = 0; b.cols = 1;
1074 } else if (value.equals("horizontal")) {
1075 if (b.cols == 0) return;
1076 b.cols = 0; b.rows = 1;
1078 Log.log(this, "invalid value put to orient property: " + value);
1082 specialBoxProperties.put("static", new SpecialBoxProperty() {
1083 public Object get(Box b) {
1085 JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
1086 for(int i=0; i<cfsn.length() - 1; i++)
1087 if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
1088 cfsn = cfsn.substring(0, i);
1091 return Static.getStatic(cfsn);
1095 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1096 public Object get(Box b) { return (((b.flags & HSHRINK_FLAG) != 0) || ((b.flags & VSHRINK_FLAG) != 0)) ? Boolean.TRUE : Boolean.FALSE; }
1097 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1100 //#repeat hshrink/vshrink HSHRINK_FLAG/VSHRINK_FLAG
1101 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1102 public Object get(Box b) { return new Boolean((b.flags & HSHRINK_FLAG) != 0); }
1103 public void put(Box b, Object value) {
1104 boolean newshrink = stob(value);
1105 if (((b.flags & HSHRINK_FLAG) != 0) == newshrink) return;
1106 if (newshrink) b.flags |= HSHRINK_FLAG; else b.flags &= ~HSHRINK_FLAG;
1113 specialBoxProperties.put("x", new SpecialBoxProperty() {
1114 public Object get(Box b) {
1115 if (b.surface == null) return new Integer(0);
1116 if ((b.flags & INVISIBLE_FLAG) != 0) return new Integer(0);
1117 return new Integer(b.x);
1119 public void put(Box b, Object value) {
1120 if (!((b.flags & ABSOLUTE_FLAG) != 0)) return;
1121 int x = stoi(value);
1122 if (x == b.x) return;
1125 if (b.parent == null && b.surface != null) {
1126 b.surface.setLocation();
1127 b.surface.centerSurfaceOnRender = false;
1135 //#repeat width/height minwidth/minheight maxwidth/maxheight
1136 specialBoxProperties.put("width", new SpecialBoxProperty() {
1137 public Object get(Box b) { return new Integer(b.width); }
1138 public void put(Box b, Object value) {
1139 b.width = stoi(value);
1140 if (b.parent == null && b.surface != null) {
1141 b.surface.setSize();
1144 if (b.minwidth == b.width && b.maxwidth == b.width) return;
1145 b.minwidth = b.maxwidth = b.width;
1151 //#repeat cols/rows rows/cols
1152 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1153 public Object get(Box b) { return new Double(b.cols); }
1154 public void put(Box b, Object value) {
1155 if (b.cols == stoi(value)) return;
1156 b.cols = stoi(value);
1157 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1158 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1163 //#repeat colspan/rowspan
1164 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1165 public Object get(Box b) { return new Double(b.colspan); }
1166 public void put(Box b, Object value) {
1167 if (b.colspan == stoi(value)) return;
1168 b.colspan = stoi(value);
1174 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1175 public Object get(Box b) { return ((b.flags & TILE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1176 public void put(Box b, Object value) {
1177 if (((b.flags & TILE_FLAG) != 0) == stob(value)) return;
1178 if (stob(value)) b.flags |= TILE_FLAG; else b.flags &= ~TILE_FLAG;
1182 specialBoxProperties.put("invisible", new SpecialBoxProperty() {
1183 public Object get(Box b) {
1184 for (Box cur = b; cur != null; cur = cur.parent) {
1185 if ((cur.flags & INVISIBLE_FLAG) != 0) return Boolean.TRUE; }
1186 return Boolean.FALSE;
1188 public void put(Box b, Object value) {
1189 if (stob(value) == ((b.flags & INVISIBLE_FLAG) != 0)) return;
1190 if (stob(value)) b.flags |= INVISIBLE_FLAG; else b.flags &= ~INVISIBLE_FLAG;
1191 if (b.parent == null) {
1192 if (b.surface != null) b.surface.setInvisible((b.flags & INVISIBLE_FLAG) != 0);
1195 MARK_FOR_REFLOW_b_parent;
1196 b.parent.dirty(b.x, b.y, b.width, b.height);
1200 specialBoxProperties.put("absolute", new SpecialBoxProperty() {
1201 public Object get(Box b) { return ((b.flags & ABSOLUTE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1202 public void put(Box b, Object value) {
1203 if (stob(value) == ((b.flags & ABSOLUTE_FLAG) != 0)) return;
1204 if (stob(value)) b.flags |= ABSOLUTE_FLAG; else b.flags &= ~ABSOLUTE_FLAG;
1205 if ((b.flags & ABSOLUTE_FLAG) != 0) { b.x = 0; b.y = 0; }
1206 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1209 specialBoxProperties.put("image", new SpecialBoxProperty() {
1210 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1211 public void put(Box b, Object value) {
1212 if ((value == null && b.image == null) ||
1213 (value != null && b.image != null && value.equals(ImageDecoder.imageToNameMap.get(b.image)))) return;
1214 String s = value == null ? null : value.toString();
1215 if (s == null || s.equals("")) b.image = null;
1217 if ((b.image = ImageDecoder.getPicture(s)) == null) {
1218 if (Log.on) Log.logJS(Box.class, "unable to load image " + s);
1220 b.minwidth = b.image.getWidth();
1221 b.minheight = b.image.getHeight();
1229 //#repeat globalx/globaly x/y
1230 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1231 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1232 public void put(Box b, Object value) {
1233 if (b.surface == null || b.parent == null) return;
1234 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1240 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1241 public Object get(Box b) { return b.cursor; }
1242 public void put(Box b, Object value) {
1243 b.cursor = (String)value;
1244 if (b.surface == null) return;
1246 // see if we need to update the surface cursor
1247 Surface surface = b.getRoot().surface;
1248 String tempcursor = surface.cursor;
1249 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1250 if (surface.cursor != tempcursor) surface.syncCursor();
1254 //#repeat mousex/mousey x/y
1255 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1256 public Object get(Box b) {
1257 Surface surface = b.getRoot().surface;
1258 if (surface == null) return new Integer(0);
1259 int mousex = surface.mousex;
1260 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1261 return new Integer(mousex);
1266 specialBoxProperties.put("xwt", new SpecialBoxProperty() {
1267 public Object get(Box b) { return XWT.singleton; }
1270 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1271 public Object get(Box b) {
1272 return ((b.flags & MOUSEINSIDE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1275 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1276 public Object get(Box b) {
1277 if (b.redirect == null) return new Integer(0);
1278 if (b.redirect != b) return get(b.redirect);
1279 return new Integer(b.numChildren());
1282 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1283 public void put(String name, Box b, Object value) {
1284 Surface surface = b.getRoot().surface;
1285 if (surface == null) return;
1286 int mousex = surface.mousex;
1287 int mousey = surface.mousey;
1288 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1292 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1293 if (c.inside(mousex - c.x, mousey - c.y)) {
1298 if (b.parent != null) b.parent.put(name, value);
1301 specialBoxProperties.put("Press1", mouseEventHandler);
1302 specialBoxProperties.put("Press2", mouseEventHandler);
1303 specialBoxProperties.put("Press3", mouseEventHandler);
1304 specialBoxProperties.put("Release1", mouseEventHandler);
1305 specialBoxProperties.put("Release2", mouseEventHandler);
1306 specialBoxProperties.put("Release3", mouseEventHandler);
1307 specialBoxProperties.put("Click1", mouseEventHandler);
1308 specialBoxProperties.put("Click2", mouseEventHandler);
1309 specialBoxProperties.put("Click3", mouseEventHandler);
1310 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1311 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1312 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1314 specialBoxProperties.put("root", new SpecialBoxProperty() {
1315 public Object get(Box b) {
1316 if (b.getRoot() == null) return null;
1317 else if (b.parent == null) return b;
1318 else return b.getRoot().getRootProxy();
1321 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1322 public Object get(Box b) {
1323 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1326 public void put(Box b, Object value) {
1327 if (b.surface == null) return;
1328 boolean val = stob(value);
1329 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1333 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1334 public Object get(Box b) {
1335 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1338 public void put(Box b, Object value) {
1339 if (b.surface == null) return;
1340 boolean val = stob(value);
1341 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1345 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1346 public void put(Box b, Object value) {
1347 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1351 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1352 public void put(Box b, Object value) {
1353 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1357 //#repeat hscar/vscar
1358 specialBoxProperties.put("hscar", new SpecialBoxProperty() {
1359 public void put(Box b, Object value) {
1360 if (b.parent == null && b.surface != null) {
1361 b.surface.hscar = stoi(value);
1362 b.surface.dirty(0, 0, b.width, b.height);
1363 b.surface.Refresh();
1369 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1370 public void put(Box b, Object value) {
1371 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1375 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1376 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
1377 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
1378 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1379 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1382 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
1383 public Object get(Box b) {
1384 if (b.redirect == null) return new Integer(0);
1385 if (b.redirect != b) return get(b.redirect);
1386 return new Integer(b.hpad);
1388 public void put(Box b, Object value) {
1389 if (b.redirect == null) return;
1390 if (b.redirect != b) { put(b.redirect, value); return; }
1391 int newval = stoi(value);
1392 if (newval == b.hpad) return;
1399 //#repeat minwidth/minheight maxwidth/maxheight
1400 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1401 public Object get(Box b) { return new Integer(b.minwidth); }
1402 public void put(Box b, Object value) {
1403 if (stoi(value) == b.minwidth) return;
1404 b.minwidth = stoi(value);
1408 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1409 public Object get(Box b) { return new Integer(b.maxwidth); }
1410 public void put(Box b, Object value) {
1411 if (stoi(value) == b.maxwidth) return;
1412 b.maxwidth = stoi(value);
1418 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1419 public void put(Box b, Object value) { }
1420 public Object get(Box b) {
1421 if (b.redirect == null) return null;
1422 if (b.redirect == b) return Boolean.TRUE;
1423 return get(b.redirect);
1427 // FEATURE: this still isn't totally harmonious; when you createSurface, these aren't checked
1428 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1429 public void put(Box b, Object value) {
1430 if (b.surface != null) b.surface.setTitleBarText(value.toString());
1431 b.put_("titlebar", value);
1433 public Object get(Box b) { return b.get_("titlebar"); }
1436 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1437 public void put(Box b, Object value) {
1438 b.put_("icon", value);
1439 if (b.surface == null) return;
1440 Picture pic = ImageDecoder.getPicture(value.toString());
1441 if (pic != null) b.surface.setIcon(pic);
1442 else if (Log.on) Log.log(this, "unable to load icon " + value);
1444 public Object get(Box b) { return b.get_("icon"); }
1449 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1450 public static boolean stob(Object o) {
1451 if (o == null) return false;
1452 return Boolean.TRUE.equals(o) || "true".equals(o);
1455 /** helper that converts a String to an int according to JavaScript coercion rules */
1456 public static int stoi(Object o) {
1457 if (o == null) return 0;
1458 if (o instanceof Integer) return ((Integer)o).intValue();
1461 if (!(o instanceof String)) s = o.toString();
1464 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1465 catch (NumberFormatException e) { return 0; }