1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
7 // FIXME: are traps on x/y meaningful?
8 // FIXME: trap on visible, trigger when parent visibility changes
9 // FIXME: mouse move/release still needs to propagate to boxen in which the mouse was pressed and is still held down
10 // FEATURE: reintroduce surface.abort
14 import org.ibex.util.*;
15 import org.ibex.graphics.*;
19 * Encapsulates the data for a single Ibex box as well as all layout
23 * <p>The rendering process consists of four phases; each requires
24 * one DFS pass over the tree</p>
25 * <ol><li> <b>pack()</b>: each box sets its childrens' row/col
26 * <ol><li> <b>constrain()</b>: contentwidth is computed
27 * <li> <b>resize()</b>: width/height and x/y positions are set
28 * <li> <b>render()</b>: children draw their content onto the PixelBuffer.
31 * The first three passes together are called the <i>reflow</i>
32 * phase. Reflowing is done in a seperate pass since SizeChanges
33 * trigger a Surface.abort; if rendering were done in the same pass,
34 * rendering work done prior to the Surface.abort would be wasted.
36 public final class Box extends JS.Obj implements Callable {
38 // Macros //////////////////////////////////////////////////////////////////////
40 final void REPLACE() { for(Box b2 = this; b2 != null && !b2.test(REPLACE); b2 = b2.parent) b2.set(REPLACE); }
41 final void RECONSTRAIN() { for(Box b2 = this; b2 != null && !b2.test(RECONSTRAIN); b2 = b2.parent) b2.set(RECONSTRAIN); }
43 //#define CHECKSET_SHORT(prop) short nu = (short)JSU.toInt(value); if (nu == prop) break; prop = nu;
44 //#define CHECKSET_INT(prop) int nu = JSU.toInt(value); if (nu == prop) break; prop = nu;
45 //#define CHECKSET_FLAG(flag) boolean nu=JSU.toBoolean(value);if(nu == test(flag)) break; if (nu) set(flag); else clear(flag);
46 //#define CHECKSET_BOOLEAN.N(prop) boolean nu = JSU.toBoolean(value); if (nu == prop) break; prop = nu;
47 //#define CHECKSET_STRING(prop) if((value==null&&prop==null)||(value!=null&&JSU.toString(value).equals(prop)))break;prop=JSU.toString(value);
49 // Instance Data //////////////////////////////////////////////////////////////////////
51 private Box parent = null;
52 private Box redirect = this;
53 private int flags = VISIBLE | RECONSTRAIN | REPLACE | STOP_UPWARD_PROPAGATION | CLIP | MOVED;
54 private BalancedTree bt = null;
55 private String text = null;
56 private Font font = DEFAULT_FONT;
57 private Picture texture = null;
58 public int fillcolor = 0x00000000;
59 private int strokecolor = 0xFF000000;
60 public float flex = 1;
61 private Path path = null;
62 private Affine transform = Affine.identity();
64 // FEATURE: polygon caching
65 private Polygon polygon = null;
67 // specified directly by user
68 public int minwidth = 0;
69 public int maxwidth = Integer.MAX_VALUE;
70 public int minheight = 0;
71 public int maxheight = Integer.MAX_VALUE;
73 // computed during reflow
75 public int height = 0;
76 public int contentwidth = 0; // == max(minwidth, textwidth, sum(child.contentwidth))
77 public int contentheight = 0;
80 // Instance Methods /////////////////////////////////////////////////////////////////////
82 /** invoked when a resource needed to render ourselves finishes loading */
83 public Object run(Object o) throws JSExn {
84 if (texture == null) { Log.warn(Box.class, "perform() called with null texture"); return null; }
85 if (texture.isLoaded) {
86 setWidth(max(texture.width, minwidth), maxwidth);
87 setHeight(max(texture.height, minheight), maxheight);
89 else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res.unclone()); }
93 /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */
94 public void dirty() { if (path==null) dirty(0, 0, contentwidth, contentheight); else dirty(path); }
95 public void dirty(int x, int y, int w, int h) { }
96 public void dirty(Path p) {
98 for(Box cur = this; cur != null; cur = cur.parent) a.premultiply(cur.transform);
99 long hbounds = p.horizontalBounds(a);
100 long vbounds = p.verticalBounds(a);
101 int x1 = (int)Encode.longToFloat2(hbounds);
102 int x2 = (int)Encode.longToFloat1(hbounds);
103 int y1 = (int)Encode.longToFloat2(vbounds);
104 int y2 = (int)Encode.longToFloat1(vbounds);
105 if (getSurface() != null) getSurface().dirty(x1, y1, x2-x1, y2-y1);
109 // Reflow ////////////////////////////////////////////////////////////////////////////////////////
111 /** should only be invoked on the root box */
112 public void reflow() {
113 constrain(this, Affine.identity());
123 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
124 child.width = max(child.minwidth, child.test(HSHRINK) ? child.contentwidth : child.maxwidth);
125 child.height = max(child.minheight, child.test(VSHRINK) ? child.contentheight : child.maxheight);
130 float slack = width, oldslack = 0, flex = 0, newflex = 0;
131 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
132 if (!child.test(VISIBLE)) continue;
133 slack -= (child.width = child.contentwidth);
134 if (child.width < (child.test(HSHRINK)?child.contentwidth:child.maxwidth)) flex += child.flex;
135 child.height = child.test(HSHRINK) ? child.contentheight : bound(child.contentheight, height, child.maxheight);
137 while(slack > 0 && flex > 0 && oldslack!=slack) {
140 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
141 if (!child.test(VISIBLE)) continue;
142 if (child.width < child.maxwidth && !child.test(HSHRINK))
143 child.width = bound(child.contentwidth, (int)(child.width + (slack/flex)), child.maxwidth);
144 newflex += child.width < child.maxwidth && !child.test(HSHRINK) ? child.flex : 0;
145 slack -= child.width;
149 float pos = slack / 2;
150 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
151 if (!child.test(VISIBLE)) continue;
152 child.transform.e += pos;
153 child.transform.f += (height-child.height)/2;
159 /** used (single-threadedly) in constrain() */
160 private static int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
161 public void constrain(Box b, Affine a) {
166 a = a.copy().premultiply(transform);
168 boolean relevant = packed() || ((fillcolor&0xff000000)!=0x0) || path!=null;
169 int save_xmin = xmin, save_ymin = ymin, save_xmax = xmax, save_ymax = ymax;
171 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
172 child.constrain(b, a);
173 contentwidth = max(contentwidth, child.contentwidth);
174 contentheight = max(contentheight, child.contentheight);
179 for(Box child = getChild(0); child != null; child = child.nextSibling()) {
180 xmin = Integer.MAX_VALUE; ymin = Integer.MAX_VALUE;
181 xmax = Integer.MIN_VALUE; ymax = Integer.MIN_VALUE;
182 child.constrain(this, Affine.identity());
183 if (xmin==Integer.MAX_VALUE||xmax==Integer.MIN_VALUE||ymin==Integer.MAX_VALUE||ymax== Integer.MIN_VALUE) continue;
184 child.transform.e = -1 * xmin;
185 child.transform.f = -1 * ymin;
186 contentwidth += (xmax-xmin);
187 contentheight = max(ymax-ymin, contentheight);
194 contentwidth = bound(minwidth, contentwidth, maxwidth);
195 contentheight = bound(minheight, contentheight, maxheight);
197 int cw = bound(minwidth, contentwidth, maxwidth), ch = bound(minheight, contentheight, maxheight);
198 //#repeat contentwidth/contentheight contentheight/contentwidth minwidth/minheight row/col col/row \
199 // textwidth/textheight maxwidth/maxheight bounds/boundsy x1/y1 x2/y2 z1/q1 z2/q2 z3/q3 z4/q4 \
200 // horizontalBounds/verticalBounds e/f multiply_px/multiply_py xmin/ymin xmax/ymax
201 long bounds = path==null ? 0 : path.horizontalBounds(a);
202 float z1 = path==null ? a.multiply_px(0, 0) : Encode.longToFloat2(bounds);
203 float z2 = path==null ? a.multiply_px(cw, ch) : Encode.longToFloat1(bounds);
204 float z3 = path==null ? a.multiply_px(cw, 0) : Encode.longToFloat2(bounds);
205 float z4 = path==null ? a.multiply_px(0, ch) : Encode.longToFloat1(bounds);
206 xmin = min(xmin, (int)min(min(z1, z2), min(z3, z4)));
207 xmax = max(xmax, (int)max(max(z1, z2), max(z3, z4)));
212 // Rendering Pipeline /////////////////////////////////////////////////////////////////////
214 private static final boolean OPTIMIZE = false;
216 /** Renders self and children within the specified region. All rendering operations are clipped to xIn,yIn,wIn,hIn */
217 public void render(int cx1, int cy1, int cx2, int cy2, PixelBuffer buf, Affine a) {
218 if (!test(VISIBLE)) return;
219 a = a.copy().premultiply(transform);
223 if (((fillcolor & 0xFF000000) != 0x00000000 || parent == null)) {
224 if (OPTIMIZE && a.doesNotRotate()) {
225 int x = (int)a.multiply_px(0, 0);
226 int y = (int)a.multiply_py(0, 0);
227 int x2 = (int)a.multiply_px(contentwidth, contentheight);
228 int y2 = (int)a.multiply_py(contentwidth, contentheight);
229 buf.fillTrapezoid(x, x, y, x2, x2, y2, (fillcolor & 0xFF000000) == 0 ? 0xffffffff : fillcolor);
231 new Polygon().addrect(0, 0, contentwidth, contentheight, a).fill(buf, new Paint.SingleColorPaint(fillcolor));
237 Polygon p = new Polygon(path, a);
238 p.fill(buf, new Paint.SingleColorPaint(fillcolor));
239 p.stroke(buf, strokecolor);
242 for(Box b = getChild(0); b != null; b = b.nextSibling()) b.render(cx1, cy1, cx2, cy2, buf, a);
246 // Methods to implement org.ibex.js.JS //////////////////////////////////////
248 public JS call(JS method, JS[] args) throws JSExn {
249 switch (args.length) {
251 //#switch(JSU.toString(method))
253 Box b = (Box)args[0];
254 if (b.parent != this)
255 return (redirect == null || redirect == this) ?
256 JSU.N(-1) : redirect.call(method, args);
257 return JSU.N(b.getIndexInParent());
260 Box b = (Box)args[0];
261 JS ret = new JS.Obj();
262 ret.put(JSU.S("x"), JSU.N(b.localToGlobalX(0) - localToGlobalX(0)));
263 ret.put(JSU.S("y"), JSU.N(b.localToGlobalY(0) - localToGlobalY(0)));
269 return super.call(method, args);
272 public JS get(JS name) throws JSExn {
274 return redirect == null ? null : redirect == this ? getChild(JSU.toInt(name)) : redirect.get(name);
276 //#switch(JSU.toString(name))
277 case "surface": return parent == null ? null : parent.getAndTriggerTraps(name);
278 case "indexof": return METHOD;
279 case "distanceto": return METHOD;
280 case "text": return JSU.S(text);
282 if (path != null) return JSU.S(path.toString());
283 if (text == null) return null;
284 if (font == null) return null;
286 for(int i=0; i<text.length(); i++) ret += font.glyphs[text.charAt(i)].path;
289 case "fill": return JSU.S(Color.colorToString(fillcolor));
290 case "strokecolor": return JSU.S(Color.colorToString(strokecolor));
291 case "textcolor": return JSU.S(Color.colorToString(strokecolor));
292 case "font": return font == null ? null : font.stream;
293 case "fontsize": return font == null ? JSU.N(10) : JSU.N(font.pointsize);
294 case "thisbox": return this;
295 case "shrink": return JSU.B(test(HSHRINK) || test(VSHRINK));
296 case "hshrink": return JSU.B(test(HSHRINK));
297 case "vshrink": return JSU.B(test(VSHRINK));
298 case "flex": return JSU.N(flex);
299 case "width": getRoot().reflow(); return JSU.N(width);
300 case "height": getRoot().reflow(); return JSU.N(height);
301 case "minwidth": return JSU.N(minwidth);
302 case "maxwidth": return JSU.N(maxwidth);
303 case "minheight": return JSU.N(minheight);
304 case "maxheight": return JSU.N(maxheight);
305 case "clip": return JSU.B(test(CLIP));
306 case "visible": return JSU.B(test(VISIBLE) && (parent == null || (parent.get(JSU.S("visible")) == JSU.T)));
307 case "axis": return test(XAXIS) ? JSU.S("x") : test(YAXIS) ? JSU.S("y") : JSU.S("z");
308 case "globalx": return JSU.N(localToGlobalX(0));
309 case "globaly": return JSU.N(localToGlobalY(0));
310 case "cursor": return test(CURSOR) ? JSU.S((String)boxToCursor.get(this)) : null;
312 if (getSurface() == null) return null;
313 if (getSurface()._mousex == Integer.MAX_VALUE)
314 throw new JSExn("you cannot read from the box.mouse property in background thread context");
316 case "numchildren": return redirect == null ? JSU.N(0) : redirect == this ? JSU.N(treeSize()) : redirect.get(JSU.S("numchildren"));
317 case "redirect": return redirect == null ? null : redirect == this ? JSU.T : redirect.get(JSU.S("redirect"));
318 case "Minimized": if (parent == null && getSurface() != null) return JSU.B(getSurface().minimized);
319 default: return super.get(name);
321 throw new Error("unreachable"); // unreachable
324 private class Mouse extends JS.Immutable implements JS.Cloneable {
325 public JS get(JS key) throws JSExn {
326 //#switch(JSU.toString(key))
327 case "x": return JSU.N(globalToLocalX(getSurface()._mousex));
328 case "y": return JSU.N(globalToLocalY(getSurface()._mousey));
330 // this might not get recomputed if we change mousex/mousey...
331 case "inside": return JSU.B(test(MOUSEINSIDE));
333 return super.get(key);
337 //#repeat setWidth/setHeight minwidth/minheight maxwidth/maxheight pendingWidth/pendingHeight
338 public void setWidth(int min, int max) {
339 // FIXME: deal with conflicting min/max
340 if (this.minwidth == min && this.maxwidth == max) return;
344 if (parent != null || getSurface() == null) return;
345 getSurface().pendingWidth = maxwidth;
347 // FIXME: the repeat doesn't work right here
348 getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
352 public void put(JS name, JS value) throws JSExn {
353 if (JSU.isInt(name)) { put(JSU.toInt(name), value); return; }
354 //#switch(JSU.toString(name))
355 case "thisbox": if (value == null) removeSelf();
356 case "text": { String s = value==null ? "" : JSU.toString(value); CHECKSET_STRING(text); RECONSTRAIN(); dirty(); }
357 case "strokecolor": value = JSU.N(Color.stringToColor(JSU.toString(value))); CHECKSET_INT(strokecolor); dirty();
358 case "textcolor": value = JSU.N(Color.stringToColor(JSU.toString(value))); CHECKSET_INT(strokecolor); dirty();
359 case "shrink": CHECKSET_FLAG(HSHRINK | VSHRINK); RECONSTRAIN();
360 case "hshrink": CHECKSET_FLAG(HSHRINK); RECONSTRAIN();
361 case "vshrink": CHECKSET_FLAG(VSHRINK); RECONSTRAIN();
363 path = new Path(JSU.toString(value));
364 float tx = -1 * Encode.longToFloat2(path.horizontalBounds(Affine.identity()));
365 float ty = -1 * Encode.longToFloat2(path.verticalBounds(Affine.identity()));
366 path.transform(Affine.translate(tx, ty), true);
373 transform = Affine.parse(JSU.toString(value));
376 if (getSurface() != null) getSurface().dirty(0, 0, 500, 500); // FIXME
378 RECONSTRAIN(); dirty(); polygon = null;
380 case "width": setWidth(JSU.toInt(value), JSU.toInt(value));
381 case "height": setHeight(JSU.toInt(value), JSU.toInt(value));
382 case "flex": flex = JSU.toFloat(value);
383 case "maxwidth": setWidth(minwidth, JSU.toInt(value));
384 case "minwidth": setWidth(JSU.toInt(value), maxwidth);
385 case "maxheight": setHeight(minheight, JSU.toInt(value));
386 case "minheight": setHeight(JSU.toInt(value), maxheight);
387 case "visible": CHECKSET_FLAG(VISIBLE); RECONSTRAIN(); dirty();
388 case "cursor": setCursor(JSU.toString(value));
389 case "fill": setFill(value);
390 case "clip": CHECKSET_FLAG(CLIP); if (parent == null) dirty(); else parent.dirty();
392 int mousex = JSU.toInt(((JS)value).get(JSU.S("x")));
393 int mousey = JSU.toInt(((JS)value).get(JSU.S("y")));
394 getSurface()._mousex = (int)localToGlobalX(mousex);
395 getSurface()._mousey = (int)localToGlobalY(mousey);
397 String s = JSU.toString(value);
398 if (s.equals("x")) { clear(YAXIS); set(XAXIS); }
399 else if (s.equals("y")) { clear(XAXIS); set(YAXIS); }
400 else if (s.equals("z")) { clear(XAXIS); clear(YAXIS); }
401 else JSU.warn("attempted to set axis property to invalid value: " + s);
403 case "Minimized": if (parent == null && getSurface() != null) getSurface().minimized = JSU.toBoolean(value); // FEATURE
404 case "Maximized": if (parent == null && getSurface() != null) getSurface().maximized = JSU.toBoolean(value); // FEATURE
405 case "Close": if (parent == null && getSurface() != null) getSurface().dispose(true);
407 for(Box cur = (Box)value; cur != null || cur == redirect; cur = cur.parent)
408 if (cur == redirect) { redirect = (Box)value; return; }
409 JSU.error("redirect can only be set to a descendant of its current value");
410 case "fontsize": font = Font.getFont(font == null ? null : font.stream, JSU.toInt(value)); RECONSTRAIN(); dirty();
412 if(!(value instanceof Fountain)) throw new JSExn("You can only put streams to the font property");
413 //FIXME: if (font == value) return; // FIXME: unclone()
414 font = value == null ? null : Font.getFont((Fountain)value, font == null ? 10 : font.pointsize);
417 case "titlebar": if (getSurface()!=null) getSurface().setTitleBarText(JSU.toString(value)); super.put(name,value);
420 case "Press1": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
421 case "Press2": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
422 case "Press3": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
423 case "Release1": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
424 case "Release2": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
425 case "Release3": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
426 case "Click1": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
427 case "Click2": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
428 case "Click3": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
429 case "DoubleClick1": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
430 case "DoubleClick2": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
431 case "DoubleClick3": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
432 case "KeyPressed": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
433 case "KeyReleased": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
434 case "Move": if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
435 case "HScroll": if (!test(STOP_UPWARD_PROPAGATION) && parent != null)
436 parent.putAndTriggerTraps(name, JSU.N(JSU.toFloat(value) * ((float)parent.fontSize()) / ((float)fontSize())));
437 case "VScroll": if (!test(STOP_UPWARD_PROPAGATION) && parent != null)
438 parent.putAndTriggerTraps(name, JSU.N(JSU.toFloat(value) * ((float)parent.fontSize()) / ((float)fontSize())));
440 case "_Move": propagateDownward(name, value, false);
441 case "_Press1": propagateDownward(name, value, false);
442 case "_Press2": propagateDownward(name, value, false);
443 case "_Press3": propagateDownward(name, value, false);
444 case "_Release1": propagateDownward(name, value, false);
445 case "_Release2": propagateDownward(name, value, false);
446 case "_Release3": propagateDownward(name, value, false);
447 case "_Click1": propagateDownward(name, value, false);
448 case "_Click2": propagateDownward(name, value, false);
449 case "_Click3": propagateDownward(name, value, false);
450 case "_DoubleClick1": propagateDownward(name, value, false);
451 case "_DoubleClick2": propagateDownward(name, value, false);
452 case "_DoubleClick3": propagateDownward(name, value, false);
453 case "_KeyPressed": propagateDownward(name, value, false);
454 case "_KeyReleased": propagateDownward(name, value, false);
455 case "_HScroll": propagateDownward(name, value, false);
456 case "_VScroll": propagateDownward(name, value, false);
458 case "SizeChange": return;
459 case "ChildChange": return;
460 case "Enter": return;
461 case "Leave": return;
463 default: super.put(name, value);
467 private void setCursor(String value) throws JSExn {
468 if (value == null) { clear(CURSOR); boxToCursor.remove(this); return; }
469 if (value.equals(boxToCursor.get(this))) return;
471 boxToCursor.put(this, value);
472 Surface surface = getSurface();
473 if (surface == null) return;
474 String tempcursor = surface.cursor;
475 propagateDownward(null, null, false);
476 if (surface.cursor != tempcursor) surface.syncCursor();
479 private void setFill(JS value) throws JSExn {
481 if (texture == null && fillcolor == 0) return;
484 } else if (JSU.isString(value)) {
485 int newfillcolor = Color.stringToColor(JSU.toString(value));
486 if (newfillcolor == fillcolor) return;
487 fillcolor = newfillcolor;
490 Picture newtex = Picture.load((JS)value, this);
491 if (texture == newtex) return;
494 if (texture != null && texture.isLoaded) run(null);
500 * Handles events which propagate down the box tree. If obscured
501 * is set, then we merely check for Enter/Leave.
503 private void propagateDownward(JS name_, JS value, boolean obscured) throws JSExn {
505 String name = JSU.toString(name_);
506 if (getSurface() == null) return;
507 int x = (int)globalToLocalX(getSurface()._mousex);
508 int y = (int)globalToLocalY(getSurface()._mousey);
509 boolean wasinside = test(MOUSEINSIDE);
510 boolean isinside = test(VISIBLE) && inside(x, y) && !obscured;
511 if (!wasinside && isinside) {
513 putAndTriggerTrapsAndCatchExceptions(JSU.S("Enter"), JSU.T);
515 if (isinside && test(CURSOR)) getSurface().cursor = (String)boxToCursor.get(this);
516 if (wasinside && !isinside) {
518 putAndTriggerTrapsAndCatchExceptions(JSU.S("Leave"), JSU.T);
521 boolean found = false;
522 if (wasinside || isinside)
523 for(Box child = getChild(treeSize() - 1); child != null; child = child.prevSibling()) {
524 boolean save_stop = child.test(STOP_UPWARD_PROPAGATION);
526 if (name.equals("_HScroll") || name.equals("_VScroll"))
527 value2 = JSU.N(JSU.toFloat(value) * ((float)child.fontSize()) / (float)fontSize());
528 if (obscured /*|| !child.inside(x - child.x, y - child.y) FIXME FIXME */) {
529 child.propagateDownward(name_, value2, true);
532 child.clear(STOP_UPWARD_PROPAGATION);
533 if (name != null) child.putAndTriggerTrapsAndCatchExceptions(name_, value2);
534 else child.propagateDownward(name_, value2, obscured);
536 if (save_stop) child.set(STOP_UPWARD_PROPAGATION); else child.clear(STOP_UPWARD_PROPAGATION);
539 if (child.inside(x - child.x, y - child.y))
540 if (name != null && name.equals("_Move")) obscured = true;
545 if (!obscured && !found)
546 if ("_Move".equals(name) || name.startsWith("_Release") || wasinside)
548 putAndTriggerTrapsAndCatchExceptions(JSU.S(name.substring(1)), value);
551 /** figures out what box in this subtree of the Box owns the pixel at x,y relitave to the Surface */
552 public static Box whoIs(Box cur, int x, int y) {
554 if (cur.parent != null) throw new Error("whoIs may only be invoked on the root box of a surface");
558 // WARNING: this method is called from the event-queueing thread -- it may run concurrently with
559 // ANY part of Ibex, and is UNSYNCHRONIZED for performance reasons. BE CAREFUL HERE.
561 if (!cur.test(VISIBLE)) return null;
562 if (!cur.inside(x - globalx, y - globaly)) return cur.parent == null ? cur : null;
564 for(int i=cur.treeSize() - 1; i>=0; i--) {
565 Box child = cur.getChild(i);
566 if (child == null) continue; // since this method is unsynchronized, we have to double-check
567 globalx += child.transform.e;
568 globaly += child.transform.f;
569 if (child.test(VISIBLE) && child.inside(x - globalx, y - globaly)) { cur = child; continue OUTER; }
570 globalx -= child.transform.e;
571 globaly -= child.transform.f;
579 // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
581 public final int fontSize() { return font == null ? DEFAULT_FONT.pointsize : font.pointsize; }
582 public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); }
583 public Box getRoot() { return parent == null ? this : parent.getRoot(); }
584 public Surface getSurface() { return Surface.fromBox(getRoot()); }
585 private final boolean packed() { return test(YAXIS) || test(XAXIS); }
586 Box nextPackedSibling() { Box b = nextSibling(); return b == null || (b.packed() && b.test(VISIBLE))?b:b.nextPackedSibling(); }
587 Box firstPackedChild() { Box b = getChild(0); return b == null || (b.packed() && b.test(VISIBLE))?b:b.nextPackedSibling(); }
588 public float globalToLocalX(float x) { return parent == null ? x : parent.globalToLocalX(x - transform.e); }
589 public float globalToLocalY(float y) { return parent == null ? y : parent.globalToLocalY(y - transform.f); }
590 public float localToGlobalX(float x) { return parent == null ? x : parent.globalToLocalX(x + transform.e); }
591 public float localToGlobalY(float y) { return parent == null ? y : parent.globalToLocalY(y + transform.f); }
593 static short min(short a, short b) { if (a<b) return a; else return b; }
594 static int min(int a, int b) { if (a<b) return a; else return b; }
595 static float min(float a, float b) { if (a<b) return a; else return b; }
597 static short max(short a, short b) { if (a>b) return a; else return b; }
598 static int max(int a, int b) { if (a>b) return a; else return b; }
599 static float max(float a, float b) { if (a>b) return a; else return b; }
601 static 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; }
602 static 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; }
603 static int bound(int a, int b, int c) { if (c < b) return c; if (a > b) return a; return b; }
604 final boolean inside(int x, int y) { return test(VISIBLE) && x >= 0 && y >= 0 && x < width && y < height; }
606 void set(int mask) { flags |= mask; }
607 void set(int mask, boolean setclear) { if (setclear) set(mask); else clear(mask); }
608 void clear(int mask) { flags &= ~mask; }
609 public boolean test(int mask) { return ((flags & mask) == mask); }
612 // Tree Handling //////////////////////////////////////////////////////////////////////
614 public final int getIndexInParent() { return parent == null ? 0 : parent.indexNode(this); }
615 public final Box nextSibling() { return parent == null ? null : parent.getChild(parent.indexNode(this) + 1); }
616 public final Box prevSibling() { return parent == null ? null : parent.getChild(parent.indexNode(this) - 1); }
617 public final Box getChild(int i) {
618 if (i < 0) return null;
619 if (i >= treeSize()) return null;
620 return (Box)getNode(i);
623 // Tree Manipulation /////////////////////////////////////////////////////////////////////
625 public void removeSelf() {
626 if (parent != null) { parent.removeChild(parent.indexNode(this)); return; }
627 Surface surface = Surface.fromBox(this);
628 if (surface != null) surface.dispose(true);
631 /** remove the i^th child */
632 public void removeChild(int i) {
636 b.clear(MOUSEINSIDE);
640 putAndTriggerTrapsAndCatchExceptions(JSU.S("ChildChange"), b);
643 public void put(int i, JS value) throws JSExn {
646 if (value != null && !(value instanceof Box)) {
647 if (Log.on) JSU.warn("attempt to set a numerical property on a box to a non-box");
651 if (redirect == null) {
652 if (value == null) putAndTriggerTrapsAndCatchExceptions(JSU.S("ChildChange"), getChild(i));
653 else JSU.warn("attempt to add/remove children to/from a node with a null redirect");
655 } else if (redirect != this) {
656 if (value != null) putAndTriggerTrapsAndCatchExceptions(JSU.S("ChildChange"), value);
657 redirect.put(i, value);
659 Box b = (Box)redirect.get(JSU.N(i));
660 if (b != null) putAndTriggerTrapsAndCatchExceptions(JSU.S("ChildChange"), b);
663 } else if (value == null) {
664 if (i < 0 || i > treeSize()) return;
667 putAndTriggerTrapsAndCatchExceptions(JSU.S("ChildChange"), b);
672 // check if box being moved is currently target of a redirect
673 for(Box cur = b.parent; cur != null; cur = cur.parent)
674 if (cur.redirect == b) {
675 if (Log.on) JSU.warn("attempt to move a box that is the target of a redirect");
679 // check for recursive ancestor violation
680 for(Box cur = this; cur != null; cur = cur.parent)
682 if (Log.on) JSU.warn("attempt to make a node a parent of its own ancestor");
683 if (Log.on) Log.info(this, "box == " + this + " ancestor == " + b);
687 if (b.parent != null) b.parent.removeChild(b.parent.indexNode(b));
691 // need both of these in case child was already uncalc'ed
696 putAndTriggerTrapsAndCatchExceptions(JSU.S("ChildChange"), b);
700 public void putAndTriggerTrapsAndCatchExceptions(JS name, JS val) {
702 putAndTriggerTraps(name, val);
704 JSU.log("caught js exception while putting to trap \""+ JSU.str(name)+"\"");
706 } catch (Exception e) {
707 JSU.log("caught exception while putting to trap \""+ JSU.str(name)+"\"");
712 // BalancedTree functions
713 private void insertNode(int p, Box b) { if(bt == null) bt = new BalancedTree(); bt.insertNode(p,b); }
714 private int treeSize() { return bt == null ? 0 : bt.treeSize(); }
715 private int indexNode(Box b) { return bt == null ? -1 : bt.indexNode(b); }
716 private void deleteNode(int p) { bt.deleteNode(p); }
717 private Box getNode(int p) { return (Box)bt.getNode(p); }
720 final static JS.Method METHOD = new JS.Method();
721 final static Basket.Map boxToCursor = new Basket.Hash(500, 3);
722 final static JS SIZECHANGE = JSU.S("SizeChange");
723 final static Font DEFAULT_FONT = Font.getFont(Main.vera, 10);
726 // Flags //////////////////////////////////////////////////////////////////////
728 public static final int MOUSEINSIDE = 0x00000001;
729 public static final int XAXIS = 0x00000002;
730 public static final int YAXIS = 0x00000004;
731 public static final int VISIBLE = 0x00000008;
732 public static final int VSHRINK = 0x00000010;
733 public static final int HSHRINK = 0x00000020;
734 public static final int RECONSTRAIN = 0x00000040;
735 public static final int REPLACE = 0x00000080;
737 // if true, this box has cursor in the cursor hash; FEATURE: GC issues?
738 public static final int CURSOR = 0x00010000;
739 public static final int CLIP = 0x00020000;
740 public static final int STOP_UPWARD_PROPAGATION = 0x00040000;
741 public static final int MOVED = 0x00080000;