fixed bug 440, reintroduced splash screen
[org.ibex.core.git] / src / org / ibex / Box.java
index 1301639..4e8d778 100644 (file)
@@ -14,7 +14,7 @@ package org.ibex;
 import java.util.*;
 import org.ibex.js.*;
 import org.ibex.util.*;
-import org.xwt.translators.*;
+import org.ibex.translators.*;
 
 /**
  *  <p>
@@ -152,6 +152,8 @@ public final class Box extends JSScope implements Scheduler.Task {
     private short col = 0;
     public LENGTH x = 0;
     public LENGTH y = 0;
+    public LENGTH ax = 0;   // FEATURE: roll these into x/y; requires lots of changes
+    public LENGTH ay = 0;   // FEATURE: roll these into x/y; requires lots of changes; perhaps y()?
     public LENGTH width = 0;
     public LENGTH height = 0;
     private LENGTH contentwidth = 0;      // == max(minwidth, textwidth, sum(child.contentwidth))
@@ -166,6 +168,7 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     // Instance Methods /////////////////////////////////////////////////////////////////////
 
+    public final int fontSize() { return font == null ? DEFAULT_FONT.pointsize : font.pointsize; }
 
     /** invoked when a resource needed to render ourselves finishes loading */
     public void perform() throws JSExn {
@@ -319,12 +322,14 @@ public final class Box extends JSScope implements Scheduler.Task {
             if (!child.test(VISIBLE)) continue;
             int child_width, child_height, child_x, child_y;
             if (!child.test(PACKED)) {
-                child_x = child.x;
-                child_y = child.y;
-                child_width = child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - child_x);
-                child_height = child.test(VSHRINK) ? child.contentheight : min(child.maxheight, height - child_y);
+                child_width = child.test(HSHRINK) ? child.contentwidth : min(child.maxwidth, width - Math.abs(child.ax));
+                child_height = child.test(VSHRINK) ? child.contentheight : min(child.maxheight, height - Math.abs(child.ay));
                 child_width = max(child.minwidth, child_width);
                 child_height = max(child.minheight, child_height);
+                int gap_x = width - child_width;
+                int gap_y = height - child_height;
+                child_x = child.ax + (child.test(ALIGN_RIGHT) ? gap_x : !child.test(ALIGN_LEFT) ? gap_x / 2 : 0);
+                child_y = child.ay + (child.test(ALIGN_BOTTOM) ? gap_y : !child.test(ALIGN_TOP) ? gap_y / 2 : 0);
             } else {
                 int unbounded;
                 //#repeat col/row colspan/rowspan contentwidth/contentheight width/height colMaxWidth/rowMaxHeight \
@@ -378,10 +383,15 @@ public final class Box extends JSScope implements Scheduler.Task {
             for(int x = globalx; x < cx2; x += texture.width)
                 for(int y = globaly; y < cy2; y += texture.height)
                     buf.drawPicture(texture, x, y, cx1, cy1, cx2, cy2);
-
-        if (text != null && !text.equals("") && font != null)
-            if (font.rasterizeGlyphs(text, buf, strokecolor, globalx, globaly, cx1, cy1, cx2, cy2, null) == -1)
-                font.rasterizeGlyphs(text, buf, strokecolor, globalx, globaly, cx1, cy1, cx2, cy2, this);
+        if (text != null && !text.equals("") && font != null) {
+            int gap_x = width - font.textwidth(text);
+            int gap_y = height - font.textheight(text);
+            int text_x = globalx + (test(ALIGN_RIGHT) ? gap_x : !test(ALIGN_LEFT) ? gap_x/2 : 0);
+            int text_y = globaly + (test(ALIGN_BOTTOM) ? gap_y : !test(ALIGN_TOP) ? gap_y/2 : 0);
+            if (font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, null) == -1)
+                font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, this);
+        }
 
         for(Box b = getChild(0); b != null; b = b.nextSibling())
             b.render(globalx, globaly, cx1, cy1, cx2, cy2, buf, null);
@@ -542,8 +552,8 @@ public final class Box extends JSScope implements Scheduler.Task {
             MARK_RESIZE;
             dirty();
         case "fontsize": font = Font.getFont(font == null ? null : font.stream, toInt(value)); MARK_RESIZE; dirty();
-        case "x": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(x); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(x); dirty(); MARK_RESIZE; dirty(); }
-        case "y": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(y); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(y); dirty(); MARK_RESIZE; dirty(); }
+        case "x": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(x); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(ax); dirty(); MARK_RESIZE; dirty(); }
+        case "y": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(y); } else { if (test(PACKED) && parent != null) return; dirty(); CHECKSET_INT(ay); dirty(); MARK_RESIZE; dirty(); }
         case "titlebar":
             if (getSurface() != null && value != null) getSurface().setTitleBarText(JS.toString(value));
             super.put(name,value);
