+++ /dev/null
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.xwt;
-
-import java.util.*;
-import java.net.*;
-import java.text.*;
-import org.xwt.js.*;
-import org.xwt.util.*;
-
-/**
- * A helper class for properties of Box which require special
- * handling.
- *
- * To avoid excessive use of String.equals(), the Box.get() and
- * Box.put() methods employ a Hash keyed on property names that
- * require special handling. The value stored in the Hash is an
- * instance of an anonymous subclass of SpecialBoxProperty, which knows
- * how to handle get()s and put()s for that property name. There
- * should be one anonymous subclass of SpecialBoxProperty for each
- * specially-handled property on Box.
- */
-class SpecialBoxProperty {
-
- SpecialBoxProperty() { }
- private static final int NUMINTS = Box.NUMINTS;
- private static final int dmax = Box.dmax;
- private static final int dmin = Box.dmin;
- private static final int cmin = Box.cmin;
- private static final int abs = Box.abs;
- private static final int pos = Box.pos;
- private static final int size = Box.size;
- private static final int oldpos = Box.oldpos;
- private static final int oldsize = Box.oldsize;
- private static final int pad = Box.pad;
-
- /** stores instances of SpecialBoxProperty; keyed on property name */
- static Hash specialBoxProperties = new Hash(200, 3);
-
- /** this method defines the behavior when the property is get()ed from b */
- Object get(Box b) { return null; }
-
- /** this method defines the behavior when the property is put() to b */
- void put(Box b, Object value) { }
-
- /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
- void put(String name, Box b, Object value) { put(b, value); }
-
- // 'ye olde IBM CGA colours', as defined in the XWT reference
- static final int black = 0xFF000000;
- static final int blue = 0xFF000088;
- static final int green = 0xFF008800;
- static final int cyan = 0xFF008888;
- static final int red = 0xFF880000;
- static final int magenta = 0xFF880088;
- static final int brown = 0xFF888800;
- static final int lightGray = 0xFFBDBEBD;
- static final int darkGray = 0xFF242424;
- static final int brightBlue = 0xFF0000FF;
- static final int brightGreen = 0xFF00FF00;
- static final int brightCyan = 0xFF00FFFF;
- static final int brightRed = 0xFFFF0000;
- static final int pink = 0xFFFF00FF;
- static final int yellow = 0xFFFFFF00;
- static final int white = 0xFFFFFFFF;
-
- static {
- specialBoxProperties.put("color", new SpecialBoxProperty() {
- public Object get(Box b) {
- if ((b.color & 0xFF000000) == 0) return null;
- String red = Integer.toHexString((b.color & 0x00FF0000) >> 16);
- String green = Integer.toHexString((b.color & 0x0000FF00) >> 8);
- String blue = Integer.toHexString(b.color & 0x000000FF);
- if (red.length() < 2) red = "0" + red;
- if (blue.length() < 2) blue = "0" + blue;
- if (green.length() < 2) green = "0" + green;
- return "#" + red + green + blue;
- }
- public void put(Box b, Object value) {
- int newcolor = b.color;
- String s = value == null ? null : value.toString();
- if (value == null) newcolor = 0x00000000;
- else if (s.length() > 0 && s.charAt(0) == '#')
- try {
- newcolor = 0xFF000000 |
- (Integer.parseInt(s.substring(1, 3), 16) << 16) |
- (Integer.parseInt(s.substring(3, 5), 16) << 8) |
- Integer.parseInt(s.substring(5, 7), 16);
- } catch (NumberFormatException e) {
- Log.log(this, "invalid color " + s);
- return;
- }
- else if (s.equals("black")) newcolor = black;
- else if (s.equals("blue")) newcolor = blue;
- else if (s.equals("green")) newcolor = green;
- else if (s.equals("cyan")) newcolor = cyan;
- else if (s.equals("red")) newcolor = red;
- else if (s.equals("magenta")) newcolor = magenta;
- else if (s.equals("brown")) newcolor = brown;
- else if (s.equals("lightGray")) newcolor = lightGray;
- else if (s.equals("darkGray")) newcolor = darkGray;
- else if (s.equals("brightBlue")) newcolor = brightBlue;
- else if (s.equals("brightGreen")) newcolor = brightGreen;
- else if (s.equals("brightCyan")) newcolor = brightCyan;
- else if (s.equals("brightRed")) newcolor = brightRed;
- else if (s.equals("pink")) newcolor = pink;
- else if (s.equals("yellow")) newcolor = yellow;
- else if (s.equals("white")) newcolor = white;
- else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
- if (newcolor == b.color) return;
- b.color = newcolor;
- b.dirty();
- } });
-
-
- specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
- public Object get(Box b) {
- if ((b.textcolor & 0xFF000000) == 0) return null;
- String red = Integer.toHexString((b.textcolor & 0x00FF0000) >> 16);
- String green = Integer.toHexString((b.textcolor & 0x0000FF00) >> 8);
- String blue = Integer.toHexString(b.textcolor & 0x000000FF);
- if (red.length() < 2) red = "0" + red;
- if (blue.length() < 2) blue = "0" + blue;
- if (green.length() < 2) green = "0" + green;
- return "#" + red + green + blue;
- }
- public void put(Box b, Object value) {
- int newtextcolor = b.color;
- if (value == null) return;
- String s = value.toString();
- if (s.length() > 0 && s.charAt(0) == '#')
- newtextcolor = 0xFF000000 |
- (Integer.parseInt(s.substring(1, 3), 16) << 16) |
- (Integer.parseInt(s.substring(3, 5), 16) << 8) |
- Integer.parseInt(s.substring(5, 7), 16);
- else if (s.equals("black")) newtextcolor = black;
- else if (s.equals("blue")) newtextcolor = blue;
- else if (s.equals("green")) newtextcolor = green;
- else if (s.equals("cyan")) newtextcolor = cyan;
- else if (s.equals("red")) newtextcolor = red;
- else if (s.equals("magenta")) newtextcolor = magenta;
- else if (s.equals("brown")) newtextcolor = brown;
- else if (s.equals("lightGray")) newtextcolor = lightGray;
- else if (s.equals("darkGray")) newtextcolor = darkGray;
- else if (s.equals("brightBlue")) newtextcolor = brightBlue;
- else if (s.equals("brightGreen")) newtextcolor = brightGreen;
- else if (s.equals("brightCyan")) newtextcolor = brightCyan;
- else if (s.equals("brightRed")) newtextcolor = brightRed;
- else if (s.equals("pink")) newtextcolor = pink;
- else if (s.equals("yellow")) newtextcolor = yellow;
- else if (s.equals("white")) newtextcolor = white;
- else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
- if (newtextcolor == b.textcolor) return;
- b.textcolor = newtextcolor;
- b.dirty();
- } });
-
- specialBoxProperties.put("text", new SpecialBoxProperty() {
- public Object get(Box b) { return b.text; }
- public void put(Box b, Object value) {
- String t = value == null ? "null" : value.toString();
- if (t.equals(b.text)) return;
-
- for(int i=0; i<t.length(); i++)
- if (Character.isISOControl(t.charAt(i))) {
- if (Log.on) Log.log(this,
- "ISO Control characters are not permitted in box text strings; offending character is ASCII " +
- ((int)t.charAt(i)));
- return;
- }
-
- // FEATURE: try removing the following line; it appears to be redundant
- b.dirty();
- b.text = t;
- b.textupdate();
- b.dirty();
- } });
-
- specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
- public Object get(Box b) { return b; }
- public void put(Box b, Object value) {
- if (value == null) b.remove();
- else if (value.equals("window") || value.equals("frame")) {
- if (b.redirect != b && Log.on)
- Log.log(this, "WARNING: you have created a surface whose root box's redirect is not itself " +
- "-- this isn't usually a good idea");
- Platform.createSurface(b, value.equals("frame"), true);
- } else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
- }
- });
-
- specialBoxProperties.put("orient", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return "horizontal";
- else if (b.redirect != b) return get(b.redirect);
- else if (b.o == 1) return "vertical";
- else return "horizontal";
- }
- public void put(Box b, Object value) {
- if (value == null) return;
- if (b.redirect == null) return;
- if (b.redirect != b) {
- put(b.redirect, value);
- return;
- }
- if (value.equals("vertical")) {
- if (b.o == 1) return;
- b.o = 1;
- b.xo = 0;
- } else if (value.equals("horizontal")) {
- if (b.o == 0) return;
- b.o = 0;
- b.xo = 1;
- } else if (Log.on)
- Log.log(this, "invalid value put to orient property: " + value);
- b.mark_for_prerender();
- b.sync_cmin_to_children();
- } });
-
- specialBoxProperties.put("static", new SpecialBoxProperty() {
- public Object get(Box b) {
- String cfsn = JS.Thread.fromJavaThread(Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
- for(int i=0; i<cfsn.length() - 1; i++)
- if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
- cfsn = cfsn.substring(0, i);
- break;
- }
- return Static.getStatic(cfsn);
- }
- public void put(Box b, Object value) { }
- });
-
- specialBoxProperties.put("sizetoimage", new SpecialBoxProperty() {
- public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- if (stob(value)) {
- if (b.sizetoimage) return;
- b.sizetoimage = true;
- b.syncSizeToImage();
- } else {
- b.sizetoimage = false;
- }
- } });
-
- specialBoxProperties.put("fixedaspect", new SpecialBoxProperty() {
- public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newval = stob(value);
- if (newval == b.fixedaspect) return;
- b.fixedaspect = newval;
- b.dirty();
- } });
-
- specialBoxProperties.put("shrink", new SpecialBoxProperty() {
- public Object get(Box b) { return (b.vshrink && b.hshrink) ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newshrink = stob(value);
- if (b.hshrink == newshrink && b.vshrink == newshrink) return;
- b.hshrink = b.vshrink = newshrink;
- b.mark_for_prerender();
- } });
-
- specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
- public Object get(Box b) { return new Boolean(b.hshrink); }
- public void put(Box b, Object value) {
- boolean newshrink = stob(value);
- if (b.hshrink == newshrink) return;
- b.hshrink = newshrink;
- b.mark_for_prerender();
- }
- });
-
- specialBoxProperties.put("vshrink", new SpecialBoxProperty() {
- public Object get(Box b) { return b.vshrink ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newshrink = stob(value);
- if (b.vshrink == newshrink) return;
- b.vshrink = newshrink;
- b.mark_for_prerender();
- }
- });
-
- specialBoxProperties.put("x", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.surface == null) return new Integer(0);
- if (b.invisible) return new Integer(0);
- return new Integer(b.abs(0));
- }
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.setLocation(stoi(value), b.abs(1));
- b.surface.centerSurfaceOnRender = false;
- }
- b.set(abs, 0, stoi(value));
- }
- });
-
- specialBoxProperties.put("y", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.surface == null) return new Integer(0);
- if (b.invisible) return new Integer(0);
- return new Integer(b.abs(1));
- }
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.setLocation(b.abs(0), stoi(value));
- b.surface.centerSurfaceOnRender = false;
- }
- b.set(abs, 1, stoi(value));
- }
- });
-
- specialBoxProperties.put("width", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.size(0)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- if (b.getParent() == null && b.surface != null) {
- b.set(size, 0, Box.max(Surface.scarPicture.getWidth(), stoi(value)));
- b.mark_for_prerender();
- } else {
- b.set(dmax, 0, stoi(value));
- b.set(dmin, 0, stoi(value));
- }
- } });
-
- specialBoxProperties.put("height", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.size(1)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- if (b.getParent() == null && b.surface != null) {
- b.set(size, 1, Box.max(Surface.scarPicture.getHeight(), stoi(value)));
- b.mark_for_prerender();
- } else {
- b.set(dmax, 1, stoi(value));
- b.set(dmin, 1, stoi(value));
- }
- } });
-
- specialBoxProperties.put("flex", new SpecialBoxProperty() {
- public Object get(Box b) { return new Double(b.flex); }
- public void put(Box b, Object value) {
- if (value == null) return;
- int newflex = stoi(value);
- if (newflex == b.flex) return;
- b.flex = newflex;
- b.mark_for_prerender();
- } });
-
- specialBoxProperties.put("tile", new SpecialBoxProperty() {
- public Object get(Box b) { return b.tile ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newtile = stob(value);
- if (newtile == b.tile) return;
- b.tile = newtile;
- b.dirty();
- } });
-
- specialBoxProperties.put("align", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.align == -1) return "topleft";
- else if (b.align == 1) return "bottomright";
- else return "center";
- }
- public void put(Box b, Object value) {
- String s = value == null ? "" : value.toString();
- byte newalign = b.align;
- if (s.equals("topleft")) newalign = -1;
- else if (s.equals("bottomright")) newalign = 1;
- else if (s.equals("center")) newalign = 0;
- else if (Log.on) Log.log(this, "invalid value put to align property: " + value);
- if (newalign == b.align) return;
- b.align = newalign;
- if (b.getParent() != null) b.getParent().mark_for_prerender();
- } });
-
- specialBoxProperties.put("invisible", new SpecialBoxProperty() {
- public Object get(Box b) {
- for (Box cur = b; cur != null; cur = cur.getParent()) { if (cur.invisible) return Boolean.TRUE; }
- return Boolean.FALSE;
- }
- public void put(Box b, Object value) {
- boolean newinvisible = stob(value);
- if (newinvisible == b.invisible) return;
- b.invisible = newinvisible;
- if (b.getParent() == null) {
- if (b.surface != null) b.surface.setInvisible(newinvisible);
- } else {
- b.dirty();
- b.getParent().mark_for_prerender();
- b.getParent().sync_cmin_to_children();
- b.getParent().dirty(b.pos(0), b.pos(1), b.size(0), b.size(1));
- b.getParent().dirty(b.oldpos(0), b.oldpos(1), b.oldsize(0), b.oldsize(1));
- }
- }});
-
- specialBoxProperties.put("absolute", new SpecialBoxProperty() {
- public Object get(Box b) { return b.absolute ? Boolean.TRUE : Boolean.FALSE; }
- public void put(Box b, Object value) {
- boolean newabsolute = stob(value);
- if (newabsolute == b.absolute) return;
- b.absolute = newabsolute;
- if (b.getParent() != null) {
- b.getParent().mark_for_prerender();
- b.getParent().sync_cmin_to_children();
- }
- } });
-
- specialBoxProperties.put("image", new SpecialBoxProperty() {
- public Object get(Box b) { return b.image == null ? null : Box.imageToNameMap.get(b.image); }
- public void put(Box b, Object value) { b.setImage(value == null ? null : value.toString()); }
- });
-
- specialBoxProperties.put("border", new SpecialBoxProperty() {
- public Object get(Box b) { return b.border == null ? null : Box.imageToNameMap.get(b.border); }
- public void put(Box b, Object value) { b.setBorder(value == null ? null : value.toString()); }
- });
-
- specialBoxProperties.put("font", new SpecialBoxProperty() {
- public Object get(Box b) { return b.font; }
- public void put(Box b, Object value) {
- b.font = value == null ? null : value.toString();
- b.fontChanged();
- b.dirty();
- } });
-
- specialBoxProperties.put("globalx", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(0)); }
- public void put(Box b, Object value) {
- if (b.surface == null || b.getParent() == null) return;
- b.put("x", new Integer(stoi(value) - stoi(get(b.getParent()))));
- }
- });
-
- specialBoxProperties.put("globaly", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(1)); }
- public void put(Box b, Object value) {
- if (b.surface == null || b.getParent() == null) return;
- b.put("y", new Integer(stoi(value) - stoi(get(b.getParent()))));
- }
- });
-
- specialBoxProperties.put("cursor", new SpecialBoxProperty() {
- public Object get(Box b) { return b.cursor; }
- public void put(Box b, Object value) {
- b.cursor = (String)value;
- if (b.surface == null) return;
-
- // see if we need to update the surface cursor
- String tempcursor = b.surface.cursor;
- b.Move(b.surface.mousex, b.surface.mousey, b.surface.mousex, b.surface.mousey);
- if (b.surface.cursor != tempcursor) b.surface.syncCursor();
- }
- });
-
- specialBoxProperties.put("mousex", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousex - b.pos(0)); }
- });
-
- specialBoxProperties.put("mousey", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousey - b.pos(1)); }
- });
-
- specialBoxProperties.put("xwt", new SpecialBoxProperty() {
- public Object get(Box b) { return XWT.singleton; }
- });
-
- specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
- public Object get(Box b) { return b.mouseinside ? Boolean.TRUE : Boolean.FALSE; }
- });
-
- specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return new Integer(0);
- if (b.redirect != b) return get(b.redirect);
- return new Integer(b.numChildren());
- } });
-
- SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
- public void put(String name, Box b, Object value) {
- if (b.surface == null) return;
- for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
- Box siblingChild = c.whoIs(c.surface.mousex, c.surface.mousey);
- if (siblingChild != null) {
- siblingChild.put(name, value);
- return;
- }
- }
- if (b.getParent() != null)
- b.getParent().put(name, value);
- }};
-
- specialBoxProperties.put("Press1", mouseEventHandler);
- specialBoxProperties.put("Press2", mouseEventHandler);
- specialBoxProperties.put("Press3", mouseEventHandler);
- specialBoxProperties.put("Release1", mouseEventHandler);
- specialBoxProperties.put("Release2", mouseEventHandler);
- specialBoxProperties.put("Release3", mouseEventHandler);
- specialBoxProperties.put("Click1", mouseEventHandler);
- specialBoxProperties.put("Click2", mouseEventHandler);
- specialBoxProperties.put("Click3", mouseEventHandler);
- specialBoxProperties.put("DoubleClick1", mouseEventHandler);
- specialBoxProperties.put("DoubleClick2", mouseEventHandler);
- specialBoxProperties.put("DoubleClick3", mouseEventHandler);
-
- specialBoxProperties.put("root", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.surface == null) return null;
- else if (b.getRoot() == null) return null;
- else if (b.getParent() == null) return b;
- else return b.getRoot().getRootProxy();
- } });
-
- specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.getParent() == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
- else return null;
- }
- public void put(Box b, Object value) {
- if (b.surface == null) return;
- boolean val = stob(value);
- if (b.getParent() == null && b.surface.minimized != val) b.surface.setMinimized(val);
- }
- });
-
- specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.getParent() == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
- else return null;
- }
- public void put(Box b, Object value) {
- if (b.surface == null) return;
- boolean val = stob(value);
- if (b.getParent() == null && b.surface.maximized != val) b.surface.setMaximized(val);
- }
- });
-
- specialBoxProperties.put("toback", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toBack();
- }
- });
-
- specialBoxProperties.put("tofront", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toFront();
- }
- });
-
- specialBoxProperties.put("hscar", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.hscar = stoi(value);
- b.surface.dirty(0, 0, b.surface.width, b.surface.height);
- b.surface.Refresh();
- }
- }
- });
-
- specialBoxProperties.put("vscar", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) {
- b.surface.vscar = stoi(value);
- b.surface.dirty(0, 0, b.surface.width, b.surface.height);
- b.surface.Refresh();
- }
- }
- });
-
- specialBoxProperties.put("Close", new SpecialBoxProperty() {
- public void put(Box b, Object value) {
- if (b.getParent() == null && b.surface != null) b.surface.dispose(true);
- }
- });
-
- // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
- specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
- specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
- specialBoxProperties.put("PosChange", new SpecialBoxProperty());
- specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
-
- specialBoxProperties.put("hpad", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return new Integer(0);
- if (b.redirect != b) return get(b.redirect);
- return new Integer(b.pad(0));
- }
- public void put(Box b, Object value) {
- if (b.redirect == null) return;
- if (b.redirect != b) { put(b.redirect, value); return; }
- int newval = stoi(value);
- if (newval == b.pad(0)) return;
- b.set(pad, 0, newval);
- }
- });
-
- specialBoxProperties.put("vpad", new SpecialBoxProperty() {
- public Object get(Box b) {
- if (b.redirect == null) return new Integer(0);
- if (b.redirect != b) return get(b.redirect);
- return new Integer(b.pad(1));
- }
- public void put(Box b, Object value) {
- if (b.redirect == null) return;
- if (b.redirect != b) { put(b.redirect, value); return; }
- int newval = stoi(value);
- if (newval == b.pad(1)) return;
- b.set(pad, 1, newval);
- }
- });
-
- specialBoxProperties.put("indexof", new SpecialBoxProperty() {
- public Object get(Box b) { return b.indexof(); }
- });
-
- specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmin(0)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmin, 0, stoi(value));
- }
- });
-
- specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmax(0)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmax, 0, stoi(value));
- }
- });
-
- specialBoxProperties.put("minheight", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmin(1)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmin, 1, stoi(value));
- }
- });
-
- specialBoxProperties.put("maxheight", new SpecialBoxProperty() {
- public Object get(Box b) { return new Integer(b.dmax(1)); }
- public void put(Box b, Object value) {
- if (b.sizetoimage) return;
- b.set(dmax, 1, stoi(value));
- }
- });
-
- specialBoxProperties.put("redirect", new SpecialBoxProperty() {
- public void put(Box b, Object value) { }
- public Object get(Box b) {
- if (b.redirect == null) return null;
- if (b.redirect == b) return Boolean.TRUE;
- return get(b.redirect);
- }
- });
-
- specialBoxProperties.put("apply", new SpecialBoxProperty() {
- public void put(Box b, Object value) { }
- public Object get(final Box b) { return new Apply(b); }
- });
-
- specialBoxProperties.put("id", new SpecialBoxProperty() {
- public void put(Box b, Object value) { }
- public Object get(Box b) { return b.id; }
- });
- }
-
-
- /** helper that converts a String to a boolean according to JavaScript coercion rules */
- public static boolean stob(Object o) {
- if (o == null) return false;
- return Boolean.TRUE.equals(o) || "true".equals(o);
- }
-
- /** helper that converts a String to an int according to JavaScript coercion rules */
- public static int stoi(Object o) {
- if (o == null) return 0;
- if (o instanceof Integer) return ((Integer)o).intValue();
-
- String s;
- if (!(o instanceof String)) s = o.toString();
- else s = (String)o;
-
- try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
- catch (NumberFormatException e) { return 0; }
- }
-
- private static class Apply extends JS.Callable {
- Box b;
- public Apply(Box b) { this.b = b; }
- public Object call(JS.Array args) throws JS.Exn {
- if (args.elementAt(0) instanceof String) {
- String templatename = (String)args.elementAt(0);
- Template t = Template.getTemplate(templatename, null);
- if (t == null) {
- if (Log.on) Log.logJS(this, "template " + templatename + " not found");
- } else {
- if (ThreadMessage.suspendThread()) try {
- JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
- t.apply(b, null, null, callback, 0, t.numUnits());
- } finally {
- ThreadMessage.resumeThread();
- }
- }
-
- } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
- JS s = (JS)args.elementAt(0);
- Object[] keys = s.keys();
- for(int j=0; j<keys.length; j++) b.put(keys[j].toString(), s.get(keys[j]));
- }
-
- return b;
- }
- }
-}
-
-
-
else if (button == 2) button2 = true;
else if (button == 3) button3 = true;
- if (button == 1) new SimpleMessage("Press1", Boolean.TRUE, root.whoIs(mousex, mousey));
- else if (button == 2) new SimpleMessage("Press2", Boolean.TRUE, root.whoIs(mousex, mousey));
+ if (button == 1) new SimpleMessage("Press1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
+ else if (button == 2) new SimpleMessage("Press2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
else if (button == 3) {
- final Box who = root.whoIs(mousex, mousey);
+ final Box who = Box.whoIs(root, mousex, mousey);
MessageQueue.add(new Message() { public void perform() {
Platform.clipboardReadEnabled = true;
root.put("Press3", Boolean.TRUE);
else if (button == 2) button2 = false;
else if (button == 3) button3 = false;
- if (button == 1) new SimpleMessage("Release1", Boolean.TRUE, root.whoIs(mousex, mousey));
- else if (button == 2) new SimpleMessage("Release2", Boolean.TRUE, root.whoIs(mousex, mousey));
- else if (button == 3) new SimpleMessage("Release3", Boolean.TRUE, root.whoIs(mousex, mousey));
+ if (button == 1) new SimpleMessage("Release1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
+ else if (button == 2) new SimpleMessage("Release2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
+ else if (button == 3) new SimpleMessage("Release3", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
if (Platform.needsAutoClick() && Math.abs(last_press_x - mousex) < 5 && Math.abs(last_press_y - mousey) < 5) Click(button);
last_press_x = Integer.MAX_VALUE;
}
protected final void Click(int button) {
- if (button == 1) new SimpleMessage("Click1", Boolean.TRUE, root.whoIs(mousex, mousey));
- else if (button == 2) new SimpleMessage("Click2", Boolean.TRUE, root.whoIs(mousex, mousey));
- else if (button == 3) new SimpleMessage("Click3", Boolean.TRUE, root.whoIs(mousex, mousey));
+ if (button == 1) new SimpleMessage("Click1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
+ else if (button == 2) new SimpleMessage("Click2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
+ else if (button == 3) new SimpleMessage("Click3", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
if (Platform.needsAutoDoubleClick()) {
long now = System.currentTimeMillis();
if (lastClickButton == button && now - lastClickTime < 350) DoubleClick(button);
}
protected final void DoubleClick(int button) {
- if (button == 1) new SimpleMessage("DoubleClick1", Boolean.TRUE, root.whoIs(mousex, mousey));
- else if (button == 2) new SimpleMessage("DoubleClick2", Boolean.TRUE, root.whoIs(mousex, mousey));
- else if (button == 3) new SimpleMessage("DoubleClick3", Boolean.TRUE, root.whoIs(mousex, mousey));
+ if (button == 1) new SimpleMessage("DoubleClick1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
+ else if (button == 2) new SimpleMessage("DoubleClick2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
+ else if (button == 3) new SimpleMessage("DoubleClick3", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
}
/** sends a KeyPressed message; subclasses should not add the C- or A- prefixes, nor should they capitalize alphabet characters */
cursor = "default";
// Root gets motion events outside itself (if trapped, of course)
- if (root.is_trapped("Move") && !root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
+ if (!root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
root.put("Move", Boolean.TRUE);
root.Move(oldmousex, oldmousey, mousex, mousey);
}
protected final void PosChange(int x, int y) {
- root.set(Box.abs, 0, x);
- root.set(Box.abs, 1, y);
+ if (x != root.x) root.put("x", new Integer(x));
+ if (y != root.y) root.put("y", new Integer(y));
new SimpleMessage("PosChange", Boolean.TRUE, root);
}
width = Math.max(width, scarPicture.getWidth());
height = Math.max(height, scarPicture.getHeight());
dirty(hscar,
- root.size(1) - vscar - scarPicture.getHeight(),
+ root.height - vscar - scarPicture.getHeight(),
scarPicture.getWidth(), scarPicture.getHeight());
}
setSize(width, height);
} else {
root.remove();
}
- root.setSurface(this);
+ root.surface = this;
// make sure the root is properly sized
- while (root.needs_prerender || abort) {
- abort = false;
- root.prerender();
- }
+ do {
+ Box.abort = false;
+ root.reflow();
+ } while(Box.abort);
// this is a bit dangerous since we're passing ourselves to another method before subclasses' ctors have run...
backbuffer = Platform.createDoubleBuffer(Platform.getScreenWidth(), Platform.getScreenHeight(), this);
- root.mark_for_prerender();
root.dirty();
Refresh();
}
public synchronized void render() {
// if the window size changed as a result of a user action, we have to update the root box's size
- if (root.size(0) != width || root.size(1) != height) {
+ if (root.width != width || root.width != height) {
// since the scar will be moving, dirty the place it used to be
if (scarred) dirty(hscar,
- root.size(1) - vscar - scarPicture.getHeight(),
+ root.height - vscar - scarPicture.getHeight(),
scarPicture.getWidth(), scarPicture.getHeight());
// sort of ugly; we can't use set() here because it will cause an infinite mutual recursion
- root._size_0 = (int)width;
- root._size_1 = (int)height;
+ root.width = (int)width;
+ root.height = (int)height;
- root.mark_for_prerender();
+ root.needs_reflow = true;
root.put("SizeChange", Boolean.TRUE);
}
- while (root.needs_prerender || abort) {
- abort = false;
- root.prerender();
-
+ // make sure the root is properly sized
+ do {
+ Box.abort = false;
+ root.reflow();
// update mouseinside and trigger Enter/Leave as a result of box size/position changes
String oldcursor = cursor;
cursor = "default";
root.Move(mousex, mousey, mousex, mousey);
if (!cursor.equals(oldcursor)) syncCursor();
- }
+ } while(Box.abort);
if (centerSurfaceOnRender) {
centerSurfaceOnRender = false;
int x = (Platform.getScreenWidth() - width) / 2;
int y = (Platform.getScreenHeight() - height) / 2;
setLocation(x, y);
- root.set(Box.abs, 0, x);
- root.set(Box.abs, 1, y);
+ root.x = x;
+ root.y = y;
}
sizePosChangesSinceLastRender = 0;
int h = dirt[i][3];
if (x < 0) x = 0;
if (y < 0) y = 0;
- if (x+w > root.size(0)) w = root.size(0) - x;
- if (y+h > root.size(1)) h = root.size(1) - y;
+ if (x+w > root.width) w = root.width - x;
+ if (y+h > root.height) h = root.height - y;
if (w <= 0 || h <= 0) continue;
// if any part of this region falls within the "bad region", just skip it
+++ /dev/null
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.xwt;
-
-import java.io.*;
-import java.util.*;
-import org.xwt.util.*;
-
-/** encapsulates a single XWF font */
-public class XWF {
-
- /** all instances of XWF */
- private static Hash xwtfonts = new Hash();
-
- /** the int array comprising the XWF */
- int[] data = null;
-
- /** the width of the XWF's PNG */
- int w = 0;
-
- /** the height of the XWF's PNG */
- int h = 0;
-
- /** the full resource name of this font */
- String name = null;
-
- /** minimum descent of all glyphs */
- int maxascent = 0;
-
- /** maximum descent of all glyphs */
- int maxdescent = 0;
-
- /** hash containing all Picture instances created, keyed on an Integer object containing the argb color of the font */
- Hash pictures = new Hash();
-
- /** each element corresponds to a single glyph; <br>
- * metrics[glyphnum][0] == x position of left edge of glyph<br>
- * metrics[glyphnum][1] == x position of right edge of glyph<br>
- * metrics[glyphnum][2] == y position of top edge of glyph<br>
- * metrics[glyphnum][3] == y position of bottom edge of glyph<br>
- * metrics[glyphnum][4] == advance amount<br>
- * metrics[glyphnum][5] == baseline
- */
- int[][] metrics = null;
-
- /** drop all cached fonts when the theme mapping changes */
- public static void flushXWFs() { xwtfonts.clear(); }
-
- /** retrieve an XWF instance, creating it if needed */
- public static XWF getXWF(String resourcename) {
- XWF ret = (XWF)xwtfonts.get(resourcename);
- if (ret != null) return ret;
-
- String resolved = Resources.resolve(resourcename + ".xwf", null);
- byte[] bytes = Resources.getResource(resolved);
- if (bytes != null) {
- PNG png = PNG.decode(new ByteArrayInputStream(bytes), resourcename);
- if (png != null) return new XWF(resourcename, png);
- }
- return null;
- }
-
- public int getMaxAscent() { return maxascent; }
- public int getMaxDescent() { return maxdescent; }
-
- /** draws <tt>text</tt> on <tt>buf</tt> in this font, with color <tt>argb</tt> */
- public void drawString(DoubleBuffer buf, String text, int x, int y, int argb) {
-
- Integer color = new Integer(argb | 0xFF000000);
- Picture pg = (Picture)pictures.get(color);
- if (pg == null) {
- int[] data2 = new int[data.length];
- for(int i=0; i<data.length; i++)
- data2[i] = (data[i] & 0xFF000000) | (argb & 0x00FFFFFF);
- pg = Platform.createPicture(data2, w, h);
- pictures.put(color, pg);
- }
-
- int left = x;
- for(int i=0; i<text.length(); i++) {
- int c = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".indexOf(text.charAt(i));
- if (c == -1 || metrics[c] == null) {
- left += metrics[64] != null ? metrics[64][4] : 10;
- continue;
- }
- buf.drawPicture(pg,
- left,
- y - (metrics[c][5] - metrics[c][2]),
- left + metrics[c][1] - metrics[c][0],
- y - (metrics[c][5] - metrics[c][2]) + metrics[c][3] - metrics[c][2],
- metrics[c][0], metrics[c][2], metrics[c][1], metrics[c][3]);
-
- left += metrics[c][4];
- }
- }
-
- /** returns the width of <tt>text</tt> when rendered in this font */
- public int stringWidth(String text) {
- int ret = 0;
- for(int i=0; i<text.length(); i++) {
- // what a hack...
- int c = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".indexOf(text.charAt(i));
- if (c == -1 || metrics[c] == null) { ret += metrics[64][4]; continue; }
- ret += metrics[c][4];
- }
- return ret;
- }
-
- private XWF(String name, PNG png) {
- this.name = name;
- xwtfonts.put(name, this);
-
- data = png.getData();
- w = png.getWidth();
- h = png.getHeight();
-
- int x, y, y1;
- boolean breakout;
-
- int start_x, start_y, end_x, end_y, advance;
-
- int[] rows = new int[120];
-
- int baseline = 0;
-
- metrics = new int[96][];
- int numglyphs = 0;
-
- for(y=0; y<h; y++) {
-
- // search for the next non-empty row
- for(breakout = false; y<h && !breakout; y++)
- for(x = 0; x<w; x++)
- if ((data[x + y * w] & 0x00FFFFFF) != 0x00FFFFFF) breakout = true;
-
- start_y = y - 2;
-
- // search for the next empty row
- for(breakout = false; y<h && !breakout; y++)
- for(x = 0, breakout = true; x<w; x++)
- if ((data[x + y * w] & 0x00FFFFFF) != 0x00FFFFFF) breakout = false;
-
- end_y = y;
-
- if (start_y == end_y) continue;
-
- for(x=0; x<w; x++) {
-
- // search for the next column with a non-grayscale pixel in it
- for(breakout = false; x<w && !breakout; x++)
- for(y1 = start_y; y1<end_y; y1++)
- if (Math.abs(((data[x + y1 * w] & 0x00FF0000) >> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 ||
- Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) {
- breakout = true;
- baseline = y1;
- }
-
- // search for the next column without a non-grayscale pixel in it
- for(breakout = false; x<w && !breakout; x++)
- for(y1 = start_y, breakout = true; y1<end_y; y1++)
- if (Math.abs(((data[x + y1 * w] & 0x00FF0000) >> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 ||
- Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) {
- breakout = false;
- }
-
- x--;
- start_x = x;
-
- // search for the next column with a non-grayscale pixel in it
- for(breakout = false; x<w && !breakout; x++)
- for(y1 = start_y; y1<end_y; y1++)
- if (Math.abs(((data[x + y1 * w] & 0x00FF0000) >> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 ||
- Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) {
- breakout = true;
- baseline = y1;
- }
-
- x--;
- advance = x - start_x;
-
- // search for the next column without a grayscale pixel in it
- for(breakout = false; x<w && !breakout; x++)
- for(y1 = start_y, breakout = true; y1<end_y; y1++)
- if (Math.abs(((data[x + y1 * w] & 0x00FF0000) >> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 ||
- Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) {
- breakout = false;
- }
-
- x--;
- end_x = x;
-
- if (start_x == end_x) break;
-
- metrics[numglyphs] = new int[6];
- metrics[numglyphs][0] = start_x;
- metrics[numglyphs][1] = end_x;
- metrics[numglyphs][2] = start_y;
- metrics[numglyphs][3] = end_y;
- metrics[numglyphs][4] = advance;
- metrics[numglyphs][5] = baseline;
- numglyphs++;
-
- if (numglyphs >= metrics.length) break;
-
- }
-
- if (numglyphs >= metrics.length) break;
- }
-
- for(int i=0; i<data.length; i++)
- if (Math.abs(((data[i] & 0x00FF0000) >> 16) - ((data[i] & 0x0000FF00) >> 8)) > 10 ||
- Math.abs(((data[i] & 0x0000FF00) >> 8) - ((data[i] & 0x000000FF))) > 10)
- data[i] = 0x00;
- else
- data[i] = (0xFF - (data[i] & 0xFF)) << 24;
-
- for(int i=33; i<=126; i++)
- if (metrics[i - 33] != null)
- maxascent = Math.max(maxascent, metrics[i - 33][5] - metrics[i - 33][2]);
-
- for(int i=33; i<=126; i++)
- if (metrics[i - 33] != null)
- maxdescent = Math.max(maxdescent, metrics[i - 33][3] - metrics[i - 33][5]);
-
- }
-
-}