2002/03/21 01:19:33
[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 org.mozilla.javascript.*;
5 import org.xwt.util.*;
6 import java.util.*;
7 import java.net.*;
8 import java.text.*;
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                         newcolor = 0xFF000000 |
85                             (Integer.parseInt(s.substring(1, 3), 16) << 16) |
86                             (Integer.parseInt(s.substring(3, 5), 16) << 8) |
87                             Integer.parseInt(s.substring(5, 7), 16);
88                     else if (s.equals("black")) newcolor = black;
89                     else if (s.equals("blue")) newcolor = blue;
90                     else if (s.equals("green")) newcolor = green;
91                     else if (s.equals("cyan")) newcolor = cyan;
92                     else if (s.equals("red")) newcolor = red;
93                     else if (s.equals("magenta")) newcolor = magenta;
94                     else if (s.equals("brown")) newcolor = brown;
95                     else if (s.equals("lightGray")) newcolor = lightGray;
96                     else if (s.equals("darkGray")) newcolor = darkGray;
97                     else if (s.equals("brightBlue")) newcolor = brightBlue;
98                     else if (s.equals("brightGreen")) newcolor = brightGreen;
99                     else if (s.equals("brightCyan")) newcolor = brightCyan;
100                     else if (s.equals("brightRed")) newcolor = brightRed;
101                     else if (s.equals("pink")) newcolor = pink;
102                     else if (s.equals("yellow")) newcolor = yellow;
103                     else if (s.equals("white")) newcolor = white;
104                     else if (Log.on) Log.log(this, "invalid color " + s);
105
106                     if (newcolor == b.color) return;
107                     b.color = newcolor;
108                     b.dirty();
109                 } });
110         
111         
112         specialBoxProperties.put("textcolor", new SpecialBoxProperty() {
113                 public Object get(Box b) {
114                     if ((b.textcolor & 0xFF000000) == 0) return null;
115                     String red = Integer.toHexString((b.textcolor & 0x00FF0000) >> 16);
116                     String green = Integer.toHexString((b.textcolor & 0x0000FF00) >> 8);
117                     String blue = Integer.toHexString(b.textcolor & 0x000000FF);
118                     if (red.length() < 2) red = "0" + red;
119                     if (blue.length() < 2) blue = "0" + blue;
120                     if (green.length() < 2) green = "0" + green;
121                     return "#" + red + green + blue;
122                 }
123                 public void put(Box b, Object value) {
124                     int newtextcolor = b.color;
125                     String s = value == null ? null : value.toString();
126                     if (s.length() > 0 && s.charAt(0) == '#')
127                         newtextcolor = 0xFF000000 |
128                             (Integer.parseInt(s.substring(1, 3), 16) << 16) |
129                             (Integer.parseInt(s.substring(3, 5), 16) << 8) |
130                             Integer.parseInt(s.substring(5, 7), 16);
131                     else if (s.equals("black")) newtextcolor = black;
132                     else if (s.equals("blue")) newtextcolor = blue;
133                     else if (s.equals("green")) newtextcolor = green;
134                     else if (s.equals("cyan")) newtextcolor = cyan;
135                     else if (s.equals("red")) newtextcolor = red;
136                     else if (s.equals("magenta")) newtextcolor = magenta;
137                     else if (s.equals("brown")) newtextcolor = brown;
138                     else if (s.equals("lightGray")) newtextcolor = lightGray;
139                     else if (s.equals("darkGray")) newtextcolor = darkGray;
140                     else if (s.equals("brightBlue")) newtextcolor = brightBlue;
141                     else if (s.equals("brightGreen")) newtextcolor = brightGreen;
142                     else if (s.equals("brightCyan")) newtextcolor = brightCyan;
143                     else if (s.equals("brightRed")) newtextcolor = brightRed;
144                     else if (s.equals("pink")) newtextcolor = pink;
145                     else if (s.equals("yellow")) newtextcolor = yellow;
146                     else if (s.equals("white")) newtextcolor = white;
147                     else if (Log.on) Log.log(this, "invalid textcolor " + s);
148
149                     if (newtextcolor == b.textcolor) return;
150                     b.textcolor = newtextcolor;
151                     b.dirty();
152                 } });
153         
154         specialBoxProperties.put("text", new SpecialBoxProperty() {
155                 public Object get(Box b) { return b.text; }
156                 public void put(Box b, Object value) {
157                     String t = value == null ? "null" : value.toString();
158                     if (t.equals(b.text)) return;
159
160                     for(int i=0; i<t.length(); i++)
161                         if (Character.isISOControl(t.charAt(i))) {
162                             if (Log.on) Log.log(this, 
163                                                 "ISO Control characters are not permitted in box text strings; offending character is ASCII " +
164                                                ((int)t.charAt(i)));
165                             /* FIXME: reinstate
166                             return;
167                             */
168                         }
169
170                     b.text = t;
171                     b.textupdate();
172                     b.dirty();
173                 } });
174         
175         specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
176                 public Object get(Box b) { return b; }
177                 public void put(Box b, Object value) {
178                     if (value == null) b.remove();
179                     else if (value.equals("window")) Platform.createSurface(b, false, true);
180                     else if (value.equals("frame")) Platform.createSurface(b, true, true);
181                     else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
182                 }
183             });
184
185         specialBoxProperties.put("orient", new SpecialBoxProperty() {
186                 public Object get(Box b) {
187                     if (b.redirect == null) return "horizontal";
188                     else if (b.redirect != b) return get(b.redirect);
189                     else if (b.o == 1) return "vertical";
190                     else return "horizontal";
191                 }
192                 public void put(Box b, Object value) {
193                     if (value == null) return;
194                     if (b.redirect == null) return;
195                     if (b.redirect != b) {
196                         put(b.redirect, value);
197                         return;
198                     }
199                     if (value.equals("vertical")) {
200                         if (b.o == 1) return;
201                         b.o = 1;
202                         b.xo = 0;
203                     } else if (value.equals("horizontal")) {
204                         if (b.o == 0) return;
205                         b.o = 0;
206                         b.xo = 1;
207                     } else if (Log.on)
208                         Log.log(this, "invalid value put to orient property: " + value);
209                     b.mark_for_prerender();
210                     b.sync_cmin_to_children();
211                 } });
212
213         specialBoxProperties.put("static", new SpecialBoxProperty() {
214                 public Object get(Box b) {
215                     String cfsn = JSObject.getCurrentFunctionSourceName();
216                     for(int i=0; i<cfsn.length() - 1; i++)
217                         if (cfsn.charAt(i) == '.' && (cfsn.charAt(i+1) == '_' || Character.isDigit(cfsn.charAt(i+1)))) {
218                             cfsn = cfsn.substring(0, i);
219                             break;
220                         }
221                     return Static.getStatic(cfsn);
222                 }
223                 public void put(Box b, Object value) { }
224             });
225
226         specialBoxProperties.put("sizetoimage", new SpecialBoxProperty() {
227                 public Object get(Box b) { return b.sizetoimage ? Boolean.TRUE : Boolean.FALSE; }
228                 public void put(Box b, Object value) {
229                     if (stob(value)) {
230                         if (b.sizetoimage) return;
231                         b.sizetoimage = true;
232                         b.syncSizeToImage();
233                     } else {
234                         b.sizetoimage = false;
235                     }
236                 } });
237         
238         specialBoxProperties.put("shrink", new SpecialBoxProperty() {
239                 public Object get(Box b) { return (b.vshrink && b.hshrink) ? Boolean.TRUE : Boolean.FALSE; }
240                 public void put(Box b, Object value) {
241                     boolean newshrink = stob(value);
242                     if (b.hshrink == newshrink && b.vshrink == newshrink) return;
243                     b.hshrink = b.vshrink = newshrink;
244                     if (b.hshrink) b.set(b.dmax, 0, Box.max(b.cmin(0), b.textdim(0) + 2 * b.pad(0), b.dmin(0)));
245                     if (b.vshrink) b.set(b.dmax, 1, Box.max(b.cmin(1), b.textdim(1) + 2 * b.pad(1), b.dmin(1)));
246                 } });
247         
248         specialBoxProperties.put("hshrink", new SpecialBoxProperty() {
249                 public Object get(Box b) { return new Boolean(b.hshrink); }
250                 public void put(Box b, Object value) {
251                     boolean newshrink = stob(value);
252                     if (b.hshrink == newshrink) return;
253                     b.hshrink = newshrink;
254                     if (b.hshrink) b.set(b.dmax, 0, Box.max(b.cmin(0), b.textdim(0) + 2 * b.pad(0), b.dmin(0)));
255                 }
256             });
257         
258         specialBoxProperties.put("vshrink", new SpecialBoxProperty() {
259                 public Object get(Box b) { return b.vshrink ? Boolean.TRUE : Boolean.FALSE; }
260                 public void put(Box b, Object value) {
261                     boolean newshrink = stob(value);
262                     if (b.vshrink == newshrink) return;
263                     b.vshrink = newshrink;
264                     if (b.vshrink) b.set(b.dmax, 1, Box.max(b.cmin(1), b.textdim(1) + 2 * b.pad(1), b.dmin(1)));
265                 }
266             });
267         
268         specialBoxProperties.put("x", new SpecialBoxProperty() {
269                 public Object get(Box b) {
270                     if (b.surface == null) return new Integer(0);
271                     if (b.invisible) return new Integer(0);
272                     return new Integer(b.abs(0));
273                 }
274                 public void put(Box b, Object value) {
275                     if (b.getParent() == null && b.surface != null) {
276                         b.surface.setLocation(stoi(value), b.abs(1));
277                         b.surface.centerSurfaceOnRender = false;
278                     }
279                     b.set(abs, 0, stosh(value));
280                 }
281             });
282         
283         specialBoxProperties.put("y", 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(1));
288                 }
289                 public void put(Box b, Object value) {
290                     if (b.getParent() == null && b.surface != null) {
291                         b.surface.setLocation(b.abs(0), stoi(value));
292                         b.surface.centerSurfaceOnRender = false;
293                     }
294                     b.set(abs, 1, stosh(value));
295                 }
296             });
297
298         specialBoxProperties.put("width", new SpecialBoxProperty() {
299                 public Object get(Box b) { return new Integer(b.size(0)); }
300                 public void put(Box b, Object value) {
301                     if (b.sizetoimage) return;
302                     if (b.getParent() == null && b.surface != null) {
303                         b.set(size, 0, Box.max(Surface.scarPicture.getWidth(), stosh(value)));
304                         b.mark_for_prerender();
305                     } else {
306                         b.set(dmax, 0, stosh(value));
307                         b.set(dmin, 0, stosh(value));
308                     }
309                 } });
310         
311         specialBoxProperties.put("height", new SpecialBoxProperty() {
312                 public Object get(Box b) { return new Integer(b.size(1)); }
313                 public void put(Box b, Object value) {
314                     if (b.sizetoimage) return;
315                     if (b.getParent() == null && b.surface != null) {
316                         b.set(size, 1, Box.max(Surface.scarPicture.getHeight(), stosh(value)));
317                         b.mark_for_prerender();
318                     } else {
319                         b.set(dmax, 1, stosh(value));
320                         b.set(dmin, 1, stosh(value));
321                     }
322                 } });
323
324         specialBoxProperties.put("flex", new SpecialBoxProperty() {
325                 public Object get(Box b) { return new Double(b.flex); }
326                 public void put(Box b, Object value) {
327                     if (value == null) return;
328                     int newflex = stoi(value);
329                     if (newflex == b.flex) return;
330                     b.flex = newflex;
331                     b.mark_for_prerender();
332                 } });
333         
334         specialBoxProperties.put("tile", new SpecialBoxProperty() {
335                 public Object get(Box b) { return b.tile ? Boolean.TRUE : Boolean.FALSE; }
336                 public void put(Box b, Object value) {
337                     boolean newtile = stob(value);
338                     if (newtile == b.tile) return;
339                     b.tile = newtile;
340                     b.dirty();
341                 } });
342         
343         specialBoxProperties.put("align", new SpecialBoxProperty() {
344                 public Object get(Box b) {
345                     if (b.align == -1) return "topleft";
346                     else if (b.align == 1) return "bottomright";
347                     else return "center";
348                 }
349                 public void put(Box b, Object value) {
350                     String s = value == null ? "" : value.toString();
351                     byte newalign = b.align;
352                     if (s.equals("topleft")) newalign = -1;
353                     else if (s.equals("bottomright")) newalign = 1;
354                     else if (s.equals("center")) newalign = 0;
355                     else if (Log.on) Log.log(this, "invalid value put to align property: " + value);
356                     if (newalign == b.align) return;
357                     b.align = newalign;
358                     if (b.getParent() != null) b.getParent().mark_for_prerender();
359                 } });
360         
361         specialBoxProperties.put("invisible", new SpecialBoxProperty() {
362                 public Object get(Box b) { return b.invisible ? Boolean.TRUE : Boolean.FALSE; }
363                 public void put(Box b, Object value) {
364                     boolean newinvisible = stob(value);
365                     if (newinvisible == b.invisible) return;
366                     b.invisible = newinvisible;
367                     if (b.getParent() == null) {
368                         if (b.surface != null) b.surface.setInvisible(newinvisible);
369                     } else {
370                         b.dirty();
371                         b.getParent().mark_for_prerender();
372                         b.getParent().sync_cmin_to_children();
373                         b.getParent().dirty(b.pos(0), b.pos(1), b.size(0), b.size(1));
374                         b.getParent().dirty(b.oldpos(0), b.oldpos(1), b.oldsize(0), b.oldsize(1));
375                     }
376                 }});
377         
378         specialBoxProperties.put("absolute", new SpecialBoxProperty() {
379                 public Object get(Box b) { return b.absolute ? Boolean.TRUE : Boolean.FALSE; }
380                 public void put(Box b, Object value) {
381                     boolean newabsolute = stob(value);
382                     if (newabsolute == b.absolute) return;
383                     b.absolute = newabsolute;
384                     if (b.getParent() != null) {
385                         b.getParent().mark_for_prerender();
386                         b.getParent().sync_cmin_to_children();
387                     }
388                 } });
389         
390         specialBoxProperties.put("image", new SpecialBoxProperty() {
391                 public Object get(Box b) { return b.image == null ? null : Box.imageToNameMap.get(b.image); }
392                 public void put(Box b, Object value) { b.setImage(value == null ? null : value.toString()); }
393             });
394
395         specialBoxProperties.put("border", new SpecialBoxProperty() {
396                 public Object get(Box b) { return b.border == null ? null : Box.imageToNameMap.get(b.border); }
397                 public void put(Box b, Object value) { b.setBorder(value == null ? null : value.toString()); }
398             });
399
400         specialBoxProperties.put("font", new SpecialBoxProperty() {
401                 public Object get(Box b) { return b.font; }
402                 public void put(Box b, Object value) {
403                     if (value == null) value = Platform.getDefaultFont();
404                     b.font = value.toString();
405                     b.textupdate();
406                     b.dirty();
407                 } });
408         
409         specialBoxProperties.put("globalx", new SpecialBoxProperty() {
410                 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(0)); }
411                 public void put(Box b, Object value) {
412                     if (b.surface == null || b.getParent() == null) return;
413                     b.put("x", null, new Integer(stoi(value) - stoi(get(b.getParent()))));
414                 }
415             });
416         
417         specialBoxProperties.put("globaly", new SpecialBoxProperty() {
418                 public Object get(Box b) { return new Integer(b.getParent() == null || b.surface == null ? 0 : b.pos(1)); }
419                 public void put(Box b, Object value) {
420                     if (b.surface == null || b.getParent() == null) return;
421                     b.put("y", null, new Integer(stoi(value) - stoi(get(b.getParent()))));
422                 }
423             });
424         
425         specialBoxProperties.put("cursor", new SpecialBoxProperty() {
426                 public Object get(Box b) { return b.cursor; } 
427                 public void put(Box b, Object value) {
428                     b.cursor = (String)value;
429                     if (b.surface == null) return;
430
431                     // see if we need to update the surface cursor
432                     String tempcursor = b.surface.cursor;
433                     b.Move(b.surface.mousex, b.surface.mousey, b.surface.mousex, b.surface.mousey);
434                     if (b.surface.cursor != tempcursor) b.surface.syncCursor();
435                 } 
436             });
437         
438         specialBoxProperties.put("mousex", new SpecialBoxProperty() {
439                 public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousex - b.pos(0)); }
440             });
441         
442         specialBoxProperties.put("mousey", new SpecialBoxProperty() {
443                 public Object get(Box b) { return new Integer(b.surface == null ? 0 : b.surface.mousey - b.pos(1)); }
444             });
445         
446         specialBoxProperties.put("xwt", new SpecialBoxProperty() {
447                 public Object get(Box b) { return XWT.singleton; }
448             });
449         
450         specialBoxProperties.put("mouseinside", new SpecialBoxProperty() {
451                 public Object get(Box b) { return b.mouseinside ? Boolean.TRUE : Boolean.FALSE; }
452             });
453         
454         specialBoxProperties.put("numchildren", new SpecialBoxProperty() {
455                 public Object get(Box b) {
456                     if (b.redirect == null) return new Integer(0);
457                     if (b.redirect != b) return get(b.redirect);
458                     return new Integer(b.numChildren());
459                 } });
460         
461         SpecialBoxProperty mouseEventHandler = new SpecialBoxProperty() {
462                 public void put(String name, Box b, Object value) {
463                     if (b.surface == null) return;
464                     if (b.getParent() == null) {
465                         if (b.surface.boxContainingMouse.getParent() != null)
466                             b.surface.boxContainingMouse.put(name, b.surface.boxContainingMouse, value);
467                     } else {
468                         // check siblings
469                         for(Box c = b.prevSibling(); c != null; c = c.prevSibling())
470                             if (c.inside(c.surface.mousex, c.surface.mousey)) {
471                                 c.put(name, c, value);
472                                 return;
473                             }
474                         // move up a level
475                         if (b.getParent() != null && b.getParent().getParent() != null)
476                             b.getParent().put(name, b.getParent(), value);
477                     }
478                 }};
479
480         specialBoxProperties.put("Press1", mouseEventHandler);
481         specialBoxProperties.put("Press2", mouseEventHandler);
482         specialBoxProperties.put("Press3", mouseEventHandler);
483         specialBoxProperties.put("Release1", mouseEventHandler);
484         specialBoxProperties.put("Release2", mouseEventHandler);
485         specialBoxProperties.put("Release3", mouseEventHandler);
486         specialBoxProperties.put("Click1", mouseEventHandler);
487         specialBoxProperties.put("Click2", mouseEventHandler);
488         specialBoxProperties.put("Click3", mouseEventHandler);
489         specialBoxProperties.put("DoubleClick1", mouseEventHandler);
490         specialBoxProperties.put("DoubleClick2", mouseEventHandler);
491         specialBoxProperties.put("DoubleClick3", mouseEventHandler);
492
493         specialBoxProperties.put("root", new SpecialBoxProperty() {
494                 public Object get(Box b) {
495                     if (b.surface == null) return null;
496                     else if (b.getRoot() == null) return null;
497                     else return b.getRoot().getRootProxy();
498                 } });
499
500         specialBoxProperties.put("Minimized", new SpecialBoxProperty() {
501                 public Object get(Box b) {
502                     if (b.getParent() == null && b.surface != null) return b.surface.minimized ? Boolean.TRUE : Boolean.FALSE;
503                     else return null;
504                 }
505                 public void put(Box b, Object value) {
506                     if (b.surface == null) return;
507                     boolean val = stob(value);
508                     if (b.getParent() == null && b.surface.minimized != val) b.surface.setMinimized(val);
509                 }
510             });
511
512         specialBoxProperties.put("Maximized", new SpecialBoxProperty() {
513                 public Object get(Box b) {
514                     if (b.getParent() == null && b.surface != null) return b.surface.maximized ? Boolean.TRUE : Boolean.FALSE;
515                     else return null;
516                 }
517                 public void put(Box b, Object value) {
518                     if (b.surface == null) return;
519                     boolean val = stob(value);
520                     if (b.getParent() == null && b.surface.maximized != val) b.surface.setMaximized(val);
521                 }
522             });
523
524         specialBoxProperties.put("toback", new SpecialBoxProperty() {
525                 public void put(Box b, Object value) {
526                     if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toBack();
527                 }
528             });
529
530         specialBoxProperties.put("tofront", new SpecialBoxProperty() {
531                 public void put(Box b, Object value) {
532                     if (b.getParent() == null && stob(value) && b.surface != null) b.surface.toFront();
533                 }
534             });
535
536         specialBoxProperties.put("hscar", new SpecialBoxProperty() {
537                 public void put(Box b, Object value) {
538                     if (b.getParent() == null && b.surface != null) {
539                         b.surface.hscar = stoi(value);
540                         b.surface.dirty(0, 0, b.surface.width, b.surface.height);
541                         b.surface.Refresh();
542                     }
543                 }
544             });
545
546         specialBoxProperties.put("vscar", new SpecialBoxProperty() {
547                 public void put(Box b, Object value) {
548                     if (b.getParent() == null && b.surface != null) {
549                         b.surface.vscar = stoi(value);
550                         b.surface.dirty(0, 0, b.surface.width, b.surface.height);
551                         b.surface.Refresh();
552                     }
553                 }
554             });
555
556         specialBoxProperties.put("Close", new SpecialBoxProperty() {
557                 public void put(Box b, Object value) {
558                     if (b.getParent() == null && b.surface != null) b.surface.dispose();
559                 }
560             });
561
562         // these are all do-nothings; just to prevent space from getting taken up in the params Hash.
563         specialBoxProperties.put("KeyPressed", new SpecialBoxProperty());
564         specialBoxProperties.put("KeyReleased", new SpecialBoxProperty());
565         specialBoxProperties.put("PosChange", new SpecialBoxProperty());
566         specialBoxProperties.put("SizeChange", new SpecialBoxProperty());
567
568         specialBoxProperties.put("hpad", new SpecialBoxProperty() {
569                 public Object get(Box b) {
570                     if (b.redirect == null) return new Integer(0);
571                     if (b.redirect != b) return get(b.redirect);
572                     return new Integer(b.pad(0));
573                 }
574                 public void put(Box b, Object value) {
575                     if (b.redirect == null) return;
576                     if (b.redirect != b) { put(b.redirect, value); return; }
577                     short newval = stosh(value);
578                     if (newval == b.pad(0)) return;
579                     b.set(pad, 0, newval);
580                 }
581             });
582
583         specialBoxProperties.put("vpad", new SpecialBoxProperty() {
584                 public Object get(Box b) {
585                     if (b.redirect == null) return new Integer(0);
586                     if (b.redirect != b) return get(b.redirect);
587                     return new Integer(b.pad(1));
588                 }
589                 public void put(Box b, Object value) {
590                     if (b.redirect == null) return;
591                     if (b.redirect != b) { put(b.redirect, value); return; }
592                     short newval = stosh(value);
593                     if (newval == b.pad(1)) return;
594                     b.set(pad, 1, newval);
595                 }
596             });
597
598         specialBoxProperties.put("indexof", new SpecialBoxProperty() {
599                 public Object get(Box b) { return b.indexof(); }
600             });
601
602         specialBoxProperties.put("minwidth", new SpecialBoxProperty() {
603                 public Object get(Box b) { return new Integer(b.dmin(0)); }
604                 public void put(Box b, Object value) {
605                     if (b.sizetoimage) return;
606                     b.set(dmin, 0, stosh(value));
607                 }
608             });
609
610         specialBoxProperties.put("maxwidth", new SpecialBoxProperty() {
611                 public Object get(Box b) { return new Integer(b.dmax(0)); }
612                 public void put(Box b, Object value) {
613                     if (b.sizetoimage) return;
614                     b.set(dmax, 0, stosh(value));
615                 }
616             });
617
618         specialBoxProperties.put("minheight", new SpecialBoxProperty() {
619                 public Object get(Box b) { return new Integer(b.dmin(1)); }
620                 public void put(Box b, Object value) {
621                     if (b.sizetoimage) return;
622                     b.set(dmin, 1, stosh(value));
623                 }
624             });
625
626         specialBoxProperties.put("maxheight", new SpecialBoxProperty() {
627                 public Object get(Box b) { return new Integer(b.dmax(1)); }
628                 public void put(Box b, Object value) {
629                     if (b.sizetoimage) return;
630                     b.set(dmax, 1, stosh(value));
631                 }
632             });
633     }
634
635         
636     /** helper that converts a String to a boolean according to JavaScript coercion rules */
637     public static boolean stob(Object o) {
638         if (o == null) return false;
639         return Boolean.TRUE.equals(o) || "true".equals(o);
640     }
641
642     /** helper that converts a String to an int according to JavaScript coercion rules */
643     public static int stoi(Object o) {
644         if (o == null) return 0;
645         if (o instanceof Integer) return ((Integer)o).intValue();
646
647         String s;
648         if (!(o instanceof String)) s = o.toString();
649         else s = (String)o;
650
651         try { return Integer.parseInt(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
652         catch (NumberFormatException e) { return 0; }
653     }
654         
655     /** helper that converts a String to a short according to JavaScript coercion rules */
656     public static short stosh(Object o) {
657         if (o == null) return 0;
658         if (o instanceof Number) return ((Number)o).shortValue();
659
660         String s;
661         if (!(o instanceof String)) s = o.toString();
662         else s = (String)o;
663
664         try { return Short.parseShort(s.indexOf('.') == -1 ? s : s.substring(0, s.indexOf('.'))); }
665         catch (NumberFormatException e) { return 0; }
666     }
667         
668 }
669
670         
671