1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
4 // **** This file must be preprocessed before compilation ****
6 // RULE: coordinates on non-static methods are ALWAYS relative to the
7 // upper-left hand corner of <tt>this</tt>
9 // FIXME: font color, italicization, bolding, and underlining
11 // FIXME: fixed aspect
12 // FEATURE: reflow before allowing js to read from width/height
13 // FEATURE: fastpath for rows=1/cols=1
14 // FEATURE: reflow starting with a certain child
15 // FEATURE: separate mark_for_reflow and mark_for_resize
21 import org.xwt.util.*;
22 import org.xwt.translators.*;
26 * Encapsulates the data for a single XWT box as well as all layout
31 * This is the real meat of XWT. Part of its monolithic design is for
32 * performance reasons: deep inheritance heirarchies are slow, and
33 * neither javago nor GCJ can inline across class boundaries.
36 * <p>The rendering process consists of three phases; each requires
37 * one DFS pass over the tree</p>
39 * <ol><li> <b>repacking</b>: children of a box are packed into columns
40 * and rows according to their colspan/rowspan attributes and
41 * ordering. Minimum and maximum sizes of columns are computed.
43 * <li> <b>resizing</b>: width/height and x/y positions of children
44 * are assigned. If a PosChange or SizeChange is triggered,
45 * <tt>Surface.abort</tt> will be set and the resizing process will
48 * <li> <b>repainting</b>: children draw their content onto the
52 * The first two passes together are called the <i>reflow</i> phase.
54 * Reflowing is done in a seperate pass since PosChanges and
55 * SizeChanges trigger an Surface.abort; if rendering were done in the same
56 * pass, rendering work done prior to the Surface.abort would be wasted.
58 * The two passes are repack and resize. Together they are known as
59 * reflow. Repacking assigns boxes to the appropriate grid
60 * coordinates within the parents and computes
61 * contentwidth/contentheight. Resize computes actual size and
64 * A note on coordinates: the Box class represents regions
65 * internally as x,y,w,h tuples, even though the PixelBuffer class
66 * uses x1,y1,x2,y2 tuples.
68 public final class Box extends JS.Scope {
70 public Box() { super(null); }
73 // Misc instance data ////////////////////////////////////////////////////////////////
75 private static int sizePosChangesSinceLastRender = 0;
78 // Misc instance data ////////////////////////////////////////////////////////////////
80 boolean needs_reflow = true;
81 //#define MARK_FOR_REFLOW_this for(Box b2 = this; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
82 //#define MARK_FOR_REFLOW_b for(Box b2 = b; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
83 //#define MARK_FOR_REFLOW_b_parent for(Box b2 = b.parent; b2 != null && !b2.needs_reflow; b2 = b2.parent) b2.needs_reflow = true;
86 Surface surface = null; // null on all non-root boxen
88 // FEATURE: combine this with the JSObject Hash
92 // Flags ///////////////////////////////////////////////////////////////////////////////
93 static int MOUSEINSIDE_FLAG = 0x00000001;
94 static int INVISIBLE_FLAG = 0x00000002;
95 static int ABSOLUTE_FLAG = 0x00000004;
96 static int HSHRINK_FLAG = 0x00000010;
97 static int VSHRINK_FLAG = 0x00000020;
98 static int TILE_FLAG = 0x00000040;
101 * Set when the font changes, cleared during repack. If set
102 * during repack, all font==null children are marked for reflow
103 * and given the font_changed_flag. We use this flag to avoid
104 * having to iterate over all descendents of a box when its font
107 static int FONT_CHANGED_FLAG = 0x00000100;
108 static int ISROOT_FLAG = 0x00000200;
110 static int ALIGN_FLAG = 0x00000000;
111 static int FIXEDASPECT_FLAG = 0x00000000;
115 // Geometry ////////////////////////////////////////////////////////////////////////////
117 // xwt can be compiled with 16-bit lengths to save memory on small devices
119 //#define MAX_LENGTH Integer.MAX_VALUE
120 //#define MIN_LENGTH Integer.MIN_VALUE
122 // always correct (set directly by user)
124 LENGTH minheight = 0;
125 LENGTH maxwidth = MAX_LENGTH;
126 LENGTH maxheight = MAX_LENGTH;
127 private LENGTH hpad = 0;
128 private LENGTH vpad = 0;
129 private String text = null;
130 private Res font = null;
131 private int fontsize = 10;
132 private LENGTH textwidth = 0;
133 private LENGTH textheight = 0;
135 // FEATURE: use shorts
136 private int rows = 1;
137 private int cols = 0;
138 private int rowspan = 1;
139 private int colspan = 1;
141 // computed during reflow
144 public LENGTH width = 0;
145 public LENGTH height = 0;
146 private int row = 0; // FEATURE use a short
147 private int col = 0; // FEATURE use a short
148 private LENGTH contentwidth = 0; // == max(minwidth, textwidth+pad, sum(child.contentwidth) + pad)
149 private LENGTH contentheight = 0;
152 // Rendering Properties ///////////////////////////////////////////////////////////
154 //private SVG.VP path = null;
155 //private SVG.Paint fill = null;
156 //private SVG.Paint stroke = null;
158 private Picture image; // will disappear
159 private int fillcolor = 0x00000000; // will become SVG.Paint
160 private int strokecolor = 0xFF000000; // will become SVG.Paint
162 private String cursor = null; // the cursor for this box
165 // Instance Methods /////////////////////////////////////////////////////////////////////
167 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
168 public final void dirty() { dirty(0, 0, width, height); }
169 public final void dirty(int x, int y, int w, int h) {
171 for(Box cur = this; cur != null; cur = cur.parent) {
172 w = min(x + w, cur.width) - max(x, 0);
173 h = min(y + h, cur.height) - max(y, 0);
176 if (w <= 0 || h <= 0) return;
177 if (cur.parent == null && cur.surface != null) cur.surface.dirty(x, y, w, h);
183 for(cur = this; cur.parent != null; cur = cur.parent);
184 if (cur.surface != null) cur.surface.dirty(0, 0, cur.width, cur.height);
188 * Given an old and new mouse position, this will update <tt>mouseinside</tt> and check
189 * to see if this node requires any Enter, Leave, or Move notifications.
191 * @param forceleave set to true by the box's parent if the mouse is inside an older
192 * sibling, which is covering up this box.
194 void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
195 void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
197 boolean wasinside = (flags & MOUSEINSIDE_FLAG) != 0;
198 boolean isinside = !((flags & INVISIBLE_FLAG) != 0) && inside(mousex, mousey) && !forceleave;
199 if (isinside) flags |= MOUSEINSIDE_FLAG; else flags &= ~MOUSEINSIDE_FLAG;
201 if (!wasinside && !isinside) return;
203 if (traps == null) { }
204 else if (!wasinside && isinside && traps.get("Enter") != null) put("Enter", Boolean.TRUE);
205 else if (wasinside && !isinside && traps.get("Leave") != null) put("Leave", Boolean.TRUE);
206 else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && traps.get("Move") != null) put("Move", Boolean.TRUE);
208 if (isinside && cursor != null) getRoot().cursor = cursor;
210 // if the mouse has moved into our padding region, it is considered 'outside' all our children
211 if (!(mousex >= hpad && mousey >= vpad && mousex < width - hpad && mousey < height + vpad)) forceleave = true;
213 for(Box b = getChild(numChildren() - 1); b != null; b = b.prevSibling()) {
214 b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
215 if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
220 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
224 if (Surface.abort) return;
225 resize(x, y, width, height);
228 /** Checks if the Box's size has changed, dirties it if necessary, and makes sure childrens' sizes are up to date */
230 if (!needs_reflow) return;
231 if (numChildren() == 0) {
232 contentwidth = max(textwidth + 2 * hpad, minwidth);
233 contentheight = max(textheight + 2 * vpad, minheight);
237 // --- Phase 0 ----------------------------------------------------------------------
239 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
240 if (((flags & FONT_CHANGED_FLAG) != 0) && child.font == null) {
241 child.flags |= FONT_CHANGED_FLAG;
242 child.needs_reflow = true;
245 if (Surface.abort) { MARK_FOR_REFLOW_this; return; }
247 flags &= ~FONT_CHANGED_FLAG;
249 // --- Phase 1 ----------------------------------------------------------------------
250 // assign children to their row/column positions (assuming constrained columns)
251 if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + " cols == " + cols);
252 //#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
254 int[] numRowsInCol = new int[cols]; // the number of cells occupied in each column
255 Box child = getChild(0);
256 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
257 OUTER: for(int row=0; child != null; row++) {
258 for(int col=0; child != null && col < cols;) {
259 INNER: while(true) { // scan across the row, looking for an unoccupied gap at least as wide as the child
260 while(col < cols && numRowsInCol[col] > row) col++;
261 for(int i=col; i < cols && i < col + min(cols, child.colspan); i++)
262 if (numRowsInCol[col] > row) { col = i + 1; continue INNER; }
265 if (col + min(cols, child.colspan) > cols) break;
266 for(int i=col; i < col + min(cols, child.colspan); i++) numRowsInCol[i] += child.rowspan;
269 col += min(cols, child.colspan);
270 child = child.nextSibling();
271 for(; child != null && (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)); child = child.nextSibling());
277 // --- Phase 2 ----------------------------------------------------------------------
278 // compute the min/max sizes of the columns and rows and set our contentwidth
279 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight numCols/numRows hpad/vpad
280 contentwidth = 2 * hpad;
283 for(Box child = getChild(0); child != null; child = child.nextSibling())
284 numCols = max(numCols, child.col + child.colspan);
285 LENGTH[] colWidth = new LENGTH[numCols];
286 for(Box child = getChild(0); child != null; child = child.nextSibling())
287 if (!(((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
288 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
289 for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
290 contentwidth = max(textwidth + 2 * hpad, contentwidth);
291 contentwidth = bound(minwidth, contentwidth, maxwidth);
296 void resize(LENGTH x, LENGTH y, LENGTH width, LENGTH height) {
298 // --- Phase 1 ----------------------------------------------------------------------
299 // run PosChange/SizeChange, dirty as needed
300 if (x != this.x || y != this.y || width != this.width || height != this.height) {
301 (parent == null ? this : parent).dirty(this.x, this.y, this.width, this.height);
302 boolean sizechange = false, poschange = false;
303 if (traps != null && (this.width != width || this.height != height) && traps.get("SizeChange") != null) sizechange = true;
304 if (traps != null && (this.x != x || this.y != y) && traps.get("PosChange") != null) poschange = true;
305 this.width = width; this.height = height; this.x = x; this.y = y;
307 if (sizechange || poschange)
308 if (sizePosChangesSinceLastRender == 500) {
309 if (Log.on) Log.logJS(this, "Warning, more than 500 SizeChange/PosChange traps triggered since last complete render");
311 sizePosChangesSinceLastRender++;
312 if (sizechange) put("SizeChange", Boolean.TRUE);
313 if (poschange) put("PosChange", Boolean.TRUE);
314 Surface.abort = true;
320 // --- short circuit ----------------------------------------------------------------
321 if (!needs_reflow) return;
322 needs_reflow = false;
323 if (numChildren() == 0) return;
325 // --- Phase 2 ----------------------------------------------------------------------
326 // compute the min/max sizes of the columns and rows and set initial width/height to minimums
328 //#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
331 for(Box child = getChild(0); child != null; child = child.nextSibling())
332 numCols = max(numCols, child.col + child.colspan);
333 LENGTH[] colWidth = new LENGTH[numCols];
334 LENGTH[] colMaxWidth = new LENGTH[numCols];
335 int marginWidth = width;
336 for(int i=0; i<colMaxWidth.length; i++) colMaxWidth[i] = -1;
339 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
340 if (((child.flags & ABSOLUTE_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)) continue;
341 //#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
342 colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
343 for(int i=child.col; i<child.col+child.colspan && i<numCols; i++)
344 colMaxWidth[i] = max(colMaxWidth[i], (((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) / child.colspan);
348 //#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
349 for(int i=0; i<colMaxWidth.length; i++) if (colMaxWidth[i] == -1) colMaxWidth[i] = MAX_LENGTH;
351 for(int i=0; i<colMaxWidth.length; i++) {
352 if (colMaxWidth[i] == MAX_LENGTH) { marginWidth = 0; break; }
353 marginWidth -= colMaxWidth[i];
354 if (marginWidth < 0) { marginWidth = 0; break; }
359 // --- Phase 3 ----------------------------------------------------------------------
360 // hand out the slack
362 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight numCols/numRows hpad/vpad
363 slack = width - 2 * hpad;
364 for(int i=0; i<numCols; i++) slack -= colWidth[i];
365 if (numChildren() > 0)
367 // FEATURE: inefficient
368 int startslack = slack;
369 int increment = max(1, slack / numCols);
370 for(int col=0; col < numCols && slack > 0; col++) {
371 slack += colWidth[col];
372 colWidth[col] = min(colMaxWidth[col], colWidth[col] + increment);
373 slack -= colWidth[col];
375 if (slack == startslack) break;
380 // --- Phase 4 ----------------------------------------------------------------------
381 // assign children's new sizes and positions and recurse
382 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
383 if ((child.flags & INVISIBLE_FLAG) != 0) continue;
384 int child_x = 0, child_y = 0, child_width = 0, child_height = 0;
385 if ((child.flags & ABSOLUTE_FLAG) != 0) {
388 child_width = ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : min(child.maxwidth, width - child.x - hpad);
389 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y - vpad);
392 //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight HSHRINK_FLAG/VSHRINK_FLAG marginWidth/marginHeight hpad/vpad child_x/child_y child_width/child_height
393 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
394 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
395 child_x = max(hpad, marginWidth / 2); for(int i=0; i<child.col; i++) child_x += colWidth[i];
396 if (diff < 0) child_x += -1 * (diff / 2);
400 child.resize(child_x, child_y, child_width, child_height);
407 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
409 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
410 void render(int parentx, int parenty, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
411 if (Surface.abort || (flags & INVISIBLE_FLAG) != 0) return;
412 int globalx = parentx + (parent == null ? 0 : x);
413 int globaly = parenty + (parent == null ? 0 : y);
415 // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
416 clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw,
417 (parent == null ? 0 : globalx) + width) - globalx;
418 cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph,
419 (parent == null ? 0 : globaly) + height) - globaly;
420 clipx = max(clipx, parent == null ? 0 : globalx);
421 clipy = max(clipy, parent == null ? 0 : globaly);
422 if (clipw <= 0 || cliph <= 0) return;
424 if ((fillcolor & 0xFF000000) != 0x00000000 || parent == null)
425 buf.fillRect(clipx, clipy, clipx + clipw, clipy + cliph,
426 (fillcolor & 0xFF000000) != 0 ? fillcolor : 0xFF777777);
429 if ((flags & TILE_FLAG) != 0) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
430 else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
432 if (text != null && !text.equals(""))
433 renderText(globalx, globaly, clipx, clipy, clipw, cliph, buf);
435 // now subtract the pad region from the clip region before proceeding
436 clipw = min(max(clipx, globalx + hpad) + clipw, globalx + width - hpad) - clipx;
437 cliph = min(max(clipy, globaly + vpad) + cliph, globaly + height - vpad) - clipy;
438 clipx = max(clipx, globalx + hpad);
439 clipy = max(clipy, globaly + vpad);
441 for(Box b = getChild(0); b != null; b = b.nextSibling())
442 b.render(globalx, globaly, clipx, clipy, clipw, cliph, buf);
445 void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
446 //buf.setClip(x, y, w + x, h + y);
447 System.out.println("draw " + clipx + " " + clipy + " " + (clipx + clipw) + " " + (clipy + cliph));
448 buf.drawPicture(image,
449 clipx, clipy, clipx + clipw, clipy + cliph,
450 0, 0, image.getWidth(), image.getHeight());
451 //buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
454 void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, PixelBuffer buf) {
455 int iw = image.getWidth();
456 int ih = image.getHeight();
457 for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
458 for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
460 int dx1 = max(i * iw + x, x);
461 int dy1 = max(j * ih + y, y);
462 int dx2 = min((i+1) * iw + x, x + w);
463 int dy2 = min((j+1) * ih + y, y + h);
465 int sx1 = dx1 - (i*iw) - x;
466 int sy1 = dy1 - (j*ih) - y;
467 int sx2 = dx2 - (i*iw) - x;
468 int sy2 = dy2 - (j*ih) - y;
470 if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
471 buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
476 void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
477 for(int i=0; i<text.length(); i++) {
479 char c = text.charAt(i);
480 Glyph g = Glyph.getGlyph(font, fontsize, c);
483 y + vpad + g.max_ascent - g.baseline,
484 x + hpad + g.p.getWidth(),
485 y + vpad + g.max_ascent - g.baseline + g.p.getHeight(),
487 g.p.getWidth(), g.p.getHeight());
493 // Methods to implement org.xwt.js.JS //////////////////////////////////////
495 public Object callMethod(Object method, JS.Array args, boolean checkOnly) throws JS.Exn {
496 if ("indexof".equals(method)) {
497 if (checkOnly) return Boolean.TRUE;
498 if (args.length() != 1 || args.elementAt(0) == null || !(args.elementAt(0) instanceof Box)) return new Integer(-1);
499 Box b = (Box)args.elementAt(0);
500 if (b.parent != Box.this) {
501 if (redirect == null || redirect == Box.this) return new Integer(-1);
502 return redirect.callMethod(method, args, checkOnly);
504 return new Integer(b.getIndexInParent());
506 } else if ("apply".equals(method)) {
507 if (checkOnly) return Boolean.TRUE;
508 if (args.elementAt(0) instanceof Res) {
510 Res res = (Res)args.elementAt(0);
511 // res = res.addExtension(".xwt");
512 Template t = Template.buildTemplate(res.getInputStream(), "fromResource");
513 if (ThreadMessage.suspendThread()) try {
514 JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
516 // FIXME!!! needs to be xwt.apply(template, box)
517 t.apply(this, null, null, callback, 0, t.numUnits(), null);
519 ThreadMessage.resumeThread();
521 } catch (IOException e) {
524 } else if (args.elementAt(0) instanceof String) {
525 String templatename = (String)args.elementAt(0);
526 Template t = Template.getTemplate(templatename, null);
528 if (Log.on) Log.logJS(this, "template " + templatename + " not found");
530 if (ThreadMessage.suspendThread()) try {
531 JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
532 // FIXME!!! needs to be xwt.apply(template, box)
533 t.apply(this, null, null, callback, 0, t.numUnits(), null);
535 ThreadMessage.resumeThread();
538 } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
539 JS s = (JS)args.elementAt(0);
540 Object[] keys = s.keys();
541 for(int j=0; j<keys.length; j++) put(keys[j].toString(), s.get(keys[j]));
548 /** Returns the i_th child */
549 public Object get(int i) {
550 if (redirect == null) return null;
551 if (redirect != this) return redirect.get(i);
552 return i >= numChildren() || i < 0 ? null : getChild(i);
556 * Inserts value as child i; calls remove() if necessary.
557 * This method handles "reinserting" one of your children properly.
558 * INVARIANT: after completion, getChild(min(i, numChildren())) == newnode
559 * WARNING: O(n) runtime, unless i == numChildren()
561 public void put(int i, Object value) {
564 if (value != null && !(value instanceof Box)) {
565 if (Log.on) Log.logJS(this, "attempt to set a numerical property on a box to anything other than a box");
566 } else if (redirect == null) {
567 if (Log.on) Log.logJS(this, "attempt to add/remove children to/from a node with a null redirect");
568 } else if (redirect != this) {
569 Box b = value == null ? (Box)redirect.get(i) : (Box)value;
570 redirect.put(i, value);
572 } else if (value == null) {
573 if (i >= 0 && i < numChildren()) {
578 } else if (value instanceof RootProxy) {
579 if (Log.on) Log.logJS(this, "attempt to reparent a box via its proxy object");
581 Box newnode = (Box)value;
583 // check if box being moved is currently target of a redirect
584 for(Box cur = newnode.parent; cur != null; cur = cur.parent)
585 if (cur.redirect == newnode) {
586 if (Log.on) Log.logJS(this, "attempt to move a box that is the target of a redirect");
590 // check for recursive ancestor violation
591 for(Box cur = this; cur != null; cur = cur.parent)
592 if (cur == newnode) {
593 if (Log.on) Log.logJS(this, "attempt to make a node a parent of its own ancestor");
594 if (Log.on) Log.log(this, "box == " + this + " ancestor == " + newnode);
598 if (numKids > 15 && children == null) convert_to_array();
600 newnode.parent = this;
602 if (children == null) {
603 if (firstKid == null) {
605 newnode.prevSibling = newnode;
606 newnode.nextSibling = newnode;
607 } else if (i >= numKids) {
608 newnode.prevSibling = firstKid.prevSibling;
609 newnode.nextSibling = firstKid;
610 firstKid.prevSibling.nextSibling = newnode;
611 firstKid.prevSibling = newnode;
614 for(int j=0; j<i; j++) cur = cur.nextSibling;
615 newnode.prevSibling = cur.prevSibling;
616 newnode.nextSibling = cur;
617 cur.prevSibling.nextSibling = newnode;
618 cur.prevSibling = newnode;
619 if (i == 0) firstKid = newnode;
624 if (i >= children.size()) {
625 newnode.indexInParent = children.size();
626 children.addElement(newnode);
628 children.insertElementAt(newnode, i);
629 for(int j=i; j<children.size(); j++)
630 getChild(j).indexInParent = j;
634 // need both of these in case child was already uncalc'ed
637 MARK_FOR_REFLOW_this;
641 // note that JavaScript box[0] will invoke put(int i), not put(String s)
646 public Object get_(Object name) { return super.get(name); }
647 public Object get(Object name) { return get(name, false); }
648 public Object get(Object name_, boolean ignoretraps) {
649 if (name_ instanceof Number) return get(((Number)name_).intValue());
651 if (!(name_ instanceof String)) return null;
652 String name = (String)name_;
653 if (name.equals("")) return null;
655 // See if we're triggering a trap
656 Trap t = traps == null || ignoretraps ? null : (Trap)traps.get(name);
657 if (t != null) return t.perform();
659 // Check for a special handler
660 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
661 if (gph != null) return gph.get(this);
663 Object ret = super.get(name);
664 if (name.startsWith("$") && ret == null)
665 if (Log.on) Log.logJS(this, "WARNING: attempt to access " + name + ", but no child with id=\"" + name.substring(1) + "\" found");
669 public Object[] keys() {
670 Object[] ret = new Object[numChildren()];
671 for(int i=0; i<ret.length; i++) ret[i] = new Integer(i);
675 public void addTrap(Object name, Object value) { Trap.addTrap(this, name, ((JS.CompiledFunction)value)); }
676 public void delTrap(Object name, Object value) { Trap.delTrap(this, name, ((JS.CompiledFunction)value)); }
681 * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack)
682 * @param rp if this put is being performed via a root proxy, rp is the root proxy.
684 public void put_(Object name, Object value) { super.put(name, value); }
685 public void put(Object name, Object value) { put(name, value, false, null); }
686 public void put(Object name, Object value, boolean ignoretraps) { put(name, value, ignoretraps, null); }
687 public void put(Object name_, Object value, boolean ignoretraps, RootProxy rp) {
688 if (name_ instanceof Number) { put(((Number)name_).intValue(), value); return; }
689 if (!(name_ instanceof String)) { super.put(name_,value); return; }
690 String name = name_.toString();
691 if (!ignoretraps && traps != null) {
692 Trap t = (Trap)traps.get(name);
699 // don't want to really cascade down to the box on this one
700 if (name.equals("0")) return;
702 SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name);
703 if (gph != null) { gph.put(name, this, value); return; }
705 super.put(name, value);
709 // Tree Manipulation /////////////////////////////////////////////////////////////////////
711 /** The parent of this node */
712 private Box parent = null;
714 // Variables used in Vector mode */
715 /** INVARIANT: if (parent != null) parent.children.elementAt(indexInParent) == this */
716 private int indexInParent;
717 private Vec children = null;
719 // Variables used in linked-list mode
720 private int numKids = 0;
721 private Box nextSibling = null;
722 private Box prevSibling = null;
723 private Box firstKid = null;
725 // when we get more than 15 children, we switch to array-mode
726 private void convert_to_array() {
727 children = new Vec(numKids);
730 children.addElement(cur);
731 cur.indexInParent = children.size() - 1;
732 cur = cur.nextSibling;
733 } while (cur != firstKid);
736 /** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
737 public void remove() {
738 if (parent == null) {
739 if (surface != null) surface.dispose(true);
742 Box oldparent = parent;
743 if (oldparent == null) return;
744 MARK_FOR_REFLOW_this;
746 flags &= ~MOUSEINSIDE_FLAG;
748 if (parent.children != null) {
749 parent.children.removeElementAt(indexInParent);
750 for(int j=indexInParent; j<parent.children.size(); j++)
751 (parent.getChild(j)).indexInParent = j;
754 if (parent.firstKid == this) {
755 if (nextSibling == this) parent.firstKid = null;
756 else parent.firstKid = nextSibling;
759 prevSibling.nextSibling = nextSibling;
760 nextSibling.prevSibling = prevSibling;
766 if (oldparent != null) { Box b = oldparent; MARK_FOR_REFLOW_b; }
768 // note that JavaScript box[0] will invoke put(int i), not put(String s)
769 if (oldparent != null) oldparent.put("0", this);
772 /** returns our next sibling (parent[ourindex + 1]) */
773 public final Box nextSibling() {
774 if (parent == null) return null;
775 if (parent.children == null) {
776 if (nextSibling == parent.firstKid) return null;
779 if (indexInParent >= parent.children.size() - 1) return null;
780 return (Box)parent.children.elementAt(indexInParent + 1);
784 /** returns our next sibling (parent[ourindex + 1]) */
785 public final Box prevSibling() {
786 if (parent == null) return null;
787 if (parent.children == null) {
788 if (this == parent.firstKid) return null;
791 if (indexInParent == 0) return null;
792 return (Box)parent.children.elementAt(indexInParent - 1);
796 /** Returns the parent of this node */
797 public Box getParent() { return parent; }
799 /** Returns ith child */
800 public Box getChild(int i) {
801 if (children == null) {
802 if (firstKid == null) return null;
803 if (i >= numKids) return null;
804 if (i == numKids - 1) return firstKid.prevSibling;
806 for(int j=0; j<i; j++) cur = cur.nextSibling;
809 if (i >= children.size() || i < 0) return null;
810 return (Box)children.elementAt(i);
814 /** Returns the number of children */
815 public int numChildren() {
816 if (children == null) {
817 if (firstKid == null) return 0;
819 for(Box cur = firstKid.nextSibling; cur != firstKid; i++) cur = cur.nextSibling;
822 return children.size();
826 /** Returns our index in our parent */
827 public int getIndexInParent() {
828 if (parent == null) return 0;
829 if (parent.children == null) {
831 for(Box cur = this; cur != parent.firstKid; i++) cur = cur.prevSibling;
834 return indexInParent;
838 /** returns the root of the surface that this box belongs to */
839 public final Box getRoot() {
840 if (parent == null && surface != null) return this;
841 if (parent == null) return null;
842 return parent.getRoot();
846 // Root Proxy ///////////////////////////////////////////////////////////////////////////////
848 // FEATURE: use xwt.graft() here
849 RootProxy myproxy = null;
850 public JS getRootProxy() {
851 if (myproxy == null) myproxy = new RootProxy(this);
855 private static class RootProxy extends JS {
857 RootProxy(Box b) { this.box = b; }
858 public Object get(Object name) { return box.get(name); }
859 public void put(Object name, Object value) { box.put(name, value, false, this); }
860 public Object[] keys() { return box.keys(); }
861 public Object callMethod(Object method, JS.Array args, boolean justChecking) {
862 return ((Box)box).callMethod(method,args,justChecking);
867 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
869 static final int min(int a, int b) { if (a<b) return a; else return b; }
870 static final double min(double a, double b) { if (a<b) return a; else return b; }
871 static final int max(int a, int b) { if (a>b) return a; else return b; }
872 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; }
873 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; }
874 static final int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
875 final boolean inside(int x, int y) { return (!((flags & INVISIBLE_FLAG) != 0) && x >= 0 && y >= 0 && x < width && y < height); }
877 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
878 public static Box whoIs(Box cur, int x, int y) {
880 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
884 // WARNING: this method is called from the event-queueing
885 // thread -- it may run concurrently with ANY part of XWT, and
886 // is UNSYNCHRONIZED for performance reasons. BE CAREFUL
889 if ((cur.flags & INVISIBLE_FLAG) != 0) return null;
890 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
892 for(int i=cur.numChildren() - 1; i>=0; i--) {
893 Box child = cur.getChild(i);
894 if (child == null) continue; // since this method is unsynchronized, we have to double-check
897 if (!((child.flags & INVISIBLE_FLAG) != 0) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
907 * A helper class for properties of Box which require special
910 * To avoid excessive use of String.equals(), the Box.get() and
911 * Box.put() methods employ a Hash keyed on property names that
912 * require special handling. The value stored in the Hash is an
913 * instance of an anonymous subclass of SpecialBoxProperty, which knows
914 * how to handle get()s and put()s for that property name. There
915 * should be one anonymous subclass of SpecialBoxProperty for each
916 * specially-handled property on Box.
918 static class SpecialBoxProperty {
920 SpecialBoxProperty() { }
922 /** stores instances of SpecialBoxProperty; keyed on property name */
923 static Hash specialBoxProperties = new Hash(200, 3);
925 /** this method defines the behavior when the property is get()ed from b */
926 Object get(Box b) { return null; }
928 /** this method defines the behavior when the property is put() to b */
929 void put(Box b, Object value) { }
931 /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
932 void put(String name, Box b, Object value) { put(b, value); }
935 //#repeat fillcolor/strokecolor
936 specialBoxProperties.put("fillcolor", new SpecialBoxProperty() {
937 public Object get(Box b) {
938 if ((b.fillcolor & 0xFF000000) == 0) return null;
939 String red = Integer.toHexString((b.fillcolor & 0x00FF0000) >> 16);
940 String green = Integer.toHexString((b.fillcolor & 0x0000FF00) >> 8);
941 String blue = Integer.toHexString(b.fillcolor & 0x000000FF);
942 if (red.length() < 2) red = "0" + red;
943 if (blue.length() < 2) blue = "0" + blue;
944 if (green.length() < 2) green = "0" + green;
945 return "#" + red + green + blue;
947 public void put(Box b, Object value) {
948 int newcolor = b.fillcolor;
949 String s = value == null ? null : value.toString();
950 if (value == null) newcolor = 0x00000000;
951 else if (s.length() > 0 && s.charAt(0) == '#')
953 newcolor = 0xFF000000 |
954 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
955 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
956 Integer.parseInt(s.substring(5, 7), 16);
957 } catch (NumberFormatException e) {
958 Log.log(this, "invalid color " + s);
961 else if (org.xwt.translators.SVG.colors.get(s) != null)
962 newcolor = 0xFF000000 | ((Integer)org.xwt.translators.SVG.colors.get(s)).intValue();
963 if (newcolor == b.fillcolor) return;
964 b.fillcolor = newcolor;
970 specialBoxProperties.put("color", new SpecialBoxProperty() {
971 public Object get(Box b) { return b.get("fillcolor"); }
972 public void put(Box b, Object value) { b.put("fillcolor", value); }
975 specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
976 public Object get(Box b) { return b.get("strokecolor"); }
977 public void put(Box b, Object value) { b.put("strokecolor", value); }
980 specialBoxProperties.put("text", new SpecialBoxProperty() {
981 public Object get(Box b) { return b.text; }
982 public void put(Box b, Object value) {
983 String t = value == null ? "null" : value.toString();
984 if (t.equals(b.text)) return;
987 if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
988 b.textwidth = b.textheight = 0;
993 for(int i=0; i<b.text.length(); i++) {
994 Glyph g = Glyph.getGlyph(b.font, b.fontsize, b.text.charAt(i));
995 b.textwidth += g.advance;
996 b.textheight = g.max_ascent + g.max_descent;
998 } catch (Exception e) {
1005 specialBoxProperties.put("font", new SpecialBoxProperty() {
1006 public Object get(Box b) { return b.font; }
1007 public void put(Box b, Object value) {
1008 // FIXME: translate value into a resource if it is a string
1009 b.font = value == null ? null : (Res)value;
1011 b.flags |= FONT_CHANGED_FLAG;
1015 specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
1016 public Object get(Box b) { return b.font; }
1017 public void put(Box b, Object value) {
1018 if (b.fontsize == stoi(value)) return;
1019 b.fontsize = stoi(value);
1021 b.flags |= FONT_CHANGED_FLAG;
1025 specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
1026 public Object get(Box b) { return b; }
1027 public void put(Box b, Object value) {
1028 if (value == null) b.remove();
1029 else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
1030 else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
1034 specialBoxProperties.put("orient", new SpecialBoxProperty() {
1035 public Object get(Box b) {
1036 Log.log(this, "warning: the orient property is deprecated");
1037 if (b.redirect == null) return "horizontal";
1038 else if (b.redirect != b) return get(b.redirect);
1039 else if (b.cols == 1) return "vertical";
1040 else if (b.rows == 1) return "horizontal";
1043 public void put(Box b, Object value) {
1044 Log.log(this, "warning: the orient property is deprecated");
1045 if (value == null) return;
1046 if (b.redirect == null) return;
1047 if (b.redirect != b) { put(b.redirect, value); return; }
1048 if (value.equals("vertical")) {
1049 if (b.rows == 0) return;
1050 b.rows = 0; b.cols = 1;
1051 } else if (value.equals("horizontal")) {
1052 if (b.cols == 0) return;
1053 b.cols = 0; b.rows = 1;
1055 Log.log(this, "invalid value put to orient property: " + value);
1059 specialBoxProperties.put("static", new SpecialBoxProperty() {
1060 public Object get(Box b) {
1062 JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
1063 for(int i=0; i<cfsn.length() - 1; i++)
1064 if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
1065 cfsn = cfsn.substring(0, i);
1068 return Static.getStatic(cfsn);
1072 specialBoxProperties.put("shrink", new SpecialBoxProperty() {
1073 public Object get(Box b) { return (((b.flags & HSHRINK_FLAG) != 0) || ((b.flags & VSHRINK_FLAG) != 0)) ? Boolean.TRUE : Boolean.FALSE; }
1074 public void put(Box b, Object value) { b.put("hshrink", value); b.put("vshrink", value); }
1077 //#repeat hshrink/vshrink HSHRINK_FLAG/VSHRINK_FLAG
1078 specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
1079 public Object get(Box b) { return new Boolean((b.flags & HSHRINK_FLAG) != 0); }
1080 public void put(Box b, Object value) {
1081 boolean newshrink = stob(value);
1082 if (((b.flags & HSHRINK_FLAG) != 0) == newshrink) return;
1083 if (newshrink) b.flags |= HSHRINK_FLAG; else b.flags &= ~HSHRINK_FLAG;
1090 specialBoxProperties.put("x", new SpecialBoxProperty() {
1091 public Object get(Box b) {
1092 if (b.surface == null) return new Integer(0);
1093 if ((b.flags & INVISIBLE_FLAG) != 0) return new Integer(0);
1094 return new Integer(b.x);
1096 public void put(Box b, Object value) {
1097 if (!((b.flags & ABSOLUTE_FLAG) != 0)) return;
1098 int x = stoi(value);
1099 if (x == b.x) return;
1102 if (b.parent == null && b.surface != null) {
1103 b.surface.setLocation();
1104 b.surface.centerSurfaceOnRender = false;
1112 //#repeat width/height minwidth/minheight maxwidth/maxheight
1113 specialBoxProperties.put("width", new SpecialBoxProperty() {
1114 public Object get(Box b) { return new Integer(b.width); }
1115 public void put(Box b, Object value) {
1116 b.width = stoi(value);
1117 if (b.parent == null && b.surface != null) {
1118 b.surface.setSize();
1121 if (b.minwidth == b.width && b.maxwidth == b.width) return;
1122 b.minwidth = b.maxwidth = b.width;
1128 //#repeat cols/rows rows/cols
1129 specialBoxProperties.put("cols", new SpecialBoxProperty() {
1130 public Object get(Box b) { return new Double(b.cols); }
1131 public void put(Box b, Object value) {
1132 if (b.cols == stoi(value)) return;
1133 b.cols = stoi(value);
1134 if (b.cols == 0 && b.rows == 0) b.rows = 1;
1135 if (b.cols != 0 && b.rows != 0) b.rows = 0;
1140 //#repeat colspan/rowspan
1141 specialBoxProperties.put("colspan", new SpecialBoxProperty() {
1142 public Object get(Box b) { return new Double(b.colspan); }
1143 public void put(Box b, Object value) {
1144 if (b.colspan == stoi(value)) return;
1145 b.colspan = stoi(value);
1151 specialBoxProperties.put("tile", new SpecialBoxProperty() {
1152 public Object get(Box b) { return ((b.flags & TILE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1153 public void put(Box b, Object value) {
1154 if (((b.flags & TILE_FLAG) != 0) == stob(value)) return;
1155 if (stob(value)) b.flags |= TILE_FLAG; else b.flags &= ~TILE_FLAG;
1159 specialBoxProperties.put("invisible", new SpecialBoxProperty() {
1160 public Object get(Box b) {
1161 for (Box cur = b; cur != null; cur = cur.parent) {
1162 if ((cur.flags & INVISIBLE_FLAG) != 0) return Boolean.TRUE; }
1163 return Boolean.FALSE;
1165 public void put(Box b, Object value) {
1166 if (stob(value) == ((b.flags & INVISIBLE_FLAG) != 0)) return;
1167 if (stob(value)) b.flags |= INVISIBLE_FLAG; else b.flags &= ~INVISIBLE_FLAG;
1168 if (b.parent == null) {
1169 if (b.surface != null) b.surface.setInvisible((b.flags & INVISIBLE_FLAG) != 0);
1172 MARK_FOR_REFLOW_b_parent;
1173 b.parent.dirty(b.x, b.y, b.width, b.height);
1177 specialBoxProperties.put("absolute", new SpecialBoxProperty() {
1178 public Object get(Box b) { return ((b.flags & ABSOLUTE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1179 public void put(Box b, Object value) {
1180 if (stob(value) == ((b.flags & ABSOLUTE_FLAG) != 0)) return;
1181 if (stob(value)) b.flags |= ABSOLUTE_FLAG; else b.flags &= ~ABSOLUTE_FLAG;
1182 if ((b.flags & ABSOLUTE_FLAG) != 0) { b.x = 0; b.y = 0; }
1183 if (b.parent != null) MARK_FOR_REFLOW_b_parent;
1186 specialBoxProperties.put("image", new SpecialBoxProperty() {
1187 public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
1188 public void put(Box b, Object value) {
1189 if ((value == null && b.image == null) ||
1190 (value != null && b.image != null && value.equals(ImageDecoder.imageToNameMap.get(b.image)))) return;
1191 String s = value == null ? null : value.toString();
1192 if (s == null || s.equals("")) b.image = null;
1194 if ((b.image = ImageDecoder.getPicture(s)) == null) {
1195 if (Log.on) Log.logJS(Box.class, "unable to load image " + s);
1197 b.minwidth = b.image.getWidth();
1198 b.minheight = b.image.getHeight();
1206 //#repeat globalx/globaly x/y
1207 specialBoxProperties.put("globalx", new SpecialBoxProperty() {
1208 public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }
1209 public void put(Box b, Object value) {
1210 if (b.surface == null || b.parent == null) return;
1211 b.put("x", new Integer(stoi(value) - stoi(get(b.parent))));
1217 specialBoxProperties.put("cursor", new SpecialBoxProperty() {
1218 public Object get(Box b) { return b.cursor; }
1219 public void put(Box b, Object value) {
1220 b.cursor = (String)value;
1221 if (b.surface == null) return;
1223 // see if we need to update the surface cursor
1224 Surface surface = b.getRoot().surface;
1225 String tempcursor = surface.cursor;
1226 b.Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
1227 if (surface.cursor != tempcursor) surface.syncCursor();
1231 //#repeat mousex/mousey x/y
1232 specialBoxProperties.put("mousex", new SpecialBoxProperty() {
1233 public Object get(Box b) {
1234 Surface surface = b.getRoot().surface;
1235 if (surface == null) return new Integer(0);
1236 int mousex = surface.mousex;
1237 for(Box cur = b; cur != null && cur.parent != null; cur = cur.parent) mousex -= cur.x;
1238 return new Integer(mousex);
1243 specialBoxProperties.put("xwt", new SpecialBoxProperty() {
1244 public Object get(Box b) { return XWT.singleton; }
1247 specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
1248 public Object get(Box b) {
1249 return ((b.flags & MOUSEINSIDE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
1252 specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
1253 public Object get(Box b) {
1254 if (b.redirect == null) return new Integer(0);
1255 if (b.redirect != b) return get(b.redirect);
1256 return new Integer(b.numChildren());
1259 SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
1260 public void put(String name, Box b, Object value) {
1261 Surface surface = b.getRoot().surface;
1262 if (surface == null) return;
1263 int mousex = surface.mousex;
1264 int mousey = surface.mousey;
1265 for(Box c = b.parent; c != null && c.parent != null; c = c.parent) {
1269 for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
1270 if (c.inside(mousex - c.x, mousey - c.y)) {
1275 if (b.parent != null) b.parent.put(name, value);
1278 specialBoxProperties.put("Press1", mouseEventHandler);
1279 specialBoxProperties.put("Press2", mouseEventHandler);
1280 specialBoxProperties.put("Press3", mouseEventHandler);
1281 specialBoxProperties.put("Release1", mouseEventHandler);
1282 specialBoxProperties.put("Release2", mouseEventHandler);
1283 specialBoxProperties.put("Release3", mouseEventHandler);
1284 specialBoxProperties.put("Click1", mouseEventHandler);
1285 specialBoxProperties.put("Click2", mouseEventHandler);
1286 specialBoxProperties.put("Click3", mouseEventHandler);
1287 specialBoxProperties.put("DoubleClick1", mouseEventHandler);
1288 specialBoxProperties.put("DoubleClick2", mouseEventHandler);
1289 specialBoxProperties.put("DoubleClick3", mouseEventHandler);
1291 specialBoxProperties.put("root", new SpecialBoxProperty() {
1292 public void put(Box b, Object value) {
1293 if (stob(value)) b.flags |= ISROOT_FLAG;
1294 else b.flags &= ~ISROOT_FLAG;
1296 public Object get(Box b) {
1297 if (b.parent == null || ((b.flags & ISROOT_FLAG) != 0)) return b;
1298 else return get(b.parent);
1301 specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
1302 public Object get(Box b) {
1303 if (b.parent == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
1306 public void put(Box b, Object value) {
1307 if (b.surface == null) return;
1308 boolean val = stob(value);
1309 if (b.parent == null && b.surface.minimized != val) b.surface.setMinimized(val);
1313 specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
1314 public Object get(Box b) {
1315 if (b.parent == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
1318 public void put(Box b, Object value) {
1319 if (b.surface == null) return;
1320 boolean val = stob(value);
1321 if (b.parent == null && b.surface.maximized != val) b.surface.setMaximized(val);
1325 specialBoxProperties.put("toback", new SpecialBoxProperty() {
1326 public void put(Box b, Object value) {
1327 if (b.parent == null && stob(value) && b.surface != null) b.surface.toBack();
1331 specialBoxProperties.put("tofront", new SpecialBoxProperty() {
1332 public void put(Box b, Object value) {
1333 if (b.parent == null && stob(value) && b.surface != null) b.surface.toFront();
1337 //#repeat hscar/vscar
1338 specialBoxProperties.put("hscar", new SpecialBoxProperty() {
1339 public void put(Box b, Object value) {
1340 if (b.parent == null && b.surface != null) {
1341 b.surface.hscar = stoi(value);
1342 b.surface.dirty(0, 0, b.width, b.height);
1343 b.surface.Refresh();
1349 specialBoxProperties.put("Close", new SpecialBoxProperty() {
1350 public void put(Box b, Object value) {
1351 if (b.parent == null && b.surface != null) b.surface.dispose(true);
1355 // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
1356 specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
1357 specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
1358 specialBoxProperties.put("PosChange", new SpecialBoxProperty());
1359 specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
1362 specialBoxProperties.put("hpad", new SpecialBoxProperty() {
1363 public Object get(Box b) {
1364 if (b.redirect == null) return new Integer(0);
1365 if (b.redirect != b) return get(b.redirect);
1366 return new Integer(b.hpad);
1368 public void put(Box b, Object value) {
1369 if (b.redirect == null) return;
1370 if (b.redirect != b) { put(b.redirect, value); return; }
1371 int newval = stoi(value);
1372 if (newval == b.hpad) return;
1379 //#repeat minwidth/minheight maxwidth/maxheight
1380 specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
1381 public Object get(Box b) { return new Integer(b.minwidth); }
1382 public void put(Box b, Object value) {
1383 if (stoi(value) == b.minwidth) return;
1384 b.minwidth = stoi(value);
1388 specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
1389 public Object get(Box b) { return new Integer(b.maxwidth); }
1390 public void put(Box b, Object value) {
1391 if (stoi(value) == b.maxwidth) return;
1392 b.maxwidth = stoi(value);
1398 specialBoxProperties.put("redirect", new SpecialBoxProperty() {
1399 public void put(Box b, Object value) { }
1400 public Object get(Box b) {
1401 if (b.redirect == null) return null;
1402 if (b.redirect == b) return Boolean.TRUE;
1403 return get(b.redirect);
1407 // FEATURE: this still isn't totally harmonious; when you createSurface, these aren't checked
1408 specialBoxProperties.put("titlebar", new SpecialBoxProperty() {
1409 public void put(Box b, Object value) {
1410 if (b.surface != null) b.surface.setTitleBarText(value.toString());
1411 b.put_("titlebar", value);
1413 public Object get(Box b) { return b.get_("titlebar"); }
1416 specialBoxProperties.put("icon", new SpecialBoxProperty() {
1417 public void put(Box b, Object value) {
1418 b.put_("icon", value);
1419 if (b.surface == null) return;
1420 Picture pic = ImageDecoder.getPicture(value.toString());
1421 if (pic != null) b.surface.setIcon(pic);
1422 else if (Log.on) Log.log(this, "unable to load icon " + value);
1424 public Object get(Box b) { return b.get_("icon"); }
1429 /** helper that converts a String to a boolean according to JavaScript coercion rules */
1430 public static boolean stob(Object o) {
1431 if (o == null) return false;
1432 return Boolean.TRUE.equals(o) || "true".equals(o);
1435 /** helper that converts a String to an int according to JavaScript coercion rules */
1436 public static int stoi(Object o) {
1437 if (o == null) return 0;
1438 if (o instanceof Integer) return ((Integer)o).intValue();
1441 if (!(o instanceof String)) s = o.toString();
1444 try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
1445 catch (NumberFormatException e) { return 0; }