068d4a140a2e434071f7a7a25ec772e8f508e614
[org.ibex.core.git] / src / org / xwt / SpecialBoxProperty.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.util.*;
5 import java.net.*;
6 import java.text.*;
7 import org.xwt.js.*;
8 import org.xwt.util.*;
9
10 /** 
11  *  A helper class for properties of Box which require special
12  *  handling.
13  *
14  *  To avoid excessive use of String.equals(), the Box.get() and
15  *  Box.put() methods employ a Hash keyed on property names that
16  *  require special handling. The value stored in the Hash is an
17  *  instance of an anonymous subclass of SpecialBoxProperty, which knows
18  *  how to handle get()s and put()s for that property name. There
19  *  should be one anonymous subclass of SpecialBoxProperty for each
20  *  specially-handled property on Box.
21  */
22 class SpecialBoxProperty {
23
24     SpecialBoxProperty() { }
25     private static final int NUMINTS = Box.NUMINTS;
26     private static final int dmax = Box.dmax;
27     private static final int dmin = Box.dmin;
28     private static final int cmin = Box.cmin;
29     private static final int abs = Box.abs;
30     private static final int pos = Box.pos;
31     private static final int size = Box.size;
32     private static final int oldpos = Box.oldpos;
33     private static final int oldsize = Box.oldsize;
34     private static final int pad = Box.pad;
35
36     /** stores instances of SpecialBoxProperty; keyed on property name */
37     static Hash specialBoxProperties = new Hash(200, 3);
38
39     /** this method defines the behavior when the property is get()ed from b */
40     Object get(Box b) { return null; }
41
42     /** this method defines the behavior when the property is put() to b */
43     void put(Box b, Object value) { }
44
45     /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */
46     void put(String name, Box b, Object value) { put(b, value); }
47
48     // 'ye olde IBM CGA colours', as defined in the XWT reference
49     static final int black = 0xFF000000;
50     static final int blue = 0xFF000088;
51     static final int green = 0xFF008800;
52     static final int cyan = 0xFF008888;
53     static final int red = 0xFF880000;
54     static final int magenta = 0xFF880088;
55     static final int brown = 0xFF888800;
56     static final int lightGray = 0xFFBDBEBD;
57     static final int darkGray = 0xFF242424;
58     static final int brightBlue = 0xFF0000FF;
59     static final int brightGreen = 0xFF00FF00;
60     static final int brightCyan = 0xFF00FFFF;
61     static final int brightRed = 0xFFFF0000;
62     static final int pink = 0xFFFF00FF;
63     static final int yellow = 0xFFFFFF00;
64     static final int white = 0xFFFFFFFF;
65
66     static {
67
68         specialBoxProperties.put("color", new SpecialBoxProperty() {
69                 public Object get(Box b) {
70                     if ((b.color & 0xFF000000) == 0) return null;
71                     String red = Integer.toHexString((b.color & 0x00FF0000) >> 16);
72                     String green = Integer.toHexString((b.color & 0x0000FF00) >> 8);
73                     String blue = Integer.toHexString(b.color & 0x000000FF);
74                     if (red.length() < 2) red = "0" + red;
75                     if (blue.length() < 2) blue = "0" + blue;
76                     if (green.length() < 2) green = "0" + green;
77                     return "#" + red + green + blue;
78                 }
79                 public void put(Box b, Object value) {
80                     int newcolor = b.color;
81                     String s = value == null ? null : value.toString();
82                     if (value == null) newcolor = 0x00000000;
83                     else if (s.length() > 0 && s.charAt(0) == '#')
84                         try {
85                             newcolor = 0xFF000000 |
86                                 (Integer.parseInt(s.substring(1, 3), 16) << 16) |
87                                 (Integer.parseInt(s.substring(3, 5), 16) << 8) |
88                                 Integer.parseInt(s.substring(5, 7), 16);
89                         } catch (NumberFormatException e) {
90                             Log.log(this, "invalid color " + s);
91                             return;
92                         }
93                     else if (s.equals("black")) newcolor = black;
94                     else if (s.equals("blue")) newcolor = blue;
95                     else if (s.equals("green")) newcolor = green;
96                     else if (s.equals("cyan")) newcolor = cyan;
97                     else if (s.equals("red")) newcolor = red;
98                     else if (s.equals("magenta")) newcolor = magenta;
99                     else if (s.equals("brown")) newcolor = brown;
100                     else if (s.equals("lightGray")) newcolor = lightGray;
101                     else if (s.equals("darkGray")) newcolor = darkGray;
102                     else if (s.equals("brightBlue")) newcolor = brightBlue;
103                     else if (s.equals("brightGreen")) newcolor = brightGreen;
104                     else if (s.equals("brightCyan")) newcolor = brightCyan;
105                     else if (s.equals("brightRed")) newcolor = brightRed;
106                     else if (s.equals("pink")) newcolor = pink;
107                     else if (s.equals("yellow")) newcolor = yellow;
108                     else if (s.equals("white")) newcolor = white;
109                     else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
110                     if (newcolor == b.color) return;
111                     b.color = newcolor;
112                     b.dirty();
113                 } });
114         
115         
116         specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
117                 public Object get(Box b) {
118                     if ((b.textcolor & 0xFF000000) == 0) return null;
119                     String red = Integer.toHexString((b.textcolor & 0x00FF0000) >> 16);
120                     String green = Integer.toHexString((b.textcolor & 0x0000FF00) >> 8);
121                     String blue = Integer.toHexString(b.textcolor & 0x000000FF);
122                     if (red.length() < 2) red = "0" + red;
123                     if (blue.length() < 2) blue = "0" + blue;
124                     if (green.length() < 2) green = "0" + green;
125                     return "#" + red + green + blue;
126                 }
127                 public void put(Box b, Object value) {
128                     int newtextcolor = b.color;
129                     if (value == null) return;
130                     String s = value.toString();
131                     if (s.length() > 0 && s.charAt(0) == '#')
132                         newtextcolor = 0xFF000000 |
133                             (Integer.parseInt(s.substring(1, 3), 16) << 16) |
134                             (Integer.parseInt(s.substring(3, 5), 16) << 8) |
135                             Integer.parseInt(s.substring(5, 7), 16);
136                     else if (s.equals("black")) newtextcolor = black;
137                     else if (s.equals("blue")) newtextcolor = blue;
138                     else if (s.equals("green")) newtextcolor = green;
139                     else if (s.equals("cyan")) newtextcolor = cyan;
140                     else if (s.equals("red")) newtextcolor = red;
141                     else if (s.equals("magenta")) newtextcolor = magenta;
142                     else if (s.equals("brown")) newtextcolor = brown;
143                     else if (s.equals("lightGray")) newtextcolor = lightGray;
144                     else if (s.equals("darkGray")) newtextcolor = darkGray;
145                     else if (s.equals("brightBlue")) newtextcolor = brightBlue;
146                     else if (s.equals("brightGreen")) newtextcolor = brightGreen;
147                     else if (s.equals("brightCyan")) newtextcolor = brightCyan;
148                     else if (s.equals("brightRed")) newtextcolor = brightRed;
149                     else if (s.equals("pink")) newtextcolor = pink;
150                     else if (s.equals("yellow")) newtextcolor = yellow;
151                     else if (s.equals("white")) newtextcolor = white;
152                     else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\"");
153                     if (newtextcolor == b.textcolor) return;
154                     b.textcolor = newtextcolor;
155                     b.dirty();
156                 } });
157         
158         specialBoxProperties.put("text", new SpecialBoxProperty() {
159                 public Object get(Box b) { return b.text; }
160                 public void put(Box b, Object value) {
161                     String t = value == null ? "null" : value.toString();
162                     if (t.equals(b.text)) return;
163
164                     for(int i=0; i<t.length(); i++)
165                         if (Character.isISOControl(t.charAt(i))) {
166                             if (Log.on) Log.log(this, 
167                                                 "ISO Control characters are not permitted in box text strings; offending character is ASCII " +
168                                                ((int)t.charAt(i)));
169                             return;
170                         }
171
172                     // FEATURE: try removing the following line; it appears to be redundant
173                     b.dirty();
174                     b.text = t;
175                     b.textupdate();
176                     b.dirty();
177                 } });
178         
179         specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
180                 public Object get(Box b) { return b; }
181                 public void put(Box b, Object value) {
182                     if (value == null) b.remove();
183                     else if (value.equals("window") || value.equals("frame")) {
184                         if (b.redirect != b && Log.on)
185                             Log.log(this, "WARNING: you have created a surface whose root box's redirect is not itself " +
186                                     "-- this isn't usually a good idea");
187                         Platform.createSurface(b, value.equals("frame"), true);
188                     } else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
189                 }
190             });
191
192         specialBoxProperties.put("orient", new SpecialBoxProperty() {
193                 public Object get(Box b) {
194                     if (b.redirect == null) return "horizontal";
195                     else if (b.redirect != b) return get(b.redirect);
196                     else if (b.o == 1) return "vertical";
197                     else return "horizontal";
198                 }
199                 public void put(Box b, Object value) {
200                     if (value == null) return;
201                     if (b.redirect == null) return;
202                     if (b.redirect != b) {
203                         put(b.redirect, value);
204                         return;
205                     }
206                     if (value.equals("vertical")) {
207                         if (b.o == 1) return;
208                         b.o = 1;
209                         b.xo = 0;
210                     } else if (value.equals("horizontal")) {
211                         if (b.o == 0) return;
212                         b.o = 0;
213                         b.xo = 1;
214                     } else if (Log.on)
215                         Log.log(this, "invalid value put to orient property: " + value);
216                     b.mark_for_prerender();
217                     b.sync_cmin_to_children();
218                 } });
219
220         specialBoxProperties.put("static", new SpecialBoxProperty() {
221                 public Object get(Box b) {
222                     String cfsn = JS.Thread.fromJavaThread(Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
223                     for(int i=0; i<cfsn.length() - 1; i++)
224                         if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
225                             cfsn = cfsn.substring(0, i);
226                             break;
227                         }
228                     return Static.getStatic(cfsn);
229                 }
230                 public void put(Box b, Object value) { }
231             });
232
233         specialBoxProperties.put("sizetoimage", new SpecialBoxProperty() {
234                 public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
235                 public void put(Box b, Object value) {
236                     if (stob(value)) {
237                         if (b.sizetoimage) return;
238                         b.sizetoimage = true;
239                         b.syncSizeToImage();
240                     } else {
241                         b.sizetoimage = false;
242                     }
243                 } });
244         
245         specialBoxProperties.put("fixedaspect", new SpecialBoxProperty() {
246                 public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
247                 public void put(Box b, Object value) {
248                     boolean newval = stob(value);
249                     if (newval == b.fixedaspect) return;
250                     b.fixedaspect = newval;
251                     b.dirty();
252                 } });
253         
254         specialBoxProperties.put("shrink", new SpecialBoxProperty() {
255                 public Object get(Box b) { return (b.vshrink && b.hshrink) ? Boolean.TRUE : Boolean.FALSE; }
256                 public void put(Box b, Object value) {
257                     boolean newshrink = stob(value);
258                     if (b.hshrink == newshrink && b.vshrink == newshrink) return;
259                     b.hshrink = b.vshrink = newshrink;
260                     b.mark_for_prerender();
261                 } });
262         
263         specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
264                 public Object get(Box b) { return new Boolean(b.hshrink); }
265                 public void put(Box b, Object value) {
266                     boolean newshrink = stob(value);
267                     if (b.hshrink == newshrink) return;
268                     b.hshrink = newshrink;
269                     b.mark_for_prerender();
270                 }
271             });
272         
273         specialBoxProperties.put("vshrink", new SpecialBoxProperty() {
274                 public Object get(Box b) { return b.vshrink ? Boolean.TRUE : Boolean.FALSE; }
275                 public void put(Box b, Object value) {
276                     boolean newshrink = stob(value);
277                     if (b.vshrink == newshrink) return;
278                     b.vshrink = newshrink;
279                     b.mark_for_prerender();
280                 }
281             });
282         
283         specialBoxProperties.put("x", new SpecialBoxProperty() {
284                 public Object get(Box b) {
285                     if (b.surface == null) return new Integer(0);
286                     if (b.invisible) return new Integer(0);
287                     return new Integer(b.abs(0));
288                 }
289                 public void put(Box b, Object value) {
290                     if (b.getParent() == null && b.surface != null) {
291                         b.surface.setLocation(stoi(value), b.abs(1));
292                         b.surface.centerSurfaceOnRender = false;
293                     }
294                     b.set(abs, 0, stoi(value));
295                 }
296             });
297         
298         specialBoxProperties.put("y", new SpecialBoxProperty() {
299                 public Object get(Box b) {
300                     if (b.surface == null) return new Integer(0);
301                     if (b.invisible) return new Integer(0);
302                     return new Integer(b.abs(1));
303                 }
304                 public void put(Box b, Object value) {
305                     if (b.getParent() == null && b.surface != null) {
306                         b.surface.setLocation(b.abs(0), stoi(value));
307                         b.surface.centerSurfaceOnRender = false;
308                     }
309                     b.set(abs, 1, stoi(value));
310                 }
311             });
312
313         specialBoxProperties.put("width", new SpecialBoxProperty() {
314                 public Object get(Box b) { return new Integer(b.size(0)); }
315                 public void put(Box b, Object value) {
316                     if (b.sizetoimage) return;
317                     if (b.getParent() == null && b.surface != null) {
318                         b.set(size, 0, Box.max(Surface.scarPicture.getWidth(), stoi(value)));
319                         b.mark_for_prerender();
320                     } else {
321                         b.set(dmax, 0, stoi(value));
322                         b.set(dmin, 0, stoi(value));
323                     }
324                 } });
325         
326         specialBoxProperties.put("height", new SpecialBoxProperty() {
327                 public Object get(Box b) { return new Integer(b.size(1)); }
328                 public void put(Box b, Object value) {
329                     if (b.sizetoimage) return;
330                     if (b.getParent() == null && b.surface != null) {
331                         b.set(size, 1, Box.max(Surface.scarPicture.getHeight(), stoi(value)));
332                         b.mark_for_prerender();
333                     } else {
334                         b.set(dmax, 1, stoi(value));
335                         b.set(dmin, 1, stoi(value));
336                     }
337                 } });
338
339         specialBoxProperties.put("flex", new SpecialBoxProperty() {
340                 public Object get(Box b) { return new Double(b.flex); }
341                 public void put(Box b, Object value) {
342                     if (value == null) return;
343                     int newflex = stoi(value);
344                     if (newflex == b.flex) return;
345                     b.flex = newflex;
346                     b.mark_for_prerender();
347                 } });
348         
349         specialBoxProperties.put("tile", new SpecialBoxProperty() {
350                 public Object get(Box b) { return b.tile ? Boolean.TRUE : Boolean.FALSE; }
351                 public void put(Box b, Object value) {
352                     boolean newtile = stob(value);
353                     if (newtile == b.tile) return;
354                     b.tile = newtile;
355                     b.dirty();
356                 } });
357         
358         specialBoxProperties.put("align", new SpecialBoxProperty() {
359                 public Object get(Box b) {
360                     if (b.align == -1) return "topleft";
361                     else if (b.align == 1) return "bottomright";
362                     else return "center";
363                 }
364                 public void put(Box b, Object value) {
365                     String s = value == null ? "" : value.toString();
366                     byte newalign = b.align;
367                     if (s.equals("topleft")) newalign = -1;
368                     else if (s.equals("bottomright")) newalign = 1;
369                     else if (s.equals("center")) newalign = 0;
370                     else if (Log.on) Log.log(this, "invalid value put to align property: " + value);
371                     if (newalign == b.align) return;
372                     b.align = newalign;
373                     if (b.getParent() != null) b.getParent().mark_for_prerender();
374                 } });
375         
376         specialBoxProperties.put("invisible", new SpecialBoxProperty() {
377                 public Object get(Box b) {
378                     for (Box cur = b; cur != null; cur = cur.getParent()) { if (cur.invisible) return Boolean.TRUE; }
379                     return Boolean.FALSE;
380                 }
381                 public void put(Box b, Object value) {
382                     boolean newinvisible = stob(value);
383                     if (newinvisible == b.invisible) return;
384                     b.invisible = newinvisible;
385                     if (b.getParent() == null) {
386                         if (b.surface != null) b.surface.setInvisible(newinvisible);
387                     } else {
388                         b.dirty();
389                         b.getParent().mark_for_prerender();
390                         b.getParent().sync_cmin_to_children();
391                         b.getParent().dirty(b.pos(0), b.pos(1), b.size(0), b.size(1));
392                         b.getParent().dirty(b.oldpos(0), b.oldpos(1), b.oldsize(0), b.oldsize(1));
393                     }
394                 }});
395         
396         specialBoxProperties.put("absolute", new SpecialBoxProperty() {
397                 public Object get(Box b) { return b.absolute ? Boolean.TRUE : Boolean.FALSE; }
398                 public void put(Box b, Object value) {
399                     boolean newabsolute = stob(value);
400                     if (newabsolute == b.absolute) return;
401                     b.absolute = newabsolute;
402                     if (b.getParent() != null) {
403                         b.getParent().mark_for_prerender();
404                         b.getParent().sync_cmin_to_children();
405                     }
406                 } });
407         
408         specialBoxProperties.put("image", new SpecialBoxProperty() {
409                 public Object get(Box b) { return b.image == null ? null : Box.imageToNameMap.get(b.image); }
410                 public void put(Box b, Object value) { b.setImage(value == null ? null : value.toString()); }
411             });
412
413         specialBoxProperties.put("border", new SpecialBoxProperty() {
414                 public Object get(Box b) { return b.border == null ? null : Box.imageToNameMap.get(b.border); }
415                 public void put(Box b, Object value) { b.setBorder(value == null ? null : value.toString()); }
416             });
417
418         specialBoxProperties.put("font", new SpecialBoxProperty() {
419                 public Object get(Box b) { return b.font; }
420                 public void put(Box b, Object value) {
421                     b.font = value == null ? null : value.toString();
422                     b.fontChanged();
423                     b.dirty();
424                 } });
425         
426         specialBoxProperties.put("globalx", new SpecialBoxProperty() {
427                 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(0)); }
428                 public void put(Box b, Object value) {
429                     if (b.surface == null || b.getParent() == null) return;
430                     b.put("x", new Integer(stoi(value) - stoi(get(b.getParent()))));
431                 }
432             });
433         
434         specialBoxProperties.put("globaly", new SpecialBoxProperty() {
435                 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(1)); }
436                 public void put(Box b, Object value) {
437                     if (b.surface == null || b.getParent() == null) return;
438                     b.put("y", new Integer(stoi(value) - stoi(get(b.getParent()))));
439                 }
440             });
441         
442         specialBoxProperties.put("cursor", new SpecialBoxProperty() {
443                 public Object get(Box b) { return b.cursor; } 
444                 public void put(Box b, Object value) {
445                     b.cursor = (String)value;
446                     if (b.surface == null) return;
447
448                     // see if we need to update the surface cursor
449                     String tempcursor = b.surface.cursor;
450                     b.Move(b.surface.mousex, b.surface.mousey, b.surface.mousex, b.surface.mousey);
451                     if (b.surface.cursor != tempcursor) b.surface.syncCursor();
452                 } 
453             });
454         
455         specialBoxProperties.put("mousex", new SpecialBoxProperty() {
456                 public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousex - b.pos(0)); }
457             });
458         
459         specialBoxProperties.put("mousey", new SpecialBoxProperty() {
460                 public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousey - b.pos(1)); }
461             });
462         
463         specialBoxProperties.put("xwt", new SpecialBoxProperty() {
464                 public Object get(Box b) { return XWT.singleton; }
465             });
466         
467         specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
468                 public Object get(Box b) { return b.mouseinside ? Boolean.TRUE : Boolean.FALSE; }
469             });
470         
471         specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
472                 public Object get(Box b) {
473                     if (b.redirect == null) return new Integer(0);
474                     if (b.redirect != b) return get(b.redirect);
475                     return new Integer(b.numChildren());
476                 } });
477         
478         SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
479                 public void put(String name, Box b, Object value) {
480                     if (b.surface == null) return;
481                     for(Box c = b.prevSibling(); c != null; c = c.prevSibling()) {
482                         Box siblingChild = c.whoIs(c.surface.mousex, c.surface.mousey);
483                         if (siblingChild != null) {
484                             siblingChild.put(name, value);
485                             return;
486                         }
487                     }
488                     if (b.getParent() != null)
489                         b.getParent().put(name, value);
490                 }};
491
492         specialBoxProperties.put("Press1", mouseEventHandler);
493         specialBoxProperties.put("Press2", mouseEventHandler);
494         specialBoxProperties.put("Press3", mouseEventHandler);
495         specialBoxProperties.put("Release1", mouseEventHandler);
496         specialBoxProperties.put("Release2", mouseEventHandler);
497         specialBoxProperties.put("Release3", mouseEventHandler);
498         specialBoxProperties.put("Click1", mouseEventHandler);
499         specialBoxProperties.put("Click2", mouseEventHandler);
500         specialBoxProperties.put("Click3", mouseEventHandler);
501         specialBoxProperties.put("DoubleClick1", mouseEventHandler);
502         specialBoxProperties.put("DoubleClick2", mouseEventHandler);
503         specialBoxProperties.put("DoubleClick3", mouseEventHandler);
504
505         specialBoxProperties.put("root", new SpecialBoxProperty() {
506                 public Object get(Box b) {
507                     if (b.surface == null) return null;
508                     else if (b.getRoot() == null) return null;
509                     else if (b.getParent() == null) return b;
510                     else return b.getRoot().getRootProxy();
511                 } });
512
513         specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
514                 public Object get(Box b) {
515                     if (b.getParent() == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
516                     else return null;
517                 }
518                 public void put(Box b, Object value) {
519                     if (b.surface == null) return;
520                     boolean val = stob(value);
521                     if (b.getParent() == null && b.surface.minimized != val) b.surface.setMinimized(val);
522                 }
523             });
524
525         specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
526                 public Object get(Box b) {
527                     if (b.getParent() == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
528                     else return null;
529                 }
530                 public void put(Box b, Object value) {
531                     if (b.surface == null) return;
532                     boolean val = stob(value);
533                     if (b.getParent() == null && b.surface.maximized != val) b.surface.setMaximized(val);
534                 }
535             });
536
537         specialBoxProperties.put("toback", new SpecialBoxProperty() {
538                 public void put(Box b, Object value) {
539                     if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toBack();
540                 }
541             });
542
543         specialBoxProperties.put("tofront", new SpecialBoxProperty() {
544                 public void put(Box b, Object value) {
545                     if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toFront();
546                 }
547             });
548
549         specialBoxProperties.put("hscar", new SpecialBoxProperty() {
550                 public void put(Box b, Object value) {
551                     if (b.getParent() == null && b.surface != null) {
552                         b.surface.hscar = stoi(value);
553                         b.surface.dirty(0, 0, b.surface.width, b.surface.height);
554                         b.surface.Refresh();
555                     }
556                 }
557             });
558
559         specialBoxProperties.put("vscar", new SpecialBoxProperty() {
560                 public void put(Box b, Object value) {
561                     if (b.getParent() == null && b.surface != null) {
562                         b.surface.vscar = stoi(value);
563                         b.surface.dirty(0, 0, b.surface.width, b.surface.height);
564                         b.surface.Refresh();
565                     }
566                 }
567             });
568
569         specialBoxProperties.put("Close", new SpecialBoxProperty() {
570                 public void put(Box b, Object value) {
571                     if (b.getParent() == null && b.surface != null) b.surface.dispose(true);
572                 }
573             });
574
575         // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
576         specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
577         specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
578         specialBoxProperties.put("PosChange", new SpecialBoxProperty());
579         specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
580
581         specialBoxProperties.put("hpad", new SpecialBoxProperty() {
582                 public Object get(Box b) {
583                     if (b.redirect == null) return new Integer(0);
584                     if (b.redirect != b) return get(b.redirect);
585                     return new Integer(b.pad(0));
586                 }
587                 public void put(Box b, Object value) {
588                     if (b.redirect == null) return;
589                     if (b.redirect != b) { put(b.redirect, value); return; }
590                     int newval = stoi(value);
591                     if (newval == b.pad(0)) return;
592                     b.set(pad, 0, newval);
593                 }
594             });
595
596         specialBoxProperties.put("vpad", new SpecialBoxProperty() {
597                 public Object get(Box b) {
598                     if (b.redirect == null) return new Integer(0);
599                     if (b.redirect != b) return get(b.redirect);
600                     return new Integer(b.pad(1));
601                 }
602                 public void put(Box b, Object value) {
603                     if (b.redirect == null) return;
604                     if (b.redirect != b) { put(b.redirect, value); return; }
605                     int newval = stoi(value);
606                     if (newval == b.pad(1)) return;
607                     b.set(pad, 1, newval);
608                 }
609             });
610
611         specialBoxProperties.put("indexof", new SpecialBoxProperty() {
612                 public Object get(Box b) { return b.indexof(); }
613             });
614
615         specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
616                 public Object get(Box b) { return new Integer(b.dmin(0)); }
617                 public void put(Box b, Object value) {
618                     if (b.sizetoimage) return;
619                     b.set(dmin, 0, stoi(value));
620                 }
621             });
622
623         specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
624                 public Object get(Box b) { return new Integer(b.dmax(0)); }
625                 public void put(Box b, Object value) {
626                     if (b.sizetoimage) return;
627                     b.set(dmax, 0, stoi(value));
628                 }
629             });
630
631         specialBoxProperties.put("minheight", new SpecialBoxProperty() {
632                 public Object get(Box b) { return new Integer(b.dmin(1)); }
633                 public void put(Box b, Object value) {
634                     if (b.sizetoimage) return;
635                     b.set(dmin, 1, stoi(value));
636                 }
637             });
638
639         specialBoxProperties.put("maxheight", new SpecialBoxProperty() {
640                 public Object get(Box b) { return new Integer(b.dmax(1)); }
641                 public void put(Box b, Object value) {
642                     if (b.sizetoimage) return;
643                     b.set(dmax, 1, stoi(value));
644                 }
645             });
646
647         specialBoxProperties.put("redirect", new SpecialBoxProperty() {
648                 public void put(Box b, Object value) { }
649                 public Object get(Box b) {
650                     if (b.redirect == null) return null;
651                     if (b.redirect == b) return Boolean.TRUE;
652                     return get(b.redirect);
653                 }
654             });
655
656         specialBoxProperties.put("apply", new SpecialBoxProperty() {
657                 public void put(Box b, Object value) { }
658                 public Object get(final Box b) { return new JS.Callable() {
659                         public Object call(JS.Array args) throws JS.Exn {
660                             if (args.elementAt(0) instanceof String) {
661                                 String templatename = (String)args.elementAt(0);
662                                 Template t = Template.getTemplate(templatename, null);
663                                 if (t == null) {
664                                     if (Log.on) Log.logJS(this, "template " + templatename + " not found");
665                                 } else {
666                                     if (ThreadMessage.suspendThread()) try {
667                                         JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1);
668                                         t.apply(b, null, null, callback, 0, t.numUnits());
669                                     } finally {
670                                         ThreadMessage.resumeThread();
671                                     }
672                                 }
673                                 
674                             } else if (args.elementAt(0) instanceof JS && !(args.elementAt(0) instanceof Box)) {
675                                 JS s = (JS)args.elementAt(0);
676                                 Object[] keys = s.keys();
677                                 for(int j=0; j<keys.length; j++) b.put(keys[j].toString(), s.get(keys[j]));
678                             }
679                             
680                             return b;
681                         } }; } });
682         
683         specialBoxProperties.put("id", new SpecialBoxProperty() {
684                 public void put(Box b, Object value) { }
685                 public Object get(Box b) { return b.id; }
686             });
687     }
688
689         
690     /** helper that converts a String to a boolean according to JavaScript coercion rules */
691     public static boolean stob(Object o) {
692         if (o == null) return false;
693         return Boolean.TRUE.equals(o) || "true".equals(o);
694     }
695
696     /** helper that converts a String to an int according to JavaScript coercion rules */
697     public static int stoi(Object o) {
698         if (o == null) return 0;
699         if (o instanceof Integer) return ((Integer)o).intValue();
700
701         String s;
702         if (!(o instanceof String)) s = o.toString();
703         else s = (String)o;
704
705         try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
706         catch (NumberFormatException e) { return 0; }
707     }
708         
709 }
710
711         
712