@@ -566,6 +576,11 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "Enter":         if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "Leave":         if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
 
+        case "HScroll":       if (!test(STOP_UPWARD_PROPAGATION) && parent != null)
+            parent.putAndTriggerTraps(name, N(((Number)value).floatValue() * ((float)parent.fontSize()) / ((float)fontSize())));
+        case "VScroll":       if (!test(STOP_UPWARD_PROPAGATION) && parent != null)
+            parent.putAndTriggerTraps(name, N(((Number)value).floatValue() * ((float)parent.fontSize()) / ((float)fontSize())));
+
         case "_Move":         propagateDownward(name, value, false);
         case "_Press1":       propagateDownward(name, value, false);
         case "_Press2":       propagateDownward(name, value, false);
@@ -581,6 +596,8 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "_DoubleClick3": propagateDownward(name, value, false);
         case "_KeyPressed":   propagateDownward(name, value, false);
         case "_KeyReleased":  propagateDownward(name, value, false);
+        case "_HScroll":      propagateDownward(name, value, false);
+        case "_VScroll":      propagateDownward(name, value, false);
 
         case "PosChange":     return;
         case "SizeChange":    return;
@@ -629,10 +646,11 @@ public final class Box extends JSScope implements Scheduler.Task {
         set(CURSOR);
         boxToCursor.put(this, value);
         Surface surface = getSurface();
-        String tempcursor = surface.cursor;
-        // FIXME
-        //Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
-        if (surface.cursor != tempcursor) surface.syncCursor();
+        if (surface != null) {
+            String tempcursor = surface.cursor;
+            propagateDownward(null, null, false);
+            if (surface.cursor != tempcursor) surface.syncCursor();
+        }
     }
 
     private void setFill(Object value) throws JSExn {
@@ -666,29 +684,42 @@ public final class Box extends JSScope implements Scheduler.Task {
         int y = globalToLocalY(getSurface()._mousey);
         boolean wasinside = test(MOUSEINSIDE);
         boolean isinside = test(VISIBLE) && inside(x, y) && !obscured;
-        if (!wasinside && isinside) { set(MOUSEINSIDE);   putAndTriggerTrapsAndCatchExceptions("Enter", T); }
-        if (wasinside && !isinside) { clear(MOUSEINSIDE); putAndTriggerTrapsAndCatchExceptions("Leave", T); }
+        if (!wasinside && isinside) {
+            set(MOUSEINSIDE);
+            putAndTriggerTrapsAndCatchExceptions("Enter", T);
+        }
+        if (isinside && test(CURSOR)) getSurface().cursor = (String)boxToCursor.get(this);
+        if (wasinside && !isinside) {
+            clear(MOUSEINSIDE);
+            putAndTriggerTrapsAndCatchExceptions("Leave", T);
+        }
 
         boolean found = false;
         if (wasinside || isinside)
             for(Box child = getChild(treeSize() - 1); child != null; child = child.prevSibling()) {
                 boolean save_stop = child.test(STOP_UPWARD_PROPAGATION);
+                Object value2 = value;
+                if (name.equals("_HScroll") || name.equals("_VScroll"))
+                    value2 = N(((Number)value).floatValue() * ((float)child.fontSize()) / (float)fontSize());
                 if (obscured || !child.inside(x - child.x, y - child.y)) {
-                    child.propagateDownward(name, value, true);
+                    child.propagateDownward(name, value2, true);
                 } else try {
                     found = true;
                     child.clear(STOP_UPWARD_PROPAGATION);
-                    child.putAndTriggerTrapsAndCatchExceptions(name, value);
+                    if (name != null) child.putAndTriggerTrapsAndCatchExceptions(name, value2);
+                    else child.propagateDownward(name, value2, obscured);
                 } finally {
                     if (save_stop) child.set(STOP_UPWARD_PROPAGATION); else child.clear(STOP_UPWARD_PROPAGATION);
                 }
                 if (child.inside(x - child.x, y - child.y))
-                    if (name.equals("_Move")) obscured = true;
+                    if (name != null && name.equals("_Move")) obscured = true;
                     else break;
             }
 
         if (!obscured && !found)
-            if (!name.equals("_Move") || wasinside) putAndTriggerTrapsAndCatchExceptions(name.substring(1), value);
+            if ("_Move".equals(name) || wasinside)
+                if (name != null)
+                    putAndTriggerTrapsAndCatchExceptions(name.substring(1), value);
     }
 
     private static int stringToColor(String s) {