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 final int MOUSEINSIDE_FLAG = 0x00000001;
88 static final int INVISIBLE_FLAG = 0x00000002;
89 static final int NOTPACKED_FLAG = 0x00000004;
90 static final int HSHRINK_FLAG = 0x00000008;
91 static final int VSHRINK_FLAG = 0x00000010;
92 static final int FONT_CHANGED_FLAG = 0x00000040; // set when font changes, cleared during repack
93 static final int ISROOT_FLAG = 0x00000080;
94 static final int NOCLIP_FLAG = 0x00000100;
95 static final int ALIGN_TOP_FLAG = 0x00001000;
96 static final int ALIGN_BOTTOM_FLAG = 0x00002000;
97 static final int ALIGN_LEFT_FLAG = 0x00004000;
98 static final int ALIGN_RIGHT_FLAG = 0x00008000;
99 static final int ALIGN_FLAGS = 0x0000f000;
102 // Geometry ////////////////////////////////////////////////////////////////////////////
104 // xwt can be compiled with 16-bit lengths to save memory on small devices
106 //#define MAX_LENGTH Integer.MAX_VALUE
107 //#define MIN_LENGTH Integer.MIN_VALUE
109 // always correct (set directly by user)
110 public LENGTH minwidth = 0;
111 public LENGTH minheight = 0;
112 public LENGTH maxwidth = MAX_LENGTH;
113 public LENGTH maxheight = MAX_LENGTH;
114 private String text = null;
115 private Res font = null;
116 private int fontsize = 10;
117 private LENGTH textwidth = 0;
118 private LENGTH textheight = 0;
120 // FEATURE: use shorts
121 private int rows = 1;
122 private int cols = 0;
123 private int rowspan = 1;
124 private int colspan = 1;
126 // computed during reflow
129 public LENGTH width = 0;
130 public LENGTH height = 0;
131 private int row = 0; // FEATURE use a short
132 private int col = 0; // FEATURE use a short
133 private LENGTH contentwidth = 0; // == max(minwidth, textwidth, sum(child.contentwidth))
134 private LENGTH contentheight = 0;
135 private LENGTH offset_x = 0;
136 private LENGTH offset_y = 0;
139 // Rendering Properties ///////////////////////////////////////////////////////////
141 private VectorGraphics.VectorPath path = null;
142 private VectorGraphics.Affine transform = null;
144 private VectorGraphics.RasterPath rpath = null;
145 private VectorGraphics.Affine rtransform = null;
147 //private VectorGraphics.Paint fill = null;
148 //private VectorGraphics.Paint stroke = null;
151 public Picture image; // will disappear
152 private int fillcolor = 0x00000000; // will become VectorGraphics.Paint
153 private int strokecolor = 0xFF000000; // will become VectorGraphics.Paint
154 private int textcolor = 0xFF000000; // will become VectorGraphics.Paint
156 private String cursor = null; // the cursor for this box
159 // Instance Methods /////////////////////////////////////////////////////////////////////
161 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
162 public final void dirty() {
163 if ((flags & NOCLIP_FLAG) != 0 && parent != null) parent.dirty();
164 else dirty(0, 0, width, height);
166 public final void dirty(int x, int y, int w, int h) {
167 for(Box cur = this; cur != null; cur = cur.parent) {
168 if ((flags & NOCLIP_FLAG) == 0) {
169 w = min(x + w, cur.width) - max(x, 0);
170 h = min(y + h, cur.height) - max(y, 0);
174 if (w <= 0 || h <= 0) return;
175 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
182 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
183 * to see if this node requires any Enter, Leave, or Move notifications.
185 * @param forceleave set to true by the box's parent if the mouse is inside an older
186 * sibling, which is covering up this box.
188 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
189 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
191 boolean wasinside = (flags & MOUSEINSIDE_FLAG) != 0;
192 boolean isinside = !((flags & INVISIBLE_FLAG) != 0) && inside(mousex, mousey) && !forceleave;
193 if (isinside) flags |= MOUSEINSIDE_FLAG; else flags &= ~MOUSEINSIDE_FLAG;
195 if (!wasinside && !isinside) return;
197 if (!wasinside && isinside && get("Enter", Trap.class) != null) put("Enter", Boolean.TRUE);
198 else if (wasinside && !isinside && get("Leave", Trap.class) != null) put("Leave", Boolean.TRUE);
199 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && get("Move", Trap.class) != null)
200 put("Move", Boolean.TRUE);
202 if (isinside && cursor != null) getRoot().cursor = cursor;
204 // if the mouse has moved into our padding region, it is considered 'outside' all our children
205 if (!(mousex >= 0 && mousey >= 0 && mousex < width && mousey < height)) forceleave = true;
207 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
208 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
209 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
214 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
216 /** only for use on the root box */
217 void reflow() { reflow(width, height); }
218 void reflow(int new_width, int new_height) {
220 if (Surface.abort) return;
221 //#repeat width/height new_width/new_height HSHRINK_FLAG/VSHRINK_FLAG contentwidth/contentheight minwidth/minheight maxwidth/maxheight
222 new_width = bound(max(contentwidth, minwidth),
224 ((flags & HSHRINK_FLAG) != 0) ? max(contentwidth, minwidth) : maxwidth);
226 resize(x, y, new_width, new_height);
229 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
231 if (!needs_reflow) return;
232 contentwidth = contentheight = 0;
234 // --- Phase 0 ----------------------------------------------------------------------
236 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
237 if (((flags & FONT_CHANGED_FLAG) != 0) && child.font == null) {
238 child.flags |= FONT_CHANGED_FLAG;
239 child.needs_reflow = true;
242 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
244 flags &= ~FONT_CHANGED_FLAG;
246 // --- Phase 1 ----------------------------------------------------------------------
247 // assign children to their row/column positions (assuming constrained columns)
248 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
249 //#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 colMaxWidth/rowMaxHeight OUTER/OUTER2 INNER/INNER2
250 if (rows == 0 && numChildren() != 0) {
251 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
252 Box child = getChild(0);
253 for(; child != null && (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
254 OUTER: for(int row=0; child != null; row++) {
255 for(int col=0; child != null && col < cols;) {
256 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
257 while(col < cols && numRowsInCol[col] > row) col++;
258 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
259 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
262 if (col + min(cols, child.colspan) > cols) break;
263 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
266 col += min(cols, child.colspan);
267 child = child.nextSibling();
268 for(; child != null && (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
274 // --- Phase 2 ----------------------------------------------------------------------
275 // compute the min/max sizes of the columns and rows and set our contentwidth
276 //#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 textwidth/textheight
277 if (numChildren() != 0) {
280 for(Box child = getChild(0); child != null; child = child.nextSibling())
281 numCols = max(numCols, child.col + child.colspan);
282 LENGTH[] colWidth = new LENGTH[numCols];
283 for(Box child = getChild(0); child != null; child = child.nextSibling())
284 if (!(((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
285 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
286 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
288 contentwidth = max(textwidth, contentwidth);
289 contentwidth = bound(minwidth, contentwidth, maxwidth);
293 if ((flags & NOTPACKED_FLAG) == 0) {
295 if (rpath == null) rpath = path.realize(transform == null ? VectorGraphics.Affine.identity() : transform);
296 if ((flags & HSHRINK_FLAG) != 0) contentwidth = max(contentwidth, rpath.boundingBoxWidth());
297 if ((flags & VSHRINK_FLAG) != 0) contentheight = max(contentheight, rpath.boundingBoxHeight());
298 // FIXME: separate offset_x needed for the path
300 //#repeat x1/y1 x2/y2 x3/y3 x4/y4 contentwidth/contentheight left/top right/bottom
301 int x1 = transform == null ? 0 : (int)transform.multiply_px(0, 0);
302 int x2 = transform == null ? 0 : (int)transform.multiply_px(contentwidth, 0);
303 int x3 = transform == null ? contentwidth : (int)transform.multiply_px(contentwidth, contentheight);
304 int x4 = transform == null ? contentwidth : (int)transform.multiply_px(0, contentheight);
305 int left = min(min(x1, x2), min(x3, x4));
306 int right = max(max(x1, x2), max(x3, x4));
307 contentwidth = max(contentwidth, right - left);
308 offset_x = -1 * left;
314 private void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
316 // --- Phase 1 ----------------------------------------------------------------------
317 // run PosChange/SizeChange, dirty as needed
318 if (x != this.x || y != this.y || width != this.width || height != this.height) {
319 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
320 boolean sizechange = false, poschange = false;
321 if ((this.width != width || this.height != height) && get("SizeChange", Trap.class) != null) sizechange = true;
322 if ((this.x != x || this.y != y) && get("PosChange", Trap.class) != null) poschange = true;
323 this.width = width; this.height = height; this.x = x; this.y = y;
325 if (sizechange || poschange)
326 if (sizePosChangesSinceLastRender == 500) {
327 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
329 sizePosChangesSinceLastRender++;
330 try { if (sizechange) put("SizeChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
331 try { if (poschange) put("PosChange", Boolean.TRUE); } catch (Exception e) { Log.log(this, e); }
332 Surface.abort = true;
338 // --- short circuit ----------------------------------------------------------------
339 if (!needs_reflow) return;
340 needs_reflow = false;
341 if (numChildren() == 0) return;
343 // --- Phase 2 ----------------------------------------------------------------------
344 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
346 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight marginWidth/marginHeight numCols/numRows
349 for(Box child = getChild(0); child != null; child = child.nextSibling())
350 numCols = max(numCols, child.col + child.colspan);
351 LENGTH[] colWidth = new LENGTH[numCols];
352 LENGTH[] colMaxWidth = new LENGTH[numCols];
353 int marginWidth = width;
354 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
357 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
358 if (((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)) continue;
359 //#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
360 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
361 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
362 colMaxWidth[i] = max(colMaxWidth[i], (((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) / child.colspan);
366 //#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
367 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
369 for(int i=0; i<colMaxWidth.length; i++) {
370 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
371 marginWidth -= colMaxWidth[i];
372 if (marginWidth < 0) { marginWidth = 0; break; }
377 // --- Phase 3 ----------------------------------------------------------------------
378 // hand out the slack
380 //#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
382 for(int i=0; i<numCols; i++) slack -= colWidth[i];
383 if (numChildren() > 0)
385 // FEATURE: inefficient
386 int startslack = slack;
387 int increment = max(1, slack / numCols);
388 for(int col=0; col < numCols && slack > 0; col++) {
389 slack += colWidth[col];
390 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
391 slack -= colWidth[col];
393 if (slack == startslack) break;
398 // --- Phase 4 ----------------------------------------------------------------------
399 // assign children's new sizes and positions and recurse
400 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
401 if ((child.flags & INVISIBLE_FLAG) != 0) continue;
402 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
403 if ((child.flags & NOTPACKED_FLAG) != 0) {
406 child_width = ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : min(child.maxwidth, width - child.x);
407 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y);
410 //#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 ALIGN_LEFT_FLAG/ALIGN_TOP_FLAG ALIGN_RIGHT_FLAG/ALIGN_BOTTOM_FLAG
411 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
412 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
413 if (transform == null) {
414 if ((flags & ALIGN_RIGHT_FLAG) != 0) child_x = marginWidth;
415 else if ((flags & ALIGN_LEFT_FLAG) == 0) child_x = marginWidth / 2;
417 for(int i=0; i<child.col; i++) child_x += colWidth[i];
418 if (diff < 0) child_x += -1 * (diff / 2);
422 child.resize(child_x, child_y, child_width, child_height);
429 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
431 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
432 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf, VectorGraphics.Affine a) {
433 if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
434 int globalx = parentx + (parent == null ? 0 : x);
435 int globaly = parenty + (parent == null ? 0 : y);
437 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
439 if ((flags & NOCLIP_FLAG) == 0) {
440 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw, (parent == null ? 0 : globalx) + width) - globalx;
441 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph, (parent == null ? 0 : globaly) + height) - globaly;
442 clipx = max(clipx, parent == null ? 0 : globalx);
443 clipy = max(clipy, parent == null ? 0 : globaly);
445 if (clipw <= 0 || cliph <= 0) return;
447 if (path == null && (fillcolor & 0xFF000000) != 0x00000000) {
448 VectorGraphics.VectorPath path2 = VectorGraphics.parseVectorPath("M0,0H" + width + "V" + height + "H0V0z");
449 path2.realize(a).fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
453 for(int x = globalx; x < clipx + clipw; x += image.getWidth())
454 for(int y = globaly; y < clipy + cliph; y += image.getHeight())
455 buf.drawPicture(image, x, y, clipx, clipy, clipx + clipw, clipy + cliph);
457 if (text != null && !text.equals(""))
458 Glyph.rasterizeGlyphs(font, fontsize, text, buf, textcolor, globalx, globaly, clipx, clipy, clipw + clipx, clipy + cliph,
459 new Callback() { public Object call(Object arg) {
468 if (rtransform == null) rpath = null;
469 else if (!rtransform.equalsIgnoringTranslation(a)) rpath = null;
471 rpath.translate((int)(a.e - rtransform.e), (int)(a.f - rtransform.f));
472 rtransform = a.copy();
476 rpath = path.realize(a == null ? VectorGraphics.Affine.identity() : a);
478 if ((strokecolor & 0xff000000) != 0) rpath.stroke(buf, 1, strokecolor);
479 if ((fillcolor & 0xff000000) != 0) rpath.fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
482 for(Box b = getChild(0); b != null; b = b.nextSibling()) {
483 VectorGraphics.Affine a2 = VectorGraphics.Affine.translate(b.x, b.y);
484 if (transform != null) a2.multiply(transform);
485 a2.multiply(VectorGraphics.Affine.translate(offset_x, offset_y));
487 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf, a2);
492 // Methods to implement org.xwt.js.JS //////////////////////////////////////
494 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
495 if ("indexof".equals(method)) {
496 if (checkOnly) return Boolean.TRUE;
497 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
498 Box b = (Box)args.elementAt(0);
499 if (b.parent != Box.this) {
500 if (redirect == null || redirect == Box.this) return new Integer(-1);
501 return redirect.callMethod(method, args, checkOnly);
503 return new Integer(b.getIndexInParent());
508 /** Returns the i_th child */
509 public Object get(int i) {
510 if (redirect == null) return null;
511 if (redirect != this) return redirect.get(i);
512 return i >= numChildren() || i < 0 ? null : getChild(i);
516 * Inserts value as child i; calls remove() if necessary.
517 * This method handles "reinserting" one of your children properly.
518 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
519 * WARNING: O(n) runtime, unless i == numChildren()
521 public void put(int i, Object value) {
523 Trap t = value == null ? (Trap)get("childremoved", Trap.class) : (Trap)get("childadded", Trap.class);
525 if (value != null && !(value instanceof Box)) {
526 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to a non-box");
530 if (redirect == null) {
531 if (t != null) t.perform(value);
532 else if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
533 } else if (redirect != this) {
534 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
535 redirect.put(i, value);
536 if (t != null) t.perform(value);
537 } else if (value == null) {
538 if (i >= 0 && i < numChildren()) {
541 if (t != null) t.perform(b);
544 Box newnode = (Box)value;
546 // check if box being moved is currently target of a redirect
547 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
548 if (cur.redirect == newnode) {
549 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
553 // check for recursive ancestor violation
554 for(Box cur = this; cur != null; cur = cur.parent)
555 if (cur == newnode) {
556 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
557 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
561 if (numKids > 15 && children == null) convert_to_array();
563 newnode.parent = this;
565 if (children == null) {
566 if (firstKid == null) {
568 newnode.prevSibling = newnode;
569 newnode.nextSibling = newnode;
570 } else if (i >= numKids) {
571 newnode.prevSibling = firstKid.prevSibling;
572 newnode.nextSibling = firstKid;
573 firstKid.prevSibling.nextSibling = newnode;
574 firstKid.prevSibling = newnode;
577 for(int j=0; j<i; j++) cur = cur.nextSibling;
578 newnode.prevSibling = cur.prevSibling;
579 newnode.nextSibling = cur;
580 cur.prevSibling.nextSibling = newnode;
581 cur.prevSibling = newnode;
582 if (i == 0) firstKid = newnode;
587 if (i >= children.size()) {
588 newnode.indexInParent = children.size();
589 children.addElement(newnode);
591 children.insertElementAt(newnode, i);
592 for(int j=i; j<children.size(); j++)
593 getChild(j).indexInParent = j;
597 // need both of these in case child was already uncalc'ed
600 MARK_FOR_REFLOW_this;
603 if (t != null) t.perform(b);
607 public Object get(Object key, Object key2) { return super.get(key, key2); }
608 public void put(Object key, Object key2, Object val) { super.put(key, key2, val); }
610 public Object get_(Object name) { return super.get(name); }
611 public Object get(Object name) { return get(name, false); }
612 public Object get(Object name_, boolean ignoretraps) {
613 if (name_ instanceof Number) return get(((Number)name_).intValue());
615 if (!(name_ instanceof String)) return null;
616 String name = (String)name_;
617 if (name.equals("")) return null;
619 // See if we're triggering a trap
620 Trap t = ignoretraps ? (Trap)null : (Trap)get(name, Trap.class);
621 if (t != null) return t.perform();
623 // Check for a special handler
624 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
625 if (gph != null) return gph.get(this);
627 Object ret = super.get(name);
628 if (name.startsWith("$") && ret == null)
629 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" +
630 name.substring(1) + "\" found");
634 public Object[] keys() {
635 Object[] ret = new Object[numChildren()];
636 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
640 public void addTrap(Object name, Object value) { Trap.addTrap(this, name, ((JS.CompiledFunction)value)); }
641 public void delTrap(Object name, Object value) { Trap.delTrap(this, name, ((JS.CompiledFunction)value)); }
646 * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack)
647 * @param rp if this put is being performed via a root proxy, rp is the root proxy.
649 public void put_(Object name, Object value) { super.put(name, value); }
650 public void put(Object name, Object value) { put(name, value, false); }
651 public void put(Object name_, Object value, boolean ignoretraps) {
652 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
653 if (!(name_ instanceof String)) { super.put(name_,value); return; }
654 String name = name_.toString();
656 Trap t = (Trap)get(name, Trap.class);
663 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
664 if (gph != null) { gph.put(name, this, value); return; }
666 super.put(name, value);
670 // Tree Manipulation /////////////////////////////////////////////////////////////////////
672 /** The parent of this node */
673 private Box parent = null;
675 // Variables used in Vector mode */
676 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
677 private int indexInParent;
678 private Vec children = null;
680 // Variables used in linked-list mode
681 private int numKids = 0;
682 private Box nextSibling = null;
683 private Box prevSibling = null;
684 private Box firstKid = null;
686 // when we get more than 15 children, we switch to array-mode
687 private void convert_to_array() {
688 children = new Vec(numKids);
691 children.addElement(cur);
692 cur.indexInParent = children.size() - 1;
693 cur = cur.nextSibling;
694 } while (cur != firstKid);
697 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
698 public void remove() {
699 if (parent == null) {
700 if (surface != null) surface.dispose(true);
703 Box oldparent = parent;
704 if (oldparent == null) return;
705 MARK_FOR_REFLOW_this;
707 flags &= ~MOUSEINSIDE_FLAG;
709 if (parent.children != null) {
710 parent.children.removeElementAt(indexInParent);
711 for(int j=indexInParent; j<parent.children.size(); j++)
712 (parent.getChild(j)).indexInParent = j;
715 if (parent.firstKid == this) {
716 if (nextSibling == this) parent.firstKid = null;
717 else parent.firstKid = nextSibling;
720 prevSibling.nextSibling = nextSibling;
721 nextSibling.prevSibling = prevSibling;
727 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
729 // note that JavaScript box[0] will invoke put(int i), not put(String s)
730 if (oldparent != null) {
731 Trap t = (Trap)oldparent.get("childremoved", Trap.class);
732 if (t != null) t.perform(this);
736 /** returns our next sibling (parent[ourindex + 1]) */
737 public final Box nextSibling() {
738 if (parent == null) return null;
739 if (parent.children == null) {
740 if (nextSibling == parent.firstKid) return null;
743 if (indexInParent >= parent.children.size() - 1) return null;
744 return (Box)parent.children.elementAt(indexInParent + 1);
748 /** returns our next sibling (parent[ourindex + 1]) */
749 public final Box prevSibling() {
750 if (parent == null) return null;
751 if (parent.children == null) {
752 if (this == parent.firstKid) return null;
755 if (indexInParent == 0) return null;
756 return (Box)parent.children.elementAt(indexInParent - 1);
760 /** Returns the parent of this node */
761 public Box getParent() { return parent; }
763 /** Returns ith child */
764 public Box getChild(int i) {
765 if (children == null) {
766 if (firstKid == null) return null;
767 if (i >= numKids) return null;
768 if (i == numKids - 1) return firstKid.prevSibling;
770 for(int j=0; j<i; j++) cur = cur.nextSibling;
773 if (i >= children.size() || i < 0) return null;
774 return (Box)children.elementAt(i);
778 /** Returns the number of children */
779 public int numChildren() {
780 if (children == null) {
781 if (firstKid == null) return 0;
783 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
786 return children.size();
790 /** Returns our index in our parent */
791 public int getIndexInParent() {
792 if (parent == null) return 0;
793 if (parent.children == null) {
795 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
798 return indexInParent;
802 /** returns the root of the surface that this box belongs to */
803 public final Box getRoot() {
804 if (parent == null && surface != null) return this;
805 if (parent == null) return null;
806 return parent.getRoot();
809 public void recompute_font() {
810 if (text == null) { textwidth = textheight = 0; return; }
811 if (font == null) { /* FIXME */ }
812 long widthheight = Glyph.rasterizeGlyphs(font, fontsize, text, null, textcolor, 0, 0, 0, 0, 0, 0,
813 new Callback() { public Object call(Object arg) {
820 if (widthheight == -1) return;
821 textwidth = (int)((widthheight & 0xffff0000) >> 16);
822 textheight = (int)(widthheight & 0x0000ffff);
823 MARK_FOR_REFLOW_this;
827 // Trivial Helper Method s(should be inlined) /////////////////////////////////////////
829 static final int min(int a, int b) { if (a<b) return a; else return b; }
830 static final float min(float a, float b) { if (a<b) return a; else return b; }
831 static final double min(double a, double b) { if (a<b) return a; else return b; }
832 static final int max(int a, int b) { if (a>b) return a; else return b; }
833 static final float max(float a, float b) { if (a>b) return a; else return b; }
834 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; }
835 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; }
836 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
837 final boolean inside(int x, int y) { return (!((flags & INVISIBLE_FLAG) != 0) && x >= 0 && y >= 0 && x < width && y < height); }
839 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
840 public static Box whoIs(Box cur, int x, int y) {
842 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
846 // WARNING: this method is called from the event-queueing
847 // thread -- it may run concurrently with ANY part of XWT, and
848 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
851 if ((cur.flags & INVISIBLE_FLAG) != 0) return null;
852 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
854 for(int i=cur.numChildren() - 1; i>=0; i--) {
855 Box child = cur.getChild(i);
856 if (child == null) continue; // since this method is unsynchronized, we have to double-check
859 if (!((child.flags & INVISIBLE_FLAG) != 0) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
869 * A helper class for properties of Box which require special
872 * To avoid excessive use of String.equals(), the Box.get() and
873 * Box.put() methods employ a Hash keyed on property names that
874 * require special handling. The value stored in the Hash is an
875 * instance of an anonymous subclass of SpecialBoxProperty, which knows
876 * how to handle get()s and put()s for that property name. There
877 * should be one anonymous subclass of SpecialBoxProperty for each
878 * specially-handled property on Box.
880 static class SpecialBoxProperty {
882 SpecialBoxProperty() { }
884 /** stores instances of SpecialBoxProperty; keyed on property name */
885 static Hash specialBoxProperties = new Hash(200, 3);
887 /** this method defines the behavior when the property is get()ed from b */
888 Object get(Box b) { return null; }
890 /** this method defines the behavior when the property is put() to b */
891 void put(Box b, Object value) { }
893 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
894 void put(String name, Box b, Object value) { put(b, value); }
897 specialBoxProperties.put("fill", new ColorBoxProperty() {
898 public void put(final Box b, final Object value) {
899 if (value == null || !(value instanceof Res)) super.put(b, value);
900 else Picture.fromRes((Res)value, new Callback() { public Object call(Object pic) {
901 if (pic == b.image) return null;
902 b.image = (Picture)pic;
903 b.minwidth = b.image.getWidth();
904 b.minheight = b.image.getHeight();
910 public int getColor(Box b) { return b.fillcolor; }
911 public void putColor(Box b, int argb) { b.fillcolor = argb; }
913 specialBoxProperties.put("textcolor", new ColorBoxProperty() {
914 public int getColor(Box b) { return b.textcolor; }
915 public void putColor(Box b, int argb) { b.textcolor = argb; }
917 specialBoxProperties.put("strokecolor", new ColorBoxProperty() {
918 public int getColor(Box b) { return b.strokecolor; }
919 public void putColor(Box b, int argb) { b.strokecolor = argb; }
922 specialBoxProperties.put("text", new SpecialBoxProperty() {
923 public Object get(Box b) { return b.text; }
924 public void put(Box b, Object value) {
925 String t = value == null ? "null" : value.toString();
926 if (t.equals(b.text)) return;
932 specialBoxProperties.put("path", new SpecialBoxProperty() {
933 public Object get(Box b) { throw new JS.Exn("cannot read from the path property"); }
934 public void put(Box b, Object value) {
935 String t = value == null ? "null" : value.toString();
936 b.path = value == null ? null : VectorGraphics.parseVectorPath(value.toString());
941 specialBoxProperties.put("transform", new SpecialBoxProperty() {
942 public void put(Box b, Object value) {
943 String t = value.toString().trim();
944 b.transform = VectorGraphics.Affine.identity();
945 while (t.length() > 0) {
946 if (t.startsWith("skewX(")) {
949 } else if (t.startsWith("shear(")) {
950 // FIXME: nonstandard; remove this
951 b.transform.multiply(VectorGraphics.Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
953 } else if (t.startsWith("skewY(")) {
956 } else if (t.startsWith("rotate(")) {
957 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
958 if (sub.indexOf(',') != -1) {
959 float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
960 sub = sub.substring(sub.indexOf(',') + 1);
961 float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
962 sub = sub.substring(sub.indexOf(',') + 1);
963 float cy = Float.parseFloat(sub);
964 b.transform.multiply(VectorGraphics.Affine.translate(cx, cy));
965 b.transform.multiply(VectorGraphics.Affine.rotate(angle));
966 b.transform.multiply(VectorGraphics.Affine.translate(-1 * cx, -1 * cy));
968 b.transform.multiply(VectorGraphics.Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
971 } else if (t.startsWith("translate(")) {
972 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
973 if (sub.indexOf(',') > -1) {
974 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
975 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
977 b.transform.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
980 } else if (t.startsWith("flip(")) {
981 String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
982 b.transform.multiply(VectorGraphics.Affine.flip(which.equals("horizontal"), which.equals("vertical")));
984 } else if (t.startsWith("scale(")) {
985 String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
986 if (sub.indexOf(',') > -1) {
987 b.transform.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
988 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
990 b.transform.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
991 Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
994 } else if (t.startsWith("matrix(")) {
995 // FIXME: is this mapped right?
996 float d[] = new float[6];
997 StringTokenizer st = new StringTokenizer(t, ",", false);
998 for(int i=0; i<6; i++)
999 d[i] = Float.parseFloat(st.nextToken());
1000 b.transform.multiply(new VectorGraphics.Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
1002 t = t.substring(t.indexOf(')') + 1).trim();
1007 public Object get(Box b) { return b.transform.toString(); }
1010 specialBoxProperties.put("font", new SpecialBoxProperty() {
1011 public Object get(Box b) { return b.font; }
1012 public void put(Box b, Object value) {
1013 // FIXME: translate value into a resource if it is a string
1014 b.font = value == null ? null : (Res)value;
1016 b.flags |= FONT_CHANGED_FLAG;
1021 specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
1022 public Object get(Box b) { return b.font; }
1023 public void put(Box b, Object value) {
1024 if (b.fontsize == stoi(value)) return;
1025 b.fontsize = stoi(value);
1027 b.flags |= FONT_CHANGED_FLAG;
1032 specialBoxProperties.put("strokewidth", new SpecialBoxProperty() {
1033 public Object get(Box b) { return new Integer(b.strokewidth); }
1034 public void put(Box b, Object value) {
1035 if (b.strokewidth == stoi(value)) return;
1036 b.strokewidth = stoi(value);
1040 specialBoxProperties.put("align", new SpecialBoxProperty() {
1041 public Object get(Box b) {
1042 switch(b.flags & ALIGN_FLAGS) {
1043 case (ALIGN_TOP_FLAG | ALIGN_LEFT_FLAG): return "topleft";
1044 case (ALIGN_BOTTOM_FLAG | ALIGN_LEFT_FLAG): return "bottomleft";
1045 case (ALIGN_TOP_FLAG | ALIGN_RIGHT_FLAG): return "topright";
1046 case (ALIGN_BOTTOM_FLAG | ALIGN_RIGHT_FLAG): return "bottomright";
1047 case ALIGN_TOP_FLAG: return "top";
1048 case ALIGN_BOTTOM_FLAG: return "bottom";
1049 case ALIGN_LEFT_FLAG: return "left";
1050 case ALIGN_RIGHT_FLAG: return "right";
1051 case 0: return "center";
1052 default: throw new Error("invalid alignment flags: " + (b.flags & ALIGN_FLAGS));
1055 public void put(Box b, Object value) {
1056 b.flags &= ~ALIGN_FLAGS;
1057 if (value == null || value.equals("center")) { b.flags &= ALIGN_FLAGS; }
1058 else if (value.equals("topleft")) { b.flags |= ALIGN_TOP_FLAG | ALIGN_LEFT_FLAG; }
1059 else if (value.equals("bottomleft")) { b.flags |= ALIGN_BOTTOM_FLAG | ALIGN_LEFT_FLAG; }
1060 else if (value.equals("topright")) { b.flags |= ALIGN_TOP_FLAG | ALIGN_RIGHT_FLAG; }
1061 else if (value.equals("bottomright")) { b.flags |= ALIGN_BOTTOM_FLAG | ALIGN_RIGHT_FLAG; }
1062 else if (value.equals("top")) { b.flags |= ALIGN_TOP_FLAG; }
1063 else if (value.equals("bottom")) { b.flags |= ALIGN_BOTTOM_FLAG; }
1064 else if (value.equals("left")) { b.flags |= ALIGN_LEFT_FLAG; }
1065 else if (value.equals("right")) { b.flags |= ALIGN_RIGHT_FLAG; }
1066 else Log.logJS("invalid alignment specifier \"" + value + "\"");
1071 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1072 public Object get(Box b) { return b; }
1073 public void put(Box b, Object value) {
1074 if (value == null) b.remove();
1075 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1079 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1080 public Object get(Box b) {
1081 Log.log(this, "warning: the orient property is deprecated");
1082 if (b.redirect == null) return "horizontal";
1083 else if (b.redirect != b) return get(b.redirect);
1084 else if (b.cols == 1) return "vertical";
1085 else if (b.rows == 1) return "horizontal";
1088 public void put(Box b, Object value) {
1089 Log.log(this, "warning: the orient property is deprecated");
1090 if (value == null) return;
1091 if (b.redirect == null) return;
1092 if (b.redirect != b) { put(b.redirect, value); return; }
1093 if (value.equals("vertical")) {
1094 if (b.rows == 0) return;
1095 b.rows = 0; b.cols = 1;
1096 } else if (value.equals("horizontal")) {
1097 if (b.cols == 0) return;
1098 b.cols = 0; b.rows = 1;
1100 Log.log(this, "invalid value put to orient property: " + value);
1104 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1105 public Object get(Box b) { return (((b.flags & HSHRINK_FLAG) != 0) || ((b.flags & VSHRINK_FLAG) != 0)) ? Boolean.TRUE : Boolean.FALSE; }
1106 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1109 //#repeat hshrink/vshrink HSHRINK_FLAG/VSHRINK_FLAG
1110 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1111 public Object get(Box b) { return new Boolean((b.flags & HSHRINK_FLAG) != 0); }
1112 public void put(Box b, Object value) {
1113 boolean newshrink = stob(value);
1114 if (((b.flags & HSHRINK_FLAG) != 0) == newshrink) return;
1115 if (newshrink) b.flags |= HSHRINK_FLAG; else b.flags &= ~HSHRINK_FLAG;
1122 specialBoxProperties.put("x", new SpecialBoxProperty() {
1123 public Object get(Box b) {
1124 if (b.surface == null) return new Integer(0);
1125 if ((b.flags & INVISIBLE_FLAG) != 0) return new Integer(0);
1126 return new Integer(b.x);
1128 public void put(Box b, Object value) {
1129 if (!((b.flags & NOTPACKED_FLAG) != 0) && (b.parent != null || b.surface == null)) return;
1130 int x = stoi(value);
1131 if (x == b.x) return;
1134 if (b.parent == null && b.surface != null) b.surface.setLocation();
1141 //#repeat width/height height/width minwidth/minheight maxwidth/maxheight true/false
1142 specialBoxProperties.put("width", new SpecialBoxProperty() {
1143 public Object get(Box b) { return new Integer(b.width); }
1144 public void put(Box b, Object value) {
1145 int newval = stoi(value);
1146 if (b.parent == null && b.surface != null) {
1147 // hack to circumvent #repeat
1148 int other = b.height;
1149 if (true) b.surface.setSize(newval, other);
1150 else b.surface.setSize(other, newval);
1153 if (b.minwidth == newval && b.maxwidth == newval) return;
1154 b.minwidth = b.maxwidth = newval;
1160 //#repeat cols/rows rows/cols
1161 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1162 public Object get(Box b) { return new Double(b.cols); }
1163 public void put(Box b, Object value) {
1164 if (b.cols == stoi(value)) return;
1165 b.cols = stoi(value);
1166 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1167 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1172 //#repeat colspan/rowspan
1173 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1174 public Object get(Box b) { return new Double(b.colspan); }
1175 public void put(Box b, Object value) {
1176 if (b.colspan == stoi(value)) return;
1177 b.colspan = stoi(value);
1183 specialBoxProperties.put("noclip", new SpecialBoxProperty() {
1184 public Object get(Box b) { return ((b.flags & NOCLIP_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1185 public void put(Box b, Object value) {
1186 if (((b.flags & NOCLIP_FLAG) != 0) == stob(value)) return;
1187 if (stob(value)) b.flags |= NOCLIP_FLAG; else b.flags &= ~NOCLIP_FLAG;
1192 specialBoxProperties.put("visible", new SpecialBoxProperty() {
1193 public Object get(Box b) {
1194 for (Box cur = b; cur != null; cur = cur.parent) {
1195 if ((cur.flags & INVISIBLE_FLAG) != 0) return Boolean.FALSE; }
1196 return Boolean.TRUE;
1198 public void put(Box b, Object value) {
1199 if (!stob(value) == ((b.flags & INVISIBLE_FLAG) != 0)) return;
1200 if (!stob(value)) b.flags |= INVISIBLE_FLAG; else b.flags &= ~INVISIBLE_FLAG;
1201 if (b.parent == null) {
1202 if (b.surface != null) b.surface.setInvisible((b.flags & INVISIBLE_FLAG) != 0);
1205 MARK_FOR_REFLOW_b_parent;
1206 b.parent.dirty(b.x, b.y, b.width, b.height);
1210 specialBoxProperties.put("packed", new SpecialBoxProperty() {
1211 public Object get(Box b) { return ((b.flags & NOTPACKED_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1212 public void put(Box b, Object value) {
1213 if (stob(value) == ((b.flags & NOTPACKED_FLAG) == 0)) return;
1214 if (!stob(value)) b.flags |= NOTPACKED_FLAG; else b.flags &= ~NOTPACKED_FLAG;
1215 if ((b.flags & NOTPACKED_FLAG) != 0) { b.x = 0; b.y = 0; }
1216 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1219 //#repeat globalx/globaly x/y
1220 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1221 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1222 public void put(Box b, Object value) {
1223 if (b.surface == null || b.parent == null) return;
1224 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1230 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1231 public Object get(Box b) { return b.cursor; }
1232 public void put(Box b, Object value) {
1233 b.cursor = (String)value;
1234 if (b.surface == null) return;
1236 // see if we need to update the surface cursor
1237 Surface surface = b.getRoot().surface;
1238 String tempcursor = surface.cursor;
1239 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1240 if (surface.cursor != tempcursor) surface.syncCursor();
1244 //#repeat mousex/mousey x/y
1245 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1246 public Object get(Box b) {
1247 Surface surface = b.getRoot().surface;
1248 if (surface == null) return new Integer(0);
1249 int mousex = surface.mousex;
1250 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1251 return new Integer(mousex);
1256 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1257 public Object get(Box b) {
1258 return ((b.flags & MOUSEINSIDE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1261 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1262 public Object get(Box b) {
1263 if (b.redirect == null) return new Integer(0);
1264 if (b.redirect != b) return get(b.redirect);
1265 return new Integer(b.numChildren());
1268 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1269 public void put(String name, Box b, Object value) {
1270 Surface surface = b.getRoot().surface;
1271 if (surface == null) return;
1272 int mousex = surface.mousex;
1273 int mousey = surface.mousey;
1274 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1278 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1279 if (c.inside(mousex - c.x, mousey - c.y)) {
1284 if (b.parent != null) b.parent.put(name, value);
1287 specialBoxProperties.put("Press1", mouseEventHandler);
1288 specialBoxProperties.put("Press2", mouseEventHandler);
1289 specialBoxProperties.put("Press3", mouseEventHandler);
1290 specialBoxProperties.put("Release1", mouseEventHandler);
1291 specialBoxProperties.put("Release2", mouseEventHandler);
1292 specialBoxProperties.put("Release3", mouseEventHandler);
1293 specialBoxProperties.put("Click1", mouseEventHandler);
1294 specialBoxProperties.put("Click2", mouseEventHandler);
1295 specialBoxProperties.put("Click3", mouseEventHandler);
1296 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1297 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1298 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1300 specialBoxProperties.put("root", new SpecialBoxProperty() {
1301 public void put(Box b, Object value) {
1302 if (stob(value)) b.flags |= ISROOT_FLAG;
1303 else b.flags &= ~ISROOT_FLAG;
1305 public Object get(Box b) {
1306 if (b.parent == null || ((b.flags & ISROOT_FLAG) != 0)) return b;
1307 else return get(b.parent);
1310 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1311 public Object get(Box b) {
1312 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1315 public void put(Box b, Object value) {
1316 if (b.surface == null) return;
1317 boolean val = stob(value);
1318 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1322 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1323 public Object get(Box b) {
1324 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1327 public void put(Box b, Object value) {
1328 if (b.surface == null) return;
1329 boolean val = stob(value);
1330 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1334 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1335 public void put(Box b, Object value) {
1336 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1340 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1341 public void put(Box b, Object value) {
1342 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1346 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1347 public void put(Box b, Object value) {
1348 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1352 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1353 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
1354 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
1355 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1356 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1358 //#repeat minwidth/minheight maxwidth/maxheight
1359 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1360 public Object get(Box b) { return new Integer(b.minwidth); }
1361 public void put(Box b, Object value) {
1362 if (stoi(value) == b.minwidth) return;
1363 b.minwidth = stoi(value);
1367 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1368 public Object get(Box b) { return new Integer(b.maxwidth); }
1369 public void put(Box b, Object value) {
1370 if (stoi(value) == b.maxwidth) return;
1371 b.maxwidth = stoi(value);
1377 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1378 public void put(Box b, Object value) {
1379 if (b.redirect != b) Log.log(this, "cannot change the redirect of a box once it is set");
1380 else if (value == null) b.redirect = null;
1382 Box b2 = (Box)value;
1383 while(b2 != b && b2 != null) b2 = b2.parent;
1384 if (b2 == null) Log.log(this, "a box's redirect must be one of its descendants");
1385 b.redirect = (Box)value;
1388 public Object get(Box b) {
1389 if (b.redirect == null) return null;
1390 if (b.redirect == b) return Boolean.TRUE;
1391 return get(b.redirect);
1395 // FEATURE: this still isn't totally harmonious; when you createSurface, these aren't checked
1396 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1397 public void put(Box b, Object value) {
1398 if (b.surface != null) b.surface.setTitleBarText(value.toString());
1399 b.put_("titlebar", value);
1401 public Object get(Box b) { return b.get_("titlebar"); }
1404 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1407 public void put(Box b, Object value) {
1408 b.put_("icon", value);
1409 if (b.surface == null) return;
1410 Picture pic = ImageDecoder.getPicture(value.toString());
1411 if (pic != null) b.surface.setIcon(pic);
1412 else if (Log.on) Log.log(this, "unable to load icon " + value);
1414 public Object get(Box b) { return b.get_("icon"); }
1419 private static abstract class ColorBoxProperty extends SpecialBoxProperty {
1420 public abstract int getColor(Box b);
1421 public abstract void putColor(Box b, int argb);
1422 public Object get(Box b) {
1423 if ((getColor(b) & 0xFF000000) == 0) return null;
1424 String red = Integer.toHexString((getColor(b) & 0x00FF0000) >> 16);
1425 String green = Integer.toHexString((getColor(b) & 0x0000FF00) >> 8);
1426 String blue = Integer.toHexString(getColor(b) & 0x000000FF);
1427 if (red.length() < 2) red = "0" + red;
1428 if (blue.length() < 2) blue = "0" + blue;
1429 if (green.length() < 2) green = "0" + green;
1430 return "#" + red + green + blue;
1432 public void put(Box b, Object value) {
1433 int newcolor = getColor(b);
1434 String s = value == null ? null : value.toString();
1435 if (value == null) newcolor = 0x00000000;
1436 else if (s.length() > 0 && s.charAt(0) == '#')
1438 newcolor = 0xFF000000 |
1439 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
1440 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
1441 Integer.parseInt(s.substring(5, 7), 16);
1442 } catch (NumberFormatException e) {
1443 Log.log(this, "invalid color " + s);
1446 else if (org.xwt.translators.SVG.colors.get(s) != null)
1447 newcolor = 0xFF000000 | ((Integer)org.xwt.translators.SVG.colors.get(s)).intValue();
1448 if (newcolor == getColor(b)) return;
1449 putColor(b, newcolor);
1455 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1456 public static boolean stob(Object o) {
1457 if (o == null) return false;
1458 return Boolean.TRUE.equals(o) || "true".equals(o);
1461 /** helper that converts a String to an int according to JavaScript coercion rules */
1462 public static int stoi(Object o) {
1463 if (o == null) return 0;
1464 if (o instanceof Integer) return ((Integer)o).intValue();
1467 if (!(o instanceof String)) s = o.toString();
1470 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1471 catch (NumberFormatException e) { return 0; }