1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
4 // **** This file must be preprocessed before compilation ****
6 // RULE: coordinates on non-static methods are ALWAYS relative to the
7 // upper-left hand corner of <tt>this</tt>
9 // FEATURE: reflow before allowing js to read from width/height
10 // FEATURE: fastpath for rows=1/cols=1
11 // FEATURE: reflow starting with a certain child
12 // FEATURE: separate mark_for_reflow and mark_for_resize
18 import org.xwt.util.*;
19 import org.xwt.translators.*;
23 * Encapsulates the data for a single XWT box as well as all layout
28 * This is the real meat of XWT. Part of its monolithic design is for
29 * performance reasons: deep inheritance heirarchies are slow, and
30 * neither javago nor GCJ can inline across class boundaries.
33 * <p>The rendering process consists of three phases; each requires
34 * one DFS pass over the tree</p>
36 * <ol><li> <b>repacking</b>: children of a box are packed into columns
37 * and rows according to their colspan/rowspan attributes and
38 * ordering. Minimum and maximum sizes of columns are computed.
40 * <li> <b>resizing</b>: width/height and x/y positions of children
41 * are assigned. If a PosChange or SizeChange is triggered,
42 * <tt>Surface.abort</tt> will be set and the resizing process will
45 * <li> <b>repainting</b>: children draw their content onto the
49 * The first two passes together are called the <i>reflow</i> phase.
51 * Reflowing is done in a seperate pass since PosChanges and
52 * SizeChanges trigger an Surface.abort; if rendering were done in the same
53 * pass, rendering work done prior to the Surface.abort would be wasted.
55 * The two passes are repack and resize. Together they are known as
56 * reflow. Repacking assigns boxes to the appropriate grid
57 * coordinates within the parents and computes
58 * contentwidth/contentheight. Resize computes actual size and
61 * A note on coordinates: the Box class represents regions
62 * internally as x,y,w,h tuples, even though the PixelBuffer class
63 * uses x1,y1,x2,y2 tuples.
65 public final class Box extends JS.Scope {
67 public Box() { super(null); }
70 // Misc instance data ////////////////////////////////////////////////////////////////
72 static int sizePosChangesSinceLastRender = 0;
75 // Misc instance data ////////////////////////////////////////////////////////////////
77 boolean needs_reflow = true;
78 //#define MARK_FOR_REFLOW_this for(Box b2 = this; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
79 //#define MARK_FOR_REFLOW_b for(Box b2 = b; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
80 //#define MARK_FOR_REFLOW_b_parent for(Box b2 = b.parent; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
83 Surface surface = null; // null on all non-root boxen
85 // Flags ///////////////////////////////////////////////////////////////////////////////
87 static int MOUSEINSIDE_FLAG = 0x00000001;
88 static int INVISIBLE_FLAG = 0x00000002;
89 static int NOTPACKED_FLAG = 0x00000004;
90 static int HSHRINK_FLAG = 0x00000008;
91 static int VSHRINK_FLAG = 0x00000010;
92 static int TILE_FLAG = 0x00000020;
93 static int FONT_CHANGED_FLAG = 0x00000040; // set when font changes, cleared during repack
94 static int ISROOT_FLAG = 0x00000080;
95 static int NOCLIP_FLAG = 0x00000100;
98 // Geometry ////////////////////////////////////////////////////////////////////////////
100 // xwt can be compiled with 16-bit lengths to save memory on small devices
102 //#define MAX_LENGTH Integer.MAX_VALUE
103 //#define MIN_LENGTH Integer.MIN_VALUE
105 // always correct (set directly by user)
106 public LENGTH minwidth = 0;
107 public LENGTH minheight = 0;
108 public LENGTH maxwidth = MAX_LENGTH;
109 public LENGTH maxheight = MAX_LENGTH;
110 private LENGTH hpad = 0;
111 private LENGTH vpad = 0;
112 private String text = null;
113 private Res font = null;
114 private int fontsize = 10;
115 private LENGTH textwidth = 0;
116 private LENGTH textheight = 0;
118 // FEATURE: use shorts
119 private int rows = 1;
120 private int cols = 0;
121 private int rowspan = 1;
122 private int colspan = 1;
124 // computed during reflow
127 public LENGTH width = 0;
128 public LENGTH height = 0;
129 private int row = 0; // FEATURE use a short
130 private int col = 0; // FEATURE use a short
131 private LENGTH contentwidth = 0; // == max(minwidth, textwidth+pad, sum(child.contentwidth) + pad)
132 private LENGTH contentheight = 0;
133 private LENGTH offset_x = 0;
134 private LENGTH offset_y = 0;
137 // Rendering Properties ///////////////////////////////////////////////////////////
139 private VectorGraphics.VectorPath path = null;
140 private VectorGraphics.Affine transform = null;
142 private VectorGraphics.RasterPath rpath = null;
143 private VectorGraphics.Affine rtransform = null;
145 //private VectorGraphics.Paint fill = null;
146 //private VectorGraphics.Paint stroke = null;
149 public Picture image; // will disappear
150 private int fillcolor = 0x00000000; // will become VectorGraphics.Paint
151 private int strokecolor = 0xFF000000; // will become VectorGraphics.Paint
152 private int textcolor = 0xFF000000; // will become VectorGraphics.Paint
154 private String cursor = null; // the cursor for this box
157 // Instance Methods /////////////////////////////////////////////////////////////////////
159 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
160 public final void dirty() {
161 if ((flags & NOCLIP_FLAG) != 0 && parent != null) parent.dirty();
162 else dirty(0, 0, width, height);
164 public final void dirty(int x, int y, int w, int h) {
165 for(Box cur = this; cur != null; cur = cur.parent) {
166 if ((flags & NOCLIP_FLAG) == 0) {
167 w = min(x + w, cur.width) - max(x, 0);
168 h = min(y + h, cur.height) - max(y, 0);
172 if (w <= 0 || h <= 0) return;
173 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
180 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
181 * to see if this node requires any Enter, Leave, or Move notifications.
183 * @param forceleave set to true by the box's parent if the mouse is inside an older
184 * sibling, which is covering up this box.
186 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
187 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
189 boolean wasinside = (flags & MOUSEINSIDE_FLAG) != 0;
190 boolean isinside = !((flags & INVISIBLE_FLAG) != 0) && inside(mousex, mousey) && !forceleave;
191 if (isinside) flags |= MOUSEINSIDE_FLAG; else flags &= ~MOUSEINSIDE_FLAG;
193 if (!wasinside && !isinside) return;
195 if (!wasinside && isinside && get("Enter", Trap.class) != null) put("Enter", Boolean.TRUE);
196 else if (wasinside && !isinside && get("Leave", Trap.class) != null) put("Leave", Boolean.TRUE);
197 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && get("Move", Trap.class) != null)
198 put("Move", Boolean.TRUE);
200 if (isinside && cursor != null) getRoot().cursor = cursor;
202 // if the mouse has moved into our padding region, it is considered 'outside' all our children
203 if (!(mousex >= hpad && mousey >= vpad && mousex < width - hpad && mousey < height + vpad)) forceleave = true;
205 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
206 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
207 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
212 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
214 void reflow() { reflow(width, height); }
215 void reflow(int new_width, int new_height) {
217 if (Surface.abort) return;
218 resize(x, y, new_width, new_height);
221 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
223 if (!needs_reflow) return;
224 contentwidth = contentheight = 0;
226 // --- Phase 0 ----------------------------------------------------------------------
228 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
229 if (((flags & FONT_CHANGED_FLAG) != 0) && child.font == null) {
230 child.flags |= FONT_CHANGED_FLAG;
231 child.needs_reflow = true;
234 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
236 flags &= ~FONT_CHANGED_FLAG;
238 // --- Phase 1 ----------------------------------------------------------------------
239 // assign children to their row/column positions (assuming constrained columns)
240 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
241 //#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
242 if (rows == 0 && numChildren() != 0) {
243 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
244 Box child = getChild(0);
245 for(; child != null && (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
246 OUTER: for(int row=0; child != null; row++) {
247 for(int col=0; child != null && col < cols;) {
248 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
249 while(col < cols && numRowsInCol[col] > row) col++;
250 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
251 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
254 if (col + min(cols, child.colspan) > cols) break;
255 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
258 col += min(cols, child.colspan);
259 child = child.nextSibling();
260 for(; child != null && (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
266 // --- Phase 2 ----------------------------------------------------------------------
267 // compute the min/max sizes of the columns and rows and set our contentwidth
268 if (numChildren() != 0) {
269 //#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
270 contentwidth = 2 * hpad;
273 for(Box child = getChild(0); child != null; child = child.nextSibling())
274 numCols = max(numCols, child.col + child.colspan);
275 LENGTH[] colWidth = new LENGTH[numCols];
276 for(Box child = getChild(0); child != null; child = child.nextSibling())
277 if (!(((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
278 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
279 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
280 contentwidth = max(textwidth + 2 * hpad, contentwidth);
281 contentwidth = bound(minwidth, contentwidth, maxwidth);
286 if ((flags & NOTPACKED_FLAG) == 0) {
288 if (rpath == null) rpath = path.realize(transform == null ? VectorGraphics.Affine.identity() : transform);
289 if ((flags & HSHRINK_FLAG) != 0) contentwidth = max(contentwidth, rpath.boundingBoxWidth());
290 if ((flags & VSHRINK_FLAG) != 0) contentheight = max(contentheight, rpath.boundingBoxHeight());
291 // FIXME: separate offset_x needed for the path
293 //#repeat x1/y1 x2/y2 x3/y3 x4/y4 contentwidth/contentheight left/top right/bottom
294 int x1 = transform == null ? 0 : (int)transform.multiply_px(0, 0);
295 int x2 = transform == null ? 0 : (int)transform.multiply_px(contentwidth, 0);
296 int x3 = transform == null ? contentwidth : (int)transform.multiply_px(contentwidth, contentheight);
297 int x4 = transform == null ? contentwidth : (int)transform.multiply_px(0, contentheight);
298 int left = min(min(x1, x2), min(x3, x4));
299 int right = max(max(x1, x2), max(x3, x4));
300 contentwidth = max(contentwidth, right - left);
301 offset_x = -1 * left;
307 private void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
309 // --- Phase 1 ----------------------------------------------------------------------
310 // run PosChange/SizeChange, dirty as needed
311 if (x != this.x || y != this.y || width != this.width || height != this.height) {
312 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
313 boolean sizechange = false, poschange = false;
314 if ((this.width != width || this.height != height) && get("SizeChange", Trap.class) != null) sizechange = true;
315 if ((this.x != x || this.y != y) && get("PosChange", Trap.class) != null) poschange = true;
316 this.width = width; this.height = height; this.x = x; this.y = y;
318 if (sizechange || poschange)
319 if (sizePosChangesSinceLastRender == 500) {
320 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
322 sizePosChangesSinceLastRender++;
323 try { if (sizechange) put("SizeChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
324 try { if (poschange) put("PosChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
325 Surface.abort = true;
331 // --- short circuit ----------------------------------------------------------------
332 if (!needs_reflow) return;
333 needs_reflow = false;
334 if (numChildren() == 0) return;
336 // --- Phase 2 ----------------------------------------------------------------------
337 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
339 //#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
342 for(Box child = getChild(0); child != null; child = child.nextSibling())
343 numCols = max(numCols, child.col + child.colspan);
344 LENGTH[] colWidth = new LENGTH[numCols];
345 LENGTH[] colMaxWidth = new LENGTH[numCols];
346 int marginWidth = width;
347 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
350 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
351 if (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)) continue;
352 //#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
353 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
354 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
355 colMaxWidth[i] = max(colMaxWidth[i], (((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) / child.colspan);
359 //#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
360 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
362 for(int i=0; i<colMaxWidth.length; i++) {
363 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
364 marginWidth -= colMaxWidth[i];
365 if (marginWidth < 0) { marginWidth = 0; break; }
370 // --- Phase 3 ----------------------------------------------------------------------
371 // hand out the slack
373 //#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
374 slack = width - 2 * hpad;
375 for(int i=0; i<numCols; i++) slack -= colWidth[i];
376 if (numChildren() > 0)
378 // FEATURE: inefficient
379 int startslack = slack;
380 int increment = max(1, slack / numCols);
381 for(int col=0; col < numCols && slack > 0; col++) {
382 slack += colWidth[col];
383 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
384 slack -= colWidth[col];
386 if (slack == startslack) break;
391 // --- Phase 4 ----------------------------------------------------------------------
392 // assign children's new sizes and positions and recurse
393 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
394 if ((child.flags & INVISIBLE_FLAG) != 0) continue;
395 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
396 if ((child.flags & NOTPACKED_FLAG) != 0) {
399 child_width = ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : min(child.maxwidth, width - child.x - hpad);
400 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y - vpad);
403 //#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
404 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
405 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
406 if (transform == null) child_x = max(hpad, marginWidth / 2);
407 for(int i=0; i<child.col; i++) child_x += colWidth[i];
408 if (diff < 0) child_x += -1 * (diff / 2);
412 child.resize(child_x, child_y, child_width, child_height);
419 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
421 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
422 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf, VectorGraphics.Affine a) {
423 if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
424 int globalx = parentx + (parent == null ? 0 : x);
425 int globaly = parenty + (parent == null ? 0 : y);
427 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
428 if ((flags & NOCLIP_FLAG) == 0) {
429 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw, (parent == null ? 0 : globalx) + width) - globalx;
430 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph, (parent == null ? 0 : globaly) + height) - globaly;
431 clipx = max(clipx, parent == null ? 0 : globalx);
432 clipy = max(clipy, parent == null ? 0 : globaly);
434 if (clipw <= 0 || cliph <= 0) return;
436 if (path == null && (fillcolor & 0xFF000000) != 0x00000000) {
437 VectorGraphics.VectorPath path2 = VectorGraphics.parseVectorPath("M0,0H" + width + "V" + height + "H0V0z");
438 path2.realize(a).fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
442 if ((flags & TILE_FLAG) != 0) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
443 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
445 if (text != null && !text.equals(""))
446 renderText(globalx, globaly, clipx, clipy, clipw, cliph, buf);
449 if (rtransform == null) rpath = null;
450 else if (!rtransform.equalsIgnoringTranslation(a)) rpath = null;
452 rpath.translate((int)(a.e - rtransform.e), (int)(a.f - rtransform.f));
453 rtransform = a.copy();
457 rpath = path.realize(a == null ? VectorGraphics.Affine.identity() : a);
459 if ((strokecolor & 0xff000000) != 0) rpath.stroke(buf, 1, strokecolor);
460 if ((fillcolor & 0xff000000) != 0) rpath.fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
463 // now subtract the pad region from the clip region before proceeding
464 if ((flags & NOCLIP_FLAG) == 0) {
465 clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
466 cliph = min(max(clipy, globaly + vpad) + cliph, globaly + height - vpad) - clipy;
467 clipx = max(clipx, globalx + hpad);
468 clipy = max(clipy, globaly + vpad);
471 for(Box b = getChild(0); b != null; b = b.nextSibling()) {
472 VectorGraphics.Affine a2 = VectorGraphics.Affine.translate(b.x, b.y);
473 if (transform != null) a2.multiply(transform);
474 a2.multiply(VectorGraphics.Affine.translate(offset_x, offset_y));
476 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf, a2);
480 void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
482 buf.drawPicture(image, clipx, clipy, clipx + clipw, clipy + cliph, 0, 0, image.getWidth(), image.getHeight());
485 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, PixelBuffer buf) {
486 int iw = image.getWidth();
487 int ih = image.getHeight();
488 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
489 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
491 int dx1 = max(i * iw + x, x);
492 int dy1 = max(j * ih + y, y);
493 int dx2 = min((i+1) * iw + x, x + w);
494 int dy2 = min((j+1) * ih + y, y + h);
496 int sx1 = dx1 - (i*iw) - x;
497 int sy1 = dy1 - (j*ih) - y;
498 int sx2 = dx2 - (i*iw) - x;
499 int sy2 = dy2 - (j*ih) - y;
501 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
502 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
507 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
508 for(int i=0; i<text.length(); i++) {
510 char c = text.charAt(i);
511 Glyph g = Glyph.getGlyph(font, fontsize, c);
512 buf.drawPictureAlphaOnly(g.p,
514 y + vpad + g.max_ascent - g.baseline,
515 x + hpad + g.p.getWidth(),
516 y + vpad + g.max_ascent - g.baseline + g.p.getHeight(),
518 g.p.getWidth(), g.p.getHeight(),
525 // Methods to implement org.xwt.js.JS //////////////////////////////////////
527 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
528 if ("indexof".equals(method)) {
529 if (checkOnly) return Boolean.TRUE;
530 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
531 Box b = (Box)args.elementAt(0);
532 if (b.parent != Box.this) {
533 if (redirect == null || redirect == Box.this) return new Integer(-1);
534 return redirect.callMethod(method, args, checkOnly);
536 return new Integer(b.getIndexInParent());
541 /** Returns the i_th child */
542 public Object get(int i) {
543 if (redirect == null) return null;
544 if (redirect != this) return redirect.get(i);
545 return i >= numChildren() || i < 0 ? null : getChild(i);
549 * Inserts value as child i; calls remove() if necessary.
550 * This method handles "reinserting" one of your children properly.
551 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
552 * WARNING: O(n) runtime, unless i == numChildren()
554 public void put(int i, Object value) {
557 if (value != null && !(value instanceof Box)) {
558 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to anything other than a box");
559 } else if (redirect == null) {
560 if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
561 put("0", (Box)value);
562 } else if (redirect != this) {
563 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
564 redirect.put(i, value);
566 } else if (value == null) {
567 if (i >= 0 && i < numChildren()) {
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 key, Object key2) { return super.get(key, key2); }
639 public void put(Object key, Object key2, Object val) { super.put(key, key2, val); }
641 public Object get_(Object name) { return super.get(name); }
642 public Object get(Object name) { return get(name, false); }
643 public Object get(Object name_, boolean ignoretraps) {
644 if (name_ instanceof Number) return get(((Number)name_).intValue());
646 if (!(name_ instanceof String)) return null;
647 String name = (String)name_;
648 if (name.equals("")) return null;
650 // See if we're triggering a trap
651 Trap t = ignoretraps ? (Trap)null : (Trap)get(name, Trap.class);
652 if (t != null) return t.perform();
654 // Check for a special handler
655 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
656 if (gph != null) return gph.get(this);
658 Object ret = super.get(name);
659 if (name.startsWith("$") && ret == null)
660 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" +
661 name.substring(1) + "\" found");
665 public Object[] keys() {
666 Object[] ret = new Object[numChildren()];
667 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
671 public void addTrap(Object name, Object value) { Trap.addTrap(this, name, ((JS.CompiledFunction)value)); }
672 public void delTrap(Object name, Object value) { Trap.delTrap(this, name, ((JS.CompiledFunction)value)); }
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); }
682 public void put(Object name_, Object value, boolean ignoretraps) {
683 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
684 if (!(name_ instanceof String)) { super.put(name_,value); return; }
685 String name = name_.toString();
687 Trap t = (Trap)get(name, Trap.class);
694 // don't want to really cascade down to the box on this one
695 if (name.equals("0")) return;
697 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
698 if (gph != null) { gph.put(name, this, value); return; }
700 super.put(name, value);
704 // Tree Manipulation /////////////////////////////////////////////////////////////////////
706 /** The parent of this node */
707 private Box parent = null;
709 // Variables used in Vector mode */
710 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
711 private int indexInParent;
712 private Vec children = null;
714 // Variables used in linked-list mode
715 private int numKids = 0;
716 private Box nextSibling = null;
717 private Box prevSibling = null;
718 private Box firstKid = null;
720 // when we get more than 15 children, we switch to array-mode
721 private void convert_to_array() {
722 children = new Vec(numKids);
725 children.addElement(cur);
726 cur.indexInParent = children.size() - 1;
727 cur = cur.nextSibling;
728 } while (cur != firstKid);
731 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
732 public void remove() {
733 if (parent == null) {
734 if (surface != null) surface.dispose(true);
737 Box oldparent = parent;
738 if (oldparent == null) return;
739 MARK_FOR_REFLOW_this;
741 flags &= ~MOUSEINSIDE_FLAG;
743 if (parent.children != null) {
744 parent.children.removeElementAt(indexInParent);
745 for(int j=indexInParent; j<parent.children.size(); j++)
746 (parent.getChild(j)).indexInParent = j;
749 if (parent.firstKid == this) {
750 if (nextSibling == this) parent.firstKid = null;
751 else parent.firstKid = nextSibling;
754 prevSibling.nextSibling = nextSibling;
755 nextSibling.prevSibling = prevSibling;
761 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
763 // note that JavaScript box[0] will invoke put(int i), not put(String s)
764 if (oldparent != null) oldparent.put("0", this);
767 /** returns our next sibling (parent[ourindex + 1]) */
768 public final Box nextSibling() {
769 if (parent == null) return null;
770 if (parent.children == null) {
771 if (nextSibling == parent.firstKid) return null;
774 if (indexInParent >= parent.children.size() - 1) return null;
775 return (Box)parent.children.elementAt(indexInParent + 1);
779 /** returns our next sibling (parent[ourindex + 1]) */
780 public final Box prevSibling() {
781 if (parent == null) return null;
782 if (parent.children == null) {
783 if (this == parent.firstKid) return null;
786 if (indexInParent == 0) return null;
787 return (Box)parent.children.elementAt(indexInParent - 1);
791 /** Returns the parent of this node */
792 public Box getParent() { return parent; }
794 /** Returns ith child */
795 public Box getChild(int i) {
796 if (children == null) {
797 if (firstKid == null) return null;
798 if (i >= numKids) return null;
799 if (i == numKids - 1) return firstKid.prevSibling;
801 for(int j=0; j<i; j++) cur = cur.nextSibling;
804 if (i >= children.size() || i < 0) return null;
805 return (Box)children.elementAt(i);
809 /** Returns the number of children */
810 public int numChildren() {
811 if (children == null) {
812 if (firstKid == null) return 0;
814 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
817 return children.size();
821 /** Returns our index in our parent */
822 public int getIndexInParent() {
823 if (parent == null) return 0;
824 if (parent.children == null) {
826 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
829 return indexInParent;
833 /** returns the root of the surface that this box belongs to */
834 public final Box getRoot() {
835 if (parent == null && surface != null) return this;
836 if (parent == null) return null;
837 return parent.getRoot();
841 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
843 static final int min(int a, int b) { if (a<b) return a; else return b; }
844 static final float min(float a, float b) { if (a<b) return a; else return b; }
845 static final double min(double a, double b) { if (a<b) return a; else return b; }
846 static final int max(int a, int b) { if (a>b) return a; else return b; }
847 static final float max(float a, float b) { if (a>b) return a; else return b; }
848 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; }
849 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; }
850 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
851 final boolean inside(int x, int y) { return (!((flags & INVISIBLE_FLAG) != 0) && x >= 0 && y >= 0 && x < width && y < height); }
853 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
854 public static Box whoIs(Box cur, int x, int y) {
856 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
860 // WARNING: this method is called from the event-queueing
861 // thread -- it may run concurrently with ANY part of XWT, and
862 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
865 if ((cur.flags & INVISIBLE_FLAG) != 0) return null;
866 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
868 for(int i=cur.numChildren() - 1; i>=0; i--) {
869 Box child = cur.getChild(i);
870 if (child == null) continue; // since this method is unsynchronized, we have to double-check
873 if (!((child.flags & INVISIBLE_FLAG) != 0) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
883 * A helper class for properties of Box which require special
886 * To avoid excessive use of String.equals(), the Box.get() and
887 * Box.put() methods employ a Hash keyed on property names that
888 * require special handling. The value stored in the Hash is an
889 * instance of an anonymous subclass of SpecialBoxProperty, which knows
890 * how to handle get()s and put()s for that property name. There
891 * should be one anonymous subclass of SpecialBoxProperty for each
892 * specially-handled property on Box.
894 static class SpecialBoxProperty {
896 SpecialBoxProperty() { }
898 /** stores instances of SpecialBoxProperty; keyed on property name */
899 static Hash specialBoxProperties = new Hash(200, 3);
901 /** this method defines the behavior when the property is get()ed from b */
902 Object get(Box b) { return null; }
904 /** this method defines the behavior when the property is put() to b */
905 void put(Box b, Object value) { }
907 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
908 void put(String name, Box b, Object value) { put(b, value); }
911 specialBoxProperties.put("fill", new ColorBoxProperty() {
912 public int getColor(Box b) { return b.fillcolor; }
913 public void putColor(Box b, int argb) { b.fillcolor = argb; }
915 specialBoxProperties.put("textcolor", new ColorBoxProperty() {
916 public int getColor(Box b) { return b.textcolor; }
917 public void putColor(Box b, int argb) { b.textcolor = argb; }
919 specialBoxProperties.put("strokecolor", new ColorBoxProperty() {
920 public int getColor(Box b) { return b.strokecolor; }
921 public void putColor(Box b, int argb) { b.strokecolor = argb; }
924 specialBoxProperties.put("text", new SpecialBoxProperty() {
925 public Object get(Box b) { return b.text; }
926 public void put(Box b, Object value) {
927 String t = value == null ? "null" : value.toString();
928 if (t.equals(b.text)) return;
931 if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
932 b.textwidth = b.textheight = 0;
937 for(int i=0; i<b.text.length(); i++) {
938 Glyph g = Glyph.getGlyph(b.font, b.fontsize, b.text.charAt(i));
939 b.textwidth += g.advance;
940 b.textheight = g.max_ascent + g.max_descent;
942 } catch (Exception e) {
949 specialBoxProperties.put("path", new SpecialBoxProperty() {
950 public Object get(Box b) { throw new JS.Exn("cannot read from the path property"); }
951 public void put(Box b, Object value) {
952 String t = value == null ? "null" : value.toString();
953 b.path = value == null ? null : VectorGraphics.parseVectorPath(value.toString());
958 specialBoxProperties.put("transform", new SpecialBoxProperty() {
959 public void put(Box b, Object value) {
960 String t = value.toString().trim();
961 b.transform = VectorGraphics.Affine.identity();
962 while (t.length() > 0) {
963 if (t.startsWith("skewX(")) {
966 } else if (t.startsWith("shear(")) {
967 // FIXME: nonstandard; remove this
968 b.transform.multiply(VectorGraphics.Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
970 } else if (t.startsWith("skewY(")) {
973 } else if (t.startsWith("rotate(")) {
974 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
975 if (sub.indexOf(',') != -1) {
976 float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
977 sub = sub.substring(sub.indexOf(',') + 1);
978 float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
979 sub = sub.substring(sub.indexOf(',') + 1);
980 float cy = Float.parseFloat(sub);
981 b.transform.multiply(VectorGraphics.Affine.translate(cx, cy));
982 b.transform.multiply(VectorGraphics.Affine.rotate(angle));
983 b.transform.multiply(VectorGraphics.Affine.translate(-1 * cx, -1 * cy));
985 b.transform.multiply(VectorGraphics.Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
988 } else if (t.startsWith("translate(")) {
989 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
990 if (sub.indexOf(',') > -1) {
991 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
992 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
994 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
997 } else if (t.startsWith("flip(")) {
998 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
999 b.transform.multiply(VectorGraphics.Affine.flip(which.equals("horizontal"), which.equals("vertical")));
1001 } else if (t.startsWith("scale(")) {
1002 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
1003 if (sub.indexOf(',') > -1) {
1004 b.transform.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
1005 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
1007 b.transform.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
1008 Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
1011 } else if (t.startsWith("matrix(")) {
1012 // FIXME: is this mapped right?
1013 float d[] = new float[6];
1014 StringTokenizer st = new StringTokenizer(t, ",", false);
1015 for(int i=0; i<6; i++)
1016 d[i] = Float.parseFloat(st.nextToken());
1017 b.transform.multiply(new VectorGraphics.Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
1019 t = t.substring(t.indexOf(')') + 1).trim();
1024 public Object get(Box b) { return b.transform.toString(); }
1027 specialBoxProperties.put("font", new SpecialBoxProperty() {
1028 public Object get(Box b) { return b.font; }
1029 public void put(Box b, Object value) {
1030 // FIXME: translate value into a resource if it is a string
1031 b.font = value == null ? null : (Res)value;
1033 b.flags |= FONT_CHANGED_FLAG;
1037 specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
1038 public Object get(Box b) { return b.font; }
1039 public void put(Box b, Object value) {
1040 if (b.fontsize == stoi(value)) return;
1041 b.fontsize = stoi(value);
1043 b.flags |= FONT_CHANGED_FLAG;
1047 specialBoxProperties.put("strokewidth", new SpecialBoxProperty() {
1048 public Object get(Box b) { return new Integer(b.strokewidth); }
1049 public void put(Box b, Object value) {
1050 if (b.strokewidth == stoi(value)) return;
1051 b.strokewidth = stoi(value);
1055 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1056 public Object get(Box b) { return b; }
1057 public void put(Box b, Object value) {
1058 if (value == null) b.remove();
1059 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1060 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1064 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1065 public Object get(Box b) {
1066 Log.log(this, "warning: the orient property is deprecated");
1067 if (b.redirect == null) return "horizontal";
1068 else if (b.redirect != b) return get(b.redirect);
1069 else if (b.cols == 1) return "vertical";
1070 else if (b.rows == 1) return "horizontal";
1073 public void put(Box b, Object value) {
1074 Log.log(this, "warning: the orient property is deprecated");
1075 if (value == null) return;
1076 if (b.redirect == null) return;
1077 if (b.redirect != b) { put(b.redirect, value); return; }
1078 if (value.equals("vertical")) {
1079 if (b.rows == 0) return;
1080 b.rows = 0; b.cols = 1;
1081 } else if (value.equals("horizontal")) {
1082 if (b.cols == 0) return;
1083 b.cols = 0; b.rows = 1;
1085 Log.log(this, "invalid value put to orient property: " + value);
1089 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1090 public Object get(Box b) { return (((b.flags & HSHRINK_FLAG) != 0) || ((b.flags & VSHRINK_FLAG) != 0)) ? Boolean.TRUE : Boolean.FALSE; }
1091 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1094 //#repeat hshrink/vshrink HSHRINK_FLAG/VSHRINK_FLAG
1095 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1096 public Object get(Box b) { return new Boolean((b.flags & HSHRINK_FLAG) != 0); }
1097 public void put(Box b, Object value) {
1098 boolean newshrink = stob(value);
1099 if (((b.flags & HSHRINK_FLAG) != 0) == newshrink) return;
1100 if (newshrink) b.flags |= HSHRINK_FLAG; else b.flags &= ~HSHRINK_FLAG;
1107 specialBoxProperties.put("x", new SpecialBoxProperty() {
1108 public Object get(Box b) {
1109 if (b.surface == null) return new Integer(0);
1110 if ((b.flags & INVISIBLE_FLAG) != 0) return new Integer(0);
1111 return new Integer(b.x);
1113 public void put(Box b, Object value) {
1114 if (!((b.flags & NOTPACKED_FLAG) != 0)) return;
1115 int x = stoi(value);
1116 if (x == b.x) return;
1119 if (b.parent == null && b.surface != null) b.surface.setLocation();
1126 //#repeat width/height minwidth/minheight maxwidth/maxheight
1127 specialBoxProperties.put("width", new SpecialBoxProperty() {
1128 public Object get(Box b) { return new Integer(b.width); }
1129 public void put(Box b, Object value) {
1130 b.width = stoi(value);
1131 if (b.parent == null && b.surface != null) {
1132 b.surface.setSize();
1135 if (b.minwidth == b.width && b.maxwidth == b.width) return;
1136 b.minwidth = b.maxwidth = b.width;
1142 //#repeat cols/rows rows/cols
1143 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1144 public Object get(Box b) { return new Double(b.cols); }
1145 public void put(Box b, Object value) {
1146 if (b.cols == stoi(value)) return;
1147 b.cols = stoi(value);
1148 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1149 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1154 //#repeat colspan/rowspan
1155 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1156 public Object get(Box b) { return new Double(b.colspan); }
1157 public void put(Box b, Object value) {
1158 if (b.colspan == stoi(value)) return;
1159 b.colspan = stoi(value);
1165 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1166 public Object get(Box b) { return ((b.flags & TILE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1167 public void put(Box b, Object value) {
1168 if (((b.flags & TILE_FLAG) != 0) == stob(value)) return;
1169 if (stob(value)) b.flags |= TILE_FLAG; else b.flags &= ~TILE_FLAG;
1173 specialBoxProperties.put("noclip", new SpecialBoxProperty() {
1174 public Object get(Box b) { return ((b.flags & NOCLIP_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1175 public void put(Box b, Object value) {
1176 if (((b.flags & NOCLIP_FLAG) != 0) == stob(value)) return;
1177 if (stob(value)) b.flags |= NOCLIP_FLAG; else b.flags &= ~NOCLIP_FLAG;
1182 specialBoxProperties.put("visible", 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.FALSE; }
1186 return Boolean.TRUE;
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("packed", new SpecialBoxProperty() {
1201 public Object get(Box b) { return ((b.flags & NOTPACKED_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1202 public void put(Box b, Object value) {
1203 if (stob(value) == ((b.flags & NOTPACKED_FLAG) == 0)) return;
1204 if (!stob(value)) b.flags |= NOTPACKED_FLAG; else b.flags &= ~NOTPACKED_FLAG;
1205 if ((b.flags & NOTPACKED_FLAG) != 0) { b.x = 0; b.y = 0; }
1206 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1209 specialBoxProperties.put("image", new SpecialBoxProperty() {
1211 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1213 public void put(Box b, Object value) {
1214 //if (image != null) System.out.println("hit");
1215 if (value == null) {
1217 } else if (value instanceof Res) {
1218 b.image = Picture.fromRes((Res)value);
1222 b.minwidth = min(b.maxwidth, max(b.minwidth, b.image == null ? 0 : b.image.getWidth()));
1223 b.minheight = min(b.maxheight, max(b.minheight, b.image == null ? 0 : 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("mouseinside", new SpecialBoxProperty() {
1267 public Object get(Box b) {
1268 return ((b.flags & MOUSEINSIDE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1271 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1272 public Object get(Box b) {
1273 if (b.redirect == null) return new Integer(0);
1274 if (b.redirect != b) return get(b.redirect);
1275 return new Integer(b.numChildren());
1278 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1279 public void put(String name, Box b, Object value) {
1280 Surface surface = b.getRoot().surface;
1281 if (surface == null) return;
1282 int mousex = surface.mousex;
1283 int mousey = surface.mousey;
1284 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1288 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1289 if (c.inside(mousex - c.x, mousey - c.y)) {
1294 if (b.parent != null) b.parent.put(name, value);
1297 specialBoxProperties.put("Press1", mouseEventHandler);
1298 specialBoxProperties.put("Press2", mouseEventHandler);
1299 specialBoxProperties.put("Press3", mouseEventHandler);
1300 specialBoxProperties.put("Release1", mouseEventHandler);
1301 specialBoxProperties.put("Release2", mouseEventHandler);
1302 specialBoxProperties.put("Release3", mouseEventHandler);
1303 specialBoxProperties.put("Click1", mouseEventHandler);
1304 specialBoxProperties.put("Click2", mouseEventHandler);
1305 specialBoxProperties.put("Click3", mouseEventHandler);
1306 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1307 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1308 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1310 specialBoxProperties.put("root", new SpecialBoxProperty() {
1311 public void put(Box b, Object value) {
1312 if (stob(value)) b.flags |= ISROOT_FLAG;
1313 else b.flags &= ~ISROOT_FLAG;
1315 public Object get(Box b) {
1316 if (b.parent == null || ((b.flags & ISROOT_FLAG) != 0)) return b;
1317 else return get(b.parent);
1320 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1321 public Object get(Box b) {
1322 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1325 public void put(Box b, Object value) {
1326 if (b.surface == null) return;
1327 boolean val = stob(value);
1328 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1332 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1333 public Object get(Box b) {
1334 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1337 public void put(Box b, Object value) {
1338 if (b.surface == null) return;
1339 boolean val = stob(value);
1340 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1344 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1345 public void put(Box b, Object value) {
1346 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1350 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1351 public void put(Box b, Object value) {
1352 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1356 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1357 public void put(Box b, Object value) {
1358 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1362 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1363 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
1364 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
1365 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1366 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1369 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
1370 public Object get(Box b) {
1371 if (b.redirect == null) return new Integer(0);
1372 if (b.redirect != b) return get(b.redirect);
1373 return new Integer(b.hpad);
1375 public void put(Box b, Object value) {
1376 if (b.redirect == null) return;
1377 if (b.redirect != b) { put(b.redirect, value); return; }
1378 int newval = stoi(value);
1379 if (newval == b.hpad) return;
1386 //#repeat minwidth/minheight maxwidth/maxheight
1387 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1388 public Object get(Box b) { return new Integer(b.minwidth); }
1389 public void put(Box b, Object value) {
1390 if (stoi(value) == b.minwidth) return;
1391 b.minwidth = stoi(value);
1395 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1396 public Object get(Box b) { return new Integer(b.maxwidth); }
1397 public void put(Box b, Object value) {
1398 if (stoi(value) == b.maxwidth) return;
1399 b.maxwidth = stoi(value);
1405 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1406 public void put(Box b, Object value) {
1407 if (b.redirect != b) Log.log(this, "cannot change the redirect of a box once it is set");
1408 else if (value == null) b.redirect = null;
1410 Box b2 = (Box)value;
1411 while(b2 != b && b2 != null) b2 = b2.parent;
1412 if (b2 == null) Log.log(this, "a box's redirect must be one of its descendants");
1413 b.redirect = (Box)value;
1416 public Object get(Box b) {
1417 if (b.redirect == null) return null;
1418 if (b.redirect == b) return Boolean.TRUE;
1419 return get(b.redirect);
1423 // FEATURE: this still isn't totally harmonious; when you createSurface, these aren't checked
1424 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1425 public void put(Box b, Object value) {
1426 if (b.surface != null) b.surface.setTitleBarText(value.toString());
1427 b.put_("titlebar", value);
1429 public Object get(Box b) { return b.get_("titlebar"); }
1432 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1435 public void put(Box b, Object value) {
1436 b.put_("icon", value);
1437 if (b.surface == null) return;
1438 Picture pic = ImageDecoder.getPicture(value.toString());
1439 if (pic != null) b.surface.setIcon(pic);
1440 else if (Log.on) Log.log(this, "unable to load icon " + value);
1442 public Object get(Box b) { return b.get_("icon"); }
1447 private static abstract class ColorBoxProperty extends SpecialBoxProperty {
1448 public abstract int getColor(Box b);
1449 public abstract void putColor(Box b, int argb);
1450 public Object get(Box b) {
1451 if ((getColor(b) & 0xFF000000) == 0) return null;
1452 String red = Integer.toHexString((getColor(b) & 0x00FF0000) >> 16);
1453 String green = Integer.toHexString((getColor(b) & 0x0000FF00) >> 8);
1454 String blue = Integer.toHexString(getColor(b) & 0x000000FF);
1455 if (red.length() < 2) red = "0" + red;
1456 if (blue.length() < 2) blue = "0" + blue;
1457 if (green.length() < 2) green = "0" + green;
1458 return "#" + red + green + blue;
1460 public void put(Box b, Object value) {
1461 int newcolor = getColor(b);
1462 String s = value == null ? null : value.toString();
1463 if (value == null) newcolor = 0x00000000;
1464 else if (s.length() > 0 && s.charAt(0) == '#')
1466 newcolor = 0xFF000000 |
1467 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
1468 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
1469 Integer.parseInt(s.substring(5, 7), 16);
1470 } catch (NumberFormatException e) {
1471 Log.log(this, "invalid color " + s);
1474 else if (org.xwt.translators.SVG.colors.get(s) != null)
1475 newcolor = 0xFF000000 | ((Integer)org.xwt.translators.SVG.colors.get(s)).intValue();
1476 if (newcolor == getColor(b)) return;
1477 putColor(b, newcolor);
1483 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1484 public static boolean stob(Object o) {
1485 if (o == null) return false;
1486 return Boolean.TRUE.equals(o) || "true".equals(o);
1489 /** helper that converts a String to an int according to JavaScript coercion rules */
1490 public static int stoi(Object o) {
1491 if (o == null) return 0;
1492 if (o instanceof Integer) return ((Integer)o).intValue();
1495 if (!(o instanceof String)) s = o.toString();
1498 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1499 catch (NumberFormatException e) { return 0; }