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 String text = null;
111 private Res font = null;
112 private int fontsize = 10;
113 private LENGTH textwidth = 0;
114 private LENGTH textheight = 0;
116 // FEATURE: use shorts
117 private int rows = 1;
118 private int cols = 0;
119 private int rowspan = 1;
120 private int colspan = 1;
122 // computed during reflow
125 public LENGTH width = 0;
126 public LENGTH height = 0;
127 private int row = 0; // FEATURE use a short
128 private int col = 0; // FEATURE use a short
129 private LENGTH contentwidth = 0; // == max(minwidth, textwidth, sum(child.contentwidth))
130 private LENGTH contentheight = 0;
131 private LENGTH offset_x = 0;
132 private LENGTH offset_y = 0;
135 // Rendering Properties ///////////////////////////////////////////////////////////
137 private VectorGraphics.VectorPath path = null;
138 private VectorGraphics.Affine transform = null;
140 private VectorGraphics.RasterPath rpath = null;
141 private VectorGraphics.Affine rtransform = null;
143 //private VectorGraphics.Paint fill = null;
144 //private VectorGraphics.Paint stroke = null;
147 public Picture image; // will disappear
148 private int fillcolor = 0x00000000; // will become VectorGraphics.Paint
149 private int strokecolor = 0xFF000000; // will become VectorGraphics.Paint
150 private int textcolor = 0xFF000000; // will become VectorGraphics.Paint
152 private String cursor = null; // the cursor for this box
155 // Instance Methods /////////////////////////////////////////////////////////////////////
157 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
158 public final void dirty() {
159 if ((flags & NOCLIP_FLAG) != 0 && parent != null) parent.dirty();
160 else dirty(0, 0, width, height);
162 public final void dirty(int x, int y, int w, int h) {
163 for(Box cur = this; cur != null; cur = cur.parent) {
164 if ((flags & NOCLIP_FLAG) == 0) {
165 w = min(x + w, cur.width) - max(x, 0);
166 h = min(y + h, cur.height) - max(y, 0);
170 if (w <= 0 || h <= 0) return;
171 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
178 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
179 * to see if this node requires any Enter, Leave, or Move notifications.
181 * @param forceleave set to true by the box's parent if the mouse is inside an older
182 * sibling, which is covering up this box.
184 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
185 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
187 boolean wasinside = (flags & MOUSEINSIDE_FLAG) != 0;
188 boolean isinside = !((flags & INVISIBLE_FLAG) != 0) && inside(mousex, mousey) && !forceleave;
189 if (isinside) flags |= MOUSEINSIDE_FLAG; else flags &= ~MOUSEINSIDE_FLAG;
191 if (!wasinside && !isinside) return;
193 if (!wasinside && isinside && get("Enter", Trap.class) != null) put("Enter", Boolean.TRUE);
194 else if (wasinside && !isinside && get("Leave", Trap.class) != null) put("Leave", Boolean.TRUE);
195 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && get("Move", Trap.class) != null)
196 put("Move", Boolean.TRUE);
198 if (isinside && cursor != null) getRoot().cursor = cursor;
200 // if the mouse has moved into our padding region, it is considered 'outside' all our children
201 if (!(mousex >= 0 && mousey >= 0 && mousex < width && mousey < height)) forceleave = true;
203 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
204 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
205 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
210 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
212 /** only for use on the root box */
213 void reflow() { reflow(width, height); }
214 void reflow(int new_width, int new_height) {
216 if (Surface.abort) return;
217 //#repeat width/height new_width/new_height HSHRINK_FLAG/VSHRINK_FLAG contentwidth/contentheight minwidth/minheight maxwidth/maxheight
218 new_width = bound(max(contentwidth, minwidth),
220 ((flags & HSHRINK_FLAG) != 0) ? max(contentwidth, minwidth) : maxwidth);
222 resize(x, y, new_width, new_height);
225 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
227 if (!needs_reflow) return;
228 contentwidth = contentheight = 0;
230 // --- Phase 0 ----------------------------------------------------------------------
232 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
233 if (((flags & FONT_CHANGED_FLAG) != 0) && child.font == null) {
234 child.flags |= FONT_CHANGED_FLAG;
235 child.needs_reflow = true;
238 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
240 flags &= ~FONT_CHANGED_FLAG;
242 // --- Phase 1 ----------------------------------------------------------------------
243 // assign children to their row/column positions (assuming constrained columns)
244 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
245 //#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
246 if (rows == 0 && numChildren() != 0) {
247 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
248 Box child = getChild(0);
249 for(; child != null && (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
250 OUTER: for(int row=0; child != null; row++) {
251 for(int col=0; child != null && col < cols;) {
252 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
253 while(col < cols && numRowsInCol[col] > row) col++;
254 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
255 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
258 if (col + min(cols, child.colspan) > cols) break;
259 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
262 col += min(cols, child.colspan);
263 child = child.nextSibling();
264 for(; child != null && (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
270 // --- Phase 2 ----------------------------------------------------------------------
271 // compute the min/max sizes of the columns and rows and set our contentwidth
272 //#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
273 if (numChildren() != 0) {
276 for(Box child = getChild(0); child != null; child = child.nextSibling())
277 numCols = max(numCols, child.col + child.colspan);
278 LENGTH[] colWidth = new LENGTH[numCols];
279 for(Box child = getChild(0); child != null; child = child.nextSibling())
280 if (!(((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
281 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
282 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
284 contentwidth = max(textwidth, contentwidth);
285 contentwidth = bound(minwidth, contentwidth, maxwidth);
289 if ((flags & NOTPACKED_FLAG) == 0) {
291 if (rpath == null) rpath = path.realize(transform == null ? VectorGraphics.Affine.identity() : transform);
292 if ((flags & HSHRINK_FLAG) != 0) contentwidth = max(contentwidth, rpath.boundingBoxWidth());
293 if ((flags & VSHRINK_FLAG) != 0) contentheight = max(contentheight, rpath.boundingBoxHeight());
294 // FIXME: separate offset_x needed for the path
296 //#repeat x1/y1 x2/y2 x3/y3 x4/y4 contentwidth/contentheight left/top right/bottom
297 int x1 = transform == null ? 0 : (int)transform.multiply_px(0, 0);
298 int x2 = transform == null ? 0 : (int)transform.multiply_px(contentwidth, 0);
299 int x3 = transform == null ? contentwidth : (int)transform.multiply_px(contentwidth, contentheight);
300 int x4 = transform == null ? contentwidth : (int)transform.multiply_px(0, contentheight);
301 int left = min(min(x1, x2), min(x3, x4));
302 int right = max(max(x1, x2), max(x3, x4));
303 contentwidth = max(contentwidth, right - left);
304 offset_x = -1 * left;
310 private void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
312 // --- Phase 1 ----------------------------------------------------------------------
313 // run PosChange/SizeChange, dirty as needed
314 if (x != this.x || y != this.y || width != this.width || height != this.height) {
315 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
316 boolean sizechange = false, poschange = false;
317 if ((this.width != width || this.height != height) && get("SizeChange", Trap.class) != null) sizechange = true;
318 if ((this.x != x || this.y != y) && get("PosChange", Trap.class) != null) poschange = true;
319 this.width = width; this.height = height; this.x = x; this.y = y;
321 if (sizechange || poschange)
322 if (sizePosChangesSinceLastRender == 500) {
323 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
325 sizePosChangesSinceLastRender++;
326 try { if (sizechange) put("SizeChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
327 try { if (poschange) put("PosChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
328 Surface.abort = true;
334 // --- short circuit ----------------------------------------------------------------
335 if (!needs_reflow) return;
336 needs_reflow = false;
337 if (numChildren() == 0) return;
339 // --- Phase 2 ----------------------------------------------------------------------
340 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
342 //#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
345 for(Box child = getChild(0); child != null; child = child.nextSibling())
346 numCols = max(numCols, child.col + child.colspan);
347 LENGTH[] colWidth = new LENGTH[numCols];
348 LENGTH[] colMaxWidth = new LENGTH[numCols];
349 int marginWidth = width;
350 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
353 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
354 if (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)) continue;
355 //#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
356 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
357 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
358 colMaxWidth[i] = max(colMaxWidth[i], (((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) / child.colspan);
362 //#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
363 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
365 for(int i=0; i<colMaxWidth.length; i++) {
366 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
367 marginWidth -= colMaxWidth[i];
368 if (marginWidth < 0) { marginWidth = 0; break; }
373 // --- Phase 3 ----------------------------------------------------------------------
374 // hand out the slack
376 //#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
378 for(int i=0; i<numCols; i++) slack -= colWidth[i];
379 if (numChildren() > 0)
381 // FEATURE: inefficient
382 int startslack = slack;
383 int increment = max(1, slack / numCols);
384 for(int col=0; col < numCols && slack > 0; col++) {
385 slack += colWidth[col];
386 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
387 slack -= colWidth[col];
389 if (slack == startslack) break;
394 // --- Phase 4 ----------------------------------------------------------------------
395 // assign children's new sizes and positions and recurse
396 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
397 if ((child.flags & INVISIBLE_FLAG) != 0) continue;
398 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
399 if ((child.flags & NOTPACKED_FLAG) != 0) {
402 child_width = ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : min(child.maxwidth, width - child.x);
403 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y);
406 //#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 child_x/child_y child_width/child_height
407 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
408 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
409 if (transform == null) child_x = marginWidth / 2;
410 for(int i=0; i<child.col; i++) child_x += colWidth[i];
411 if (diff < 0) child_x += -1 * (diff / 2);
415 child.resize(child_x, child_y, child_width, child_height);
422 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
424 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
425 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf, VectorGraphics.Affine a) {
426 if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
427 int globalx = parentx + (parent == null ? 0 : x);
428 int globaly = parenty + (parent == null ? 0 : y);
430 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
431 if ((flags & NOCLIP_FLAG) == 0) {
432 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw, (parent == null ? 0 : globalx) + width) - globalx;
433 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph, (parent == null ? 0 : globaly) + height) - globaly;
434 clipx = max(clipx, parent == null ? 0 : globalx);
435 clipy = max(clipy, parent == null ? 0 : globaly);
437 if (clipw <= 0 || cliph <= 0) return;
439 if (path == null && (fillcolor & 0xFF000000) != 0x00000000) {
440 VectorGraphics.VectorPath path2 = VectorGraphics.parseVectorPath("M0,0H" + width + "V" + height + "H0V0z");
441 path2.realize(a).fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
445 if ((flags & TILE_FLAG) != 0) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
446 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
448 if (text != null && !text.equals(""))
449 renderText(globalx, globaly, clipx, clipy, clipw, cliph, buf);
452 if (rtransform == null) rpath = null;
453 else if (!rtransform.equalsIgnoringTranslation(a)) rpath = null;
455 rpath.translate((int)(a.e - rtransform.e), (int)(a.f - rtransform.f));
456 rtransform = a.copy();
460 rpath = path.realize(a == null ? VectorGraphics.Affine.identity() : a);
462 if ((strokecolor & 0xff000000) != 0) rpath.stroke(buf, 1, strokecolor);
463 if ((fillcolor & 0xff000000) != 0) rpath.fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
466 for(Box b = getChild(0); b != null; b = b.nextSibling()) {
467 VectorGraphics.Affine a2 = VectorGraphics.Affine.translate(b.x, b.y);
468 if (transform != null) a2.multiply(transform);
469 a2.multiply(VectorGraphics.Affine.translate(offset_x, offset_y));
471 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf, a2);
475 void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
477 buf.drawPicture(image, clipx, clipy, clipx + clipw, clipy + cliph, 0, 0, image.getWidth(), image.getHeight());
480 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, PixelBuffer buf) {
481 int iw = image.getWidth();
482 int ih = image.getHeight();
483 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
484 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
486 int dx1 = max(i * iw + x, x);
487 int dy1 = max(j * ih + y, y);
488 int dx2 = min((i+1) * iw + x, x + w);
489 int dy2 = min((j+1) * ih + y, y + h);
491 int sx1 = dx1 - (i*iw) - x;
492 int sy1 = dy1 - (j*ih) - y;
493 int sx2 = dx2 - (i*iw) - x;
494 int sy2 = dy2 - (j*ih) - y;
496 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
497 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
502 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
503 for(int i=0; i<text.length(); i++) {
505 char c = text.charAt(i);
506 Glyph g = Glyph.getGlyph(font, fontsize, c);
507 buf.drawPictureAlphaOnly(g.p,
509 y + g.max_ascent - g.baseline,
511 y + g.max_ascent - g.baseline + g.p.getHeight(),
513 g.p.getWidth(), g.p.getHeight(),
520 // Methods to implement org.xwt.js.JS //////////////////////////////////////
522 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
523 if ("indexof".equals(method)) {
524 if (checkOnly) return Boolean.TRUE;
525 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
526 Box b = (Box)args.elementAt(0);
527 if (b.parent != Box.this) {
528 if (redirect == null || redirect == Box.this) return new Integer(-1);
529 return redirect.callMethod(method, args, checkOnly);
531 return new Integer(b.getIndexInParent());
536 /** Returns the i_th child */
537 public Object get(int i) {
538 if (redirect == null) return null;
539 if (redirect != this) return redirect.get(i);
540 return i >= numChildren() || i < 0 ? null : getChild(i);
544 * Inserts value as child i; calls remove() if necessary.
545 * This method handles "reinserting" one of your children properly.
546 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
547 * WARNING: O(n) runtime, unless i == numChildren()
549 public void put(int i, Object value) {
551 Trap t = value == null ? (Trap)get("childremoved", Trap.class) : (Trap)get("childadded", Trap.class);
553 if (value != null && !(value instanceof Box)) {
554 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to a non-box");
558 if (redirect == null) {
559 if (t != null) t.perform(value);
560 else if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
561 } else if (redirect != this) {
562 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
563 redirect.put(i, value);
564 if (t != null) t.perform(value);
565 } else if (value == null) {
566 if (i >= 0 && i < numChildren()) {
569 if (t != null) t.perform(b);
572 Box newnode = (Box)value;
574 // check if box being moved is currently target of a redirect
575 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
576 if (cur.redirect == newnode) {
577 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
581 // check for recursive ancestor violation
582 for(Box cur = this; cur != null; cur = cur.parent)
583 if (cur == newnode) {
584 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
585 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
589 if (numKids > 15 && children == null) convert_to_array();
591 newnode.parent = this;
593 if (children == null) {
594 if (firstKid == null) {
596 newnode.prevSibling = newnode;
597 newnode.nextSibling = newnode;
598 } else if (i >= numKids) {
599 newnode.prevSibling = firstKid.prevSibling;
600 newnode.nextSibling = firstKid;
601 firstKid.prevSibling.nextSibling = newnode;
602 firstKid.prevSibling = newnode;
605 for(int j=0; j<i; j++) cur = cur.nextSibling;
606 newnode.prevSibling = cur.prevSibling;
607 newnode.nextSibling = cur;
608 cur.prevSibling.nextSibling = newnode;
609 cur.prevSibling = newnode;
610 if (i == 0) firstKid = newnode;
615 if (i >= children.size()) {
616 newnode.indexInParent = children.size();
617 children.addElement(newnode);
619 children.insertElementAt(newnode, i);
620 for(int j=i; j<children.size(); j++)
621 getChild(j).indexInParent = j;
625 // need both of these in case child was already uncalc'ed
628 MARK_FOR_REFLOW_this;
631 if (t != null) t.perform(b);
635 public Object get(Object key, Object key2) { return super.get(key, key2); }
636 public void put(Object key, Object key2, Object val) { super.put(key, key2, val); }
638 public Object get_(Object name) { return super.get(name); }
639 public Object get(Object name) { return get(name, false); }
640 public Object get(Object name_, boolean ignoretraps) {
641 if (name_ instanceof Number) return get(((Number)name_).intValue());
643 if (!(name_ instanceof String)) return null;
644 String name = (String)name_;
645 if (name.equals("")) return null;
647 // See if we're triggering a trap
648 Trap t = ignoretraps ? (Trap)null : (Trap)get(name, Trap.class);
649 if (t != null) return t.perform();
651 // Check for a special handler
652 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
653 if (gph != null) return gph.get(this);
655 Object ret = super.get(name);
656 if (name.startsWith("$") && ret == null)
657 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" +
658 name.substring(1) + "\" found");
662 public Object[] keys() {
663 Object[] ret = new Object[numChildren()];
664 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
668 public void addTrap(Object name, Object value) { Trap.addTrap(this, name, ((JS.CompiledFunction)value)); }
669 public void delTrap(Object name, Object value) { Trap.delTrap(this, name, ((JS.CompiledFunction)value)); }
674 * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack)
675 * @param rp if this put is being performed via a root proxy, rp is the root proxy.
677 public void put_(Object name, Object value) { super.put(name, value); }
678 public void put(Object name, Object value) { put(name, value, false); }
679 public void put(Object name_, Object value, boolean ignoretraps) {
680 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
681 if (!(name_ instanceof String)) { super.put(name_,value); return; }
682 String name = name_.toString();
684 Trap t = (Trap)get(name, Trap.class);
691 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
692 if (gph != null) { gph.put(name, this, value); return; }
694 super.put(name, value);
698 // Tree Manipulation /////////////////////////////////////////////////////////////////////
700 /** The parent of this node */
701 private Box parent = null;
703 // Variables used in Vector mode */
704 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
705 private int indexInParent;
706 private Vec children = null;
708 // Variables used in linked-list mode
709 private int numKids = 0;
710 private Box nextSibling = null;
711 private Box prevSibling = null;
712 private Box firstKid = null;
714 // when we get more than 15 children, we switch to array-mode
715 private void convert_to_array() {
716 children = new Vec(numKids);
719 children.addElement(cur);
720 cur.indexInParent = children.size() - 1;
721 cur = cur.nextSibling;
722 } while (cur != firstKid);
725 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
726 public void remove() {
727 if (parent == null) {
728 if (surface != null) surface.dispose(true);
731 Box oldparent = parent;
732 if (oldparent == null) return;
733 MARK_FOR_REFLOW_this;
735 flags &= ~MOUSEINSIDE_FLAG;
737 if (parent.children != null) {
738 parent.children.removeElementAt(indexInParent);
739 for(int j=indexInParent; j<parent.children.size(); j++)
740 (parent.getChild(j)).indexInParent = j;
743 if (parent.firstKid == this) {
744 if (nextSibling == this) parent.firstKid = null;
745 else parent.firstKid = nextSibling;
748 prevSibling.nextSibling = nextSibling;
749 nextSibling.prevSibling = prevSibling;
755 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
757 // note that JavaScript box[0] will invoke put(int i), not put(String s)
758 if (oldparent != null) {
759 Trap t = (Trap)oldparent.get("childremoved", Trap.class);
760 if (t != null) t.perform(this);
764 /** returns our next sibling (parent[ourindex + 1]) */
765 public final Box nextSibling() {
766 if (parent == null) return null;
767 if (parent.children == null) {
768 if (nextSibling == parent.firstKid) return null;
771 if (indexInParent >= parent.children.size() - 1) return null;
772 return (Box)parent.children.elementAt(indexInParent + 1);
776 /** returns our next sibling (parent[ourindex + 1]) */
777 public final Box prevSibling() {
778 if (parent == null) return null;
779 if (parent.children == null) {
780 if (this == parent.firstKid) return null;
783 if (indexInParent == 0) return null;
784 return (Box)parent.children.elementAt(indexInParent - 1);
788 /** Returns the parent of this node */
789 public Box getParent() { return parent; }
791 /** Returns ith child */
792 public Box getChild(int i) {
793 if (children == null) {
794 if (firstKid == null) return null;
795 if (i >= numKids) return null;
796 if (i == numKids - 1) return firstKid.prevSibling;
798 for(int j=0; j<i; j++) cur = cur.nextSibling;
801 if (i >= children.size() || i < 0) return null;
802 return (Box)children.elementAt(i);
806 /** Returns the number of children */
807 public int numChildren() {
808 if (children == null) {
809 if (firstKid == null) return 0;
811 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
814 return children.size();
818 /** Returns our index in our parent */
819 public int getIndexInParent() {
820 if (parent == null) return 0;
821 if (parent.children == null) {
823 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
826 return indexInParent;
830 /** returns the root of the surface that this box belongs to */
831 public final Box getRoot() {
832 if (parent == null && surface != null) return this;
833 if (parent == null) return null;
834 return parent.getRoot();
838 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
840 static final int min(int a, int b) { if (a<b) return a; else return b; }
841 static final float min(float a, float b) { if (a<b) return a; else return b; }
842 static final double min(double a, double b) { if (a<b) return a; else return b; }
843 static final int max(int a, int b) { if (a>b) return a; else return b; }
844 static final float max(float a, float b) { if (a>b) return a; else return b; }
845 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; }
846 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; }
847 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
848 final boolean inside(int x, int y) { return (!((flags & INVISIBLE_FLAG) != 0) && x >= 0 && y >= 0 && x < width && y < height); }
850 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
851 public static Box whoIs(Box cur, int x, int y) {
853 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
857 // WARNING: this method is called from the event-queueing
858 // thread -- it may run concurrently with ANY part of XWT, and
859 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
862 if ((cur.flags & INVISIBLE_FLAG) != 0) return null;
863 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
865 for(int i=cur.numChildren() - 1; i>=0; i--) {
866 Box child = cur.getChild(i);
867 if (child == null) continue; // since this method is unsynchronized, we have to double-check
870 if (!((child.flags & INVISIBLE_FLAG) != 0) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
880 * A helper class for properties of Box which require special
883 * To avoid excessive use of String.equals(), the Box.get() and
884 * Box.put() methods employ a Hash keyed on property names that
885 * require special handling. The value stored in the Hash is an
886 * instance of an anonymous subclass of SpecialBoxProperty, which knows
887 * how to handle get()s and put()s for that property name. There
888 * should be one anonymous subclass of SpecialBoxProperty for each
889 * specially-handled property on Box.
891 static class SpecialBoxProperty {
893 SpecialBoxProperty() { }
895 /** stores instances of SpecialBoxProperty; keyed on property name */
896 static Hash specialBoxProperties = new Hash(200, 3);
898 /** this method defines the behavior when the property is get()ed from b */
899 Object get(Box b) { return null; }
901 /** this method defines the behavior when the property is put() to b */
902 void put(Box b, Object value) { }
904 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
905 void put(String name, Box b, Object value) { put(b, value); }
908 specialBoxProperties.put("fill", new ColorBoxProperty() {
909 public int getColor(Box b) { return b.fillcolor; }
910 public void putColor(Box b, int argb) { b.fillcolor = argb; }
912 specialBoxProperties.put("textcolor", new ColorBoxProperty() {
913 public int getColor(Box b) { return b.textcolor; }
914 public void putColor(Box b, int argb) { b.textcolor = argb; }
916 specialBoxProperties.put("strokecolor", new ColorBoxProperty() {
917 public int getColor(Box b) { return b.strokecolor; }
918 public void putColor(Box b, int argb) { b.strokecolor = argb; }
921 specialBoxProperties.put("text", new SpecialBoxProperty() {
922 public Object get(Box b) { return b.text; }
923 public void put(Box b, Object value) {
924 String t = value == null ? "null" : value.toString();
925 if (t.equals(b.text)) return;
928 if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
929 b.textwidth = b.textheight = 0;
934 for(int i=0; i<b.text.length(); i++) {
935 Glyph g = Glyph.getGlyph(b.font, b.fontsize, b.text.charAt(i));
936 b.textwidth += g.advance;
937 b.textheight = g.max_ascent + g.max_descent;
939 } catch (Exception e) {
946 specialBoxProperties.put("path", new SpecialBoxProperty() {
947 public Object get(Box b) { throw new JS.Exn("cannot read from the path property"); }
948 public void put(Box b, Object value) {
949 String t = value == null ? "null" : value.toString();
950 b.path = value == null ? null : VectorGraphics.parseVectorPath(value.toString());
955 specialBoxProperties.put("transform", new SpecialBoxProperty() {
956 public void put(Box b, Object value) {
957 String t = value.toString().trim();
958 b.transform = VectorGraphics.Affine.identity();
959 while (t.length() > 0) {
960 if (t.startsWith("skewX(")) {
963 } else if (t.startsWith("shear(")) {
964 // FIXME: nonstandard; remove this
965 b.transform.multiply(VectorGraphics.Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
967 } else if (t.startsWith("skewY(")) {
970 } else if (t.startsWith("rotate(")) {
971 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
972 if (sub.indexOf(',') != -1) {
973 float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
974 sub = sub.substring(sub.indexOf(',') + 1);
975 float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
976 sub = sub.substring(sub.indexOf(',') + 1);
977 float cy = Float.parseFloat(sub);
978 b.transform.multiply(VectorGraphics.Affine.translate(cx, cy));
979 b.transform.multiply(VectorGraphics.Affine.rotate(angle));
980 b.transform.multiply(VectorGraphics.Affine.translate(-1 * cx, -1 * cy));
982 b.transform.multiply(VectorGraphics.Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
985 } else if (t.startsWith("translate(")) {
986 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
987 if (sub.indexOf(',') > -1) {
988 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
989 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
991 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
994 } else if (t.startsWith("flip(")) {
995 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
996 b.transform.multiply(VectorGraphics.Affine.flip(which.equals("horizontal"), which.equals("vertical")));
998 } else if (t.startsWith("scale(")) {
999 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
1000 if (sub.indexOf(',') > -1) {
1001 b.transform.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
1002 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
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(',')))));
1008 } else if (t.startsWith("matrix(")) {
1009 // FIXME: is this mapped right?
1010 float d[] = new float[6];
1011 StringTokenizer st = new StringTokenizer(t, ",", false);
1012 for(int i=0; i<6; i++)
1013 d[i] = Float.parseFloat(st.nextToken());
1014 b.transform.multiply(new VectorGraphics.Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
1016 t = t.substring(t.indexOf(')') + 1).trim();
1021 public Object get(Box b) { return b.transform.toString(); }
1024 specialBoxProperties.put("font", new SpecialBoxProperty() {
1025 public Object get(Box b) { return b.font; }
1026 public void put(Box b, Object value) {
1027 // FIXME: translate value into a resource if it is a string
1028 b.font = value == null ? null : (Res)value;
1030 b.flags |= FONT_CHANGED_FLAG;
1034 specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
1035 public Object get(Box b) { return b.font; }
1036 public void put(Box b, Object value) {
1037 if (b.fontsize == stoi(value)) return;
1038 b.fontsize = stoi(value);
1040 b.flags |= FONT_CHANGED_FLAG;
1044 specialBoxProperties.put("strokewidth", new SpecialBoxProperty() {
1045 public Object get(Box b) { return new Integer(b.strokewidth); }
1046 public void put(Box b, Object value) {
1047 if (b.strokewidth == stoi(value)) return;
1048 b.strokewidth = stoi(value);
1052 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1053 public Object get(Box b) { return b; }
1054 public void put(Box b, Object value) {
1055 if (value == null) b.remove();
1056 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1057 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1061 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1062 public Object get(Box b) {
1063 Log.log(this, "warning: the orient property is deprecated");
1064 if (b.redirect == null) return "horizontal";
1065 else if (b.redirect != b) return get(b.redirect);
1066 else if (b.cols == 1) return "vertical";
1067 else if (b.rows == 1) return "horizontal";
1070 public void put(Box b, Object value) {
1071 Log.log(this, "warning: the orient property is deprecated");
1072 if (value == null) return;
1073 if (b.redirect == null) return;
1074 if (b.redirect != b) { put(b.redirect, value); return; }
1075 if (value.equals("vertical")) {
1076 if (b.rows == 0) return;
1077 b.rows = 0; b.cols = 1;
1078 } else if (value.equals("horizontal")) {
1079 if (b.cols == 0) return;
1080 b.cols = 0; b.rows = 1;
1082 Log.log(this, "invalid value put to orient property: " + value);
1086 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1087 public Object get(Box b) { return (((b.flags & HSHRINK_FLAG) != 0) || ((b.flags & VSHRINK_FLAG) != 0)) ? Boolean.TRUE : Boolean.FALSE; }
1088 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1091 //#repeat hshrink/vshrink HSHRINK_FLAG/VSHRINK_FLAG
1092 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1093 public Object get(Box b) { return new Boolean((b.flags & HSHRINK_FLAG) != 0); }
1094 public void put(Box b, Object value) {
1095 boolean newshrink = stob(value);
1096 if (((b.flags & HSHRINK_FLAG) != 0) == newshrink) return;
1097 if (newshrink) b.flags |= HSHRINK_FLAG; else b.flags &= ~HSHRINK_FLAG;
1104 specialBoxProperties.put("x", new SpecialBoxProperty() {
1105 public Object get(Box b) {
1106 if (b.surface == null) return new Integer(0);
1107 if ((b.flags & INVISIBLE_FLAG) != 0) return new Integer(0);
1108 return new Integer(b.x);
1110 public void put(Box b, Object value) {
1111 if (!((b.flags & NOTPACKED_FLAG) != 0)) return;
1112 int x = stoi(value);
1113 if (x == b.x) return;
1116 if (b.parent == null && b.surface != null) b.surface.setLocation();
1123 //#repeat width/height minwidth/minheight maxwidth/maxheight
1124 specialBoxProperties.put("width", new SpecialBoxProperty() {
1125 public Object get(Box b) { return new Integer(b.width); }
1126 public void put(Box b, Object value) {
1127 b.width = stoi(value);
1128 if (b.parent == null && b.surface != null) {
1129 b.surface.setSize();
1132 if (b.minwidth == b.width && b.maxwidth == b.width) return;
1133 b.minwidth = b.maxwidth = b.width;
1139 //#repeat cols/rows rows/cols
1140 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1141 public Object get(Box b) { return new Double(b.cols); }
1142 public void put(Box b, Object value) {
1143 if (b.cols == stoi(value)) return;
1144 b.cols = stoi(value);
1145 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1146 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1151 //#repeat colspan/rowspan
1152 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1153 public Object get(Box b) { return new Double(b.colspan); }
1154 public void put(Box b, Object value) {
1155 if (b.colspan == stoi(value)) return;
1156 b.colspan = stoi(value);
1162 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1163 public Object get(Box b) { return ((b.flags & TILE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1164 public void put(Box b, Object value) {
1165 if (((b.flags & TILE_FLAG) != 0) == stob(value)) return;
1166 if (stob(value)) b.flags |= TILE_FLAG; else b.flags &= ~TILE_FLAG;
1170 specialBoxProperties.put("noclip", new SpecialBoxProperty() {
1171 public Object get(Box b) { return ((b.flags & NOCLIP_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1172 public void put(Box b, Object value) {
1173 if (((b.flags & NOCLIP_FLAG) != 0) == stob(value)) return;
1174 if (stob(value)) b.flags |= NOCLIP_FLAG; else b.flags &= ~NOCLIP_FLAG;
1179 specialBoxProperties.put("visible", new SpecialBoxProperty() {
1180 public Object get(Box b) {
1181 for (Box cur = b; cur != null; cur = cur.parent) {
1182 if ((cur.flags & INVISIBLE_FLAG) != 0) return Boolean.FALSE; }
1183 return Boolean.TRUE;
1185 public void put(Box b, Object value) {
1186 if (!stob(value) == ((b.flags & INVISIBLE_FLAG) != 0)) return;
1187 if (!stob(value)) b.flags |= INVISIBLE_FLAG; else b.flags &= ~INVISIBLE_FLAG;
1188 if (b.parent == null) {
1189 if (b.surface != null) b.surface.setInvisible((b.flags & INVISIBLE_FLAG) != 0);
1192 MARK_FOR_REFLOW_b_parent;
1193 b.parent.dirty(b.x, b.y, b.width, b.height);
1197 specialBoxProperties.put("packed", new SpecialBoxProperty() {
1198 public Object get(Box b) { return ((b.flags & NOTPACKED_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1199 public void put(Box b, Object value) {
1200 if (stob(value) == ((b.flags & NOTPACKED_FLAG) == 0)) return;
1201 if (!stob(value)) b.flags |= NOTPACKED_FLAG; else b.flags &= ~NOTPACKED_FLAG;
1202 if ((b.flags & NOTPACKED_FLAG) != 0) { b.x = 0; b.y = 0; }
1203 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1206 specialBoxProperties.put("image", new SpecialBoxProperty() {
1208 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1210 public void put(Box b, Object value) {
1211 //if (image != null) System.out.println("hit");
1212 if (value == null) {
1214 } else if (value instanceof Res) {
1215 b.image = Picture.fromRes((Res)value);
1219 b.minwidth = min(b.maxwidth, max(b.minwidth, b.image == null ? 0 : b.image.getWidth()));
1220 b.minheight = min(b.maxheight, max(b.minheight, b.image == null ? 0 : b.image.getHeight()));
1226 //#repeat globalx/globaly x/y
1227 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1228 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1229 public void put(Box b, Object value) {
1230 if (b.surface == null || b.parent == null) return;
1231 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1237 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1238 public Object get(Box b) { return b.cursor; }
1239 public void put(Box b, Object value) {
1240 b.cursor = (String)value;
1241 if (b.surface == null) return;
1243 // see if we need to update the surface cursor
1244 Surface surface = b.getRoot().surface;
1245 String tempcursor = surface.cursor;
1246 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1247 if (surface.cursor != tempcursor) surface.syncCursor();
1251 //#repeat mousex/mousey x/y
1252 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1253 public Object get(Box b) {
1254 Surface surface = b.getRoot().surface;
1255 if (surface == null) return new Integer(0);
1256 int mousex = surface.mousex;
1257 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1258 return new Integer(mousex);
1263 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1264 public Object get(Box b) {
1265 return ((b.flags & MOUSEINSIDE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1268 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1269 public Object get(Box b) {
1270 if (b.redirect == null) return new Integer(0);
1271 if (b.redirect != b) return get(b.redirect);
1272 return new Integer(b.numChildren());
1275 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1276 public void put(String name, Box b, Object value) {
1277 Surface surface = b.getRoot().surface;
1278 if (surface == null) return;
1279 int mousex = surface.mousex;
1280 int mousey = surface.mousey;
1281 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1285 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1286 if (c.inside(mousex - c.x, mousey - c.y)) {
1291 if (b.parent != null) b.parent.put(name, value);
1294 specialBoxProperties.put("Press1", mouseEventHandler);
1295 specialBoxProperties.put("Press2", mouseEventHandler);
1296 specialBoxProperties.put("Press3", mouseEventHandler);
1297 specialBoxProperties.put("Release1", mouseEventHandler);
1298 specialBoxProperties.put("Release2", mouseEventHandler);
1299 specialBoxProperties.put("Release3", mouseEventHandler);
1300 specialBoxProperties.put("Click1", mouseEventHandler);
1301 specialBoxProperties.put("Click2", mouseEventHandler);
1302 specialBoxProperties.put("Click3", mouseEventHandler);
1303 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1304 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1305 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1307 specialBoxProperties.put("root", new SpecialBoxProperty() {
1308 public void put(Box b, Object value) {
1309 if (stob(value)) b.flags |= ISROOT_FLAG;
1310 else b.flags &= ~ISROOT_FLAG;
1312 public Object get(Box b) {
1313 if (b.parent == null || ((b.flags & ISROOT_FLAG) != 0)) return b;
1314 else return get(b.parent);
1317 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1318 public Object get(Box b) {
1319 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1322 public void put(Box b, Object value) {
1323 if (b.surface == null) return;
1324 boolean val = stob(value);
1325 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1329 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1330 public Object get(Box b) {
1331 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1334 public void put(Box b, Object value) {
1335 if (b.surface == null) return;
1336 boolean val = stob(value);
1337 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1341 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1342 public void put(Box b, Object value) {
1343 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1347 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1348 public void put(Box b, Object value) {
1349 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1353 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1354 public void put(Box b, Object value) {
1355 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1359 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1360 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
1361 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
1362 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1363 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1365 //#repeat minwidth/minheight maxwidth/maxheight
1366 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1367 public Object get(Box b) { return new Integer(b.minwidth); }
1368 public void put(Box b, Object value) {
1369 if (stoi(value) == b.minwidth) return;
1370 b.minwidth = stoi(value);
1374 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1375 public Object get(Box b) { return new Integer(b.maxwidth); }
1376 public void put(Box b, Object value) {
1377 if (stoi(value) == b.maxwidth) return;
1378 b.maxwidth = stoi(value);
1384 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1385 public void put(Box b, Object value) {
1386 if (b.redirect != b) Log.log(this, "cannot change the redirect of a box once it is set");
1387 else if (value == null) b.redirect = null;
1389 Box b2 = (Box)value;
1390 while(b2 != b && b2 != null) b2 = b2.parent;
1391 if (b2 == null) Log.log(this, "a box's redirect must be one of its descendants");
1392 b.redirect = (Box)value;
1395 public Object get(Box b) {
1396 if (b.redirect == null) return null;
1397 if (b.redirect == b) return Boolean.TRUE;
1398 return get(b.redirect);
1402 // FEATURE: this still isn't totally harmonious; when you createSurface, these aren't checked
1403 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1404 public void put(Box b, Object value) {
1405 if (b.surface != null) b.surface.setTitleBarText(value.toString());
1406 b.put_("titlebar", value);
1408 public Object get(Box b) { return b.get_("titlebar"); }
1411 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1414 public void put(Box b, Object value) {
1415 b.put_("icon", value);
1416 if (b.surface == null) return;
1417 Picture pic = ImageDecoder.getPicture(value.toString());
1418 if (pic != null) b.surface.setIcon(pic);
1419 else if (Log.on) Log.log(this, "unable to load icon " + value);
1421 public Object get(Box b) { return b.get_("icon"); }
1426 private static abstract class ColorBoxProperty extends SpecialBoxProperty {
1427 public abstract int getColor(Box b);
1428 public abstract void putColor(Box b, int argb);
1429 public Object get(Box b) {
1430 if ((getColor(b) & 0xFF000000) == 0) return null;
1431 String red = Integer.toHexString((getColor(b) & 0x00FF0000) >> 16);
1432 String green = Integer.toHexString((getColor(b) & 0x0000FF00) >> 8);
1433 String blue = Integer.toHexString(getColor(b) & 0x000000FF);
1434 if (red.length() < 2) red = "0" + red;
1435 if (blue.length() < 2) blue = "0" + blue;
1436 if (green.length() < 2) green = "0" + green;
1437 return "#" + red + green + blue;
1439 public void put(Box b, Object value) {
1440 int newcolor = getColor(b);
1441 String s = value == null ? null : value.toString();
1442 if (value == null) newcolor = 0x00000000;
1443 else if (s.length() > 0 && s.charAt(0) == '#')
1445 newcolor = 0xFF000000 |
1446 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
1447 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
1448 Integer.parseInt(s.substring(5, 7), 16);
1449 } catch (NumberFormatException e) {
1450 Log.log(this, "invalid color " + s);
1453 else if (org.xwt.translators.SVG.colors.get(s) != null)
1454 newcolor = 0xFF000000 | ((Integer)org.xwt.translators.SVG.colors.get(s)).intValue();
1455 if (newcolor == getColor(b)) return;
1456 putColor(b, newcolor);
1462 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1463 public static boolean stob(Object o) {
1464 if (o == null) return false;
1465 return Boolean.TRUE.equals(o) || "true".equals(o);
1468 /** helper that converts a String to an int according to JavaScript coercion rules */
1469 public static int stoi(Object o) {
1470 if (o == null) return 0;
1471 if (o instanceof Integer) return ((Integer)o).intValue();
1474 if (!(o instanceof String)) s = o.toString();
1477 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1478 catch (NumberFormatException e) { return 0; }