cleanups
[org.ibex.core.git] / src / org / ibex / Box.java
index d3f3450..be1c4f4 100644 (file)
@@ -8,6 +8,9 @@ package org.ibex;
 // FIXME: trap on numchildren?  replaces ChildChanged?
 // FIXME: trap on visible, trigger when parent visibility changes
 
+// FIXME: ax/ay nonsense
+// FIXME: mouse move/release still needs to propagate to boxen in which the mouse was pressed and is still held down
+
 // FEATURE: mark to reflow starting with a certain child
 // FEATURE: reintroduce surface.abort
 
@@ -51,20 +54,11 @@ public final class Box extends JSScope implements Scheduler.Task {
 
     protected Box() { super(null); }
 
-    static Hash boxToCursor = new Hash(500, 3);               // FIXME memory leak
+    // FIXME memory leak
+    static Hash boxToCursor = new Hash(500, 3);
+
     static final Font DEFAULT_FONT = Font.getFont((Stream)Main.builtin.get("fonts/vera/Vera.ttf"), 10);
 
-    // FIXME update these
-    // events can have write traps, but not read traps
-    static final String[] events = new String[] {
-        "Press1", "Press2", "Press3",
-        "Release1", "Release2", "Release3",
-        "Click1", "Click2", "Click3",
-        "DoubleClick1", "DoubleClick2", "DoubleClick3",
-        "Enter", "Leave", "Move", "ChildChange",
-        "KeyPressed", "KeyReleased", "SizeChange",
-        "Focused", "Maximized", "Minimized", "Close"
-    };
 
     // Flags //////////////////////////////////////////////////////////////////////
 
@@ -149,8 +143,8 @@ public final class Box extends JSScope implements Scheduler.Task {
     public void perform() throws JSExn {
         if (texture == null) { Log.warn(Box.class, "perform() called with null texture"); return; }
         if (texture.isLoaded) {
-            setMinWidth(max(texture.width, minwidth));
-            setMinHeight(max(texture.height, minheight));
+            setMinWidth(max(texture.width, maxwidth));
+            setMinHeight(max(texture.height, maxheight));
             DIRTY; }
         else { JS res = texture.stream; texture = null; throw new JSExn("image not found: "+res.unclone()); }
     }
@@ -525,70 +519,70 @@ public final class Box extends JSScope implements Scheduler.Task {
         }
     }
 
-    public void setMaxWidth(Object value) {
-        do { CHECKSET_INT(maxwidth); RECONSTRAIN(); } while(false);
-        if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth; 
-    }
-    public void setMaxHeight(Object value) {
-        do { CHECKSET_INT(maxheight); RECONSTRAIN(); } while(false);
-        if (parent == null && getSurface() != null) getSurface().pendingHeight = maxheight;
-    }
+    //#repeat setWidth/setHeight minwidth/minheight maxwidth/maxheight pendingWidth/pendingHeight
+    public void setWidth(int min, int max) {
+        // FIXME: deal with conflicting min/max
+        if (this.minwidth == min && this.maxwidth == max) return;
+        this.minwidth = min;
+        this.maxwidth = max;
+        RECONSTRAIN();
+        if (parent != null || getSurface() == null) return;
+        getSurface().pendingWidth = maxwidth;
 
-    private void setMinWidth(int m) { if (this.minwidth != m) { RECONSTRAIN(); this.minwidth = m; } }
-    private void setMinHeight(int m) { if (this.minheight != m) { RECONSTRAIN(); this.minheight = m; } }
+        // FIXME: the repeat doesn't work right here
+        getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
+    }
+    //#end
 
     public void put(Object name, Object value) throws JSExn {
         if (name instanceof Number) { put(toInt(name), value); return; }
         //#switch(name)
-        case "text": if (value == null) value = ""; CHECKSET_STRING(text); RECONSTRAIN(); DIRTY;
+        case "thisbox":     if (value == null) removeSelf();
+        case "text":        if (value == null) value = ""; CHECKSET_STRING(text); RECONSTRAIN(); DIRTY;
         case "strokecolor": value = N(stringToColor((String)value)); CHECKSET_INT(strokecolor); DIRTY;
-        case "textcolor": value = N(stringToColor((String)value)); CHECKSET_INT(strokecolor); DIRTY;
+        case "textcolor":   value = N(stringToColor((String)value)); CHECKSET_INT(strokecolor); DIRTY;
         case "strokewidth": CHECKSET_SHORT(strokewidth); DIRTY;
-        case "shrink": put("hshrink", value); put("vshrink", value);
-        case "hshrink": CHECKSET_FLAG(HSHRINK); RECONSTRAIN();
-        case "vshrink": CHECKSET_FLAG(VSHRINK); RECONSTRAIN();
-        case "width": put("maxwidth", value); put("minwidth", value);
-        case "height": put("maxheight", value); put("minheight", value);
-        case "maxwidth": setMaxWidth(value);
-        case "minwidth": CHECKSET_INT(minwidth); RECONSTRAIN();
-                         if (parent == null && getSurface() != null)
-                             getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
-        case "maxheight": setMaxHeight(value);
-        case "minheight": CHECKSET_INT(minheight); RECONSTRAIN();
-                         if (parent == null && getSurface() != null)
-                             getSurface().setMinimumSize(minwidth, minheight, minwidth != maxwidth || minheight != maxheight);
-        case "colspan": if (toInt(value) > 0) { CHECKSET_SHORT(colspan); if (parent != null) parent.REPACK(); }
-        case "rowspan": if (toInt(value) > 0) { CHECKSET_SHORT(rowspan); if (parent != null) parent.REPACK(); }
+        case "shrink":      CHECKSET_FLAG(HSHRINK | VSHRINK); RECONSTRAIN();
+        case "hshrink":     CHECKSET_FLAG(HSHRINK); RECONSTRAIN();
+        case "vshrink":     CHECKSET_FLAG(VSHRINK); RECONSTRAIN();
+        case "width":       setWidth(toInt(value), toInt(value));
+        case "height":      setHeight(toInt(value), toInt(value));
+        case "maxwidth":    setWidth(minwidth, toInt(value));
+        case "minwidth":    setWidth(toInt(value), maxwidth);
+        case "maxheight":   setHeight(minheight, toInt(value));
+        case "minheight":   setHeight(toInt(value), maxheight);
+        case "colspan":     if (toInt(value) > 0) { CHECKSET_SHORT(colspan); if (parent != null) parent.REPACK(); }
+        case "rowspan":     if (toInt(value) > 0) { CHECKSET_SHORT(rowspan); if (parent != null) parent.REPACK(); }
+        case "visible":     CHECKSET_FLAG(VISIBLE); RECONSTRAIN(); DIRTY;
+        case "packed":      CHECKSET_FLAG(PACKED); if (parent != null) { parent.REPACK(); } else { REPACK(); }
+        case "align":       clear(ALIGNS); setAlign(value == null ? "center" : value); REPLACE();
+        case "cursor":      setCursor(value);
+        case "fill":        setFill(value);
+        case "clip":        CHECKSET_FLAG(CLIP); if (parent == null) DIRTY; else parent.DIRTY;
         case "rows": CHECKSET_SHORT(rows); if (rows==0){set(FIXED, COLS);if(cols==0)cols=1;} else set(FIXED, ROWS); REPACK();
         case "cols": CHECKSET_SHORT(cols); if (cols==0){set(FIXED, ROWS);if(rows==0)rows=1;} else set(FIXED, COLS); REPACK();
-        case "clip": CHECKSET_FLAG(CLIP); if (parent == null) DIRTY; else parent.DIRTY;
-        case "visible": CHECKSET_FLAG(VISIBLE); RECONSTRAIN(); DIRTY;
-        case "packed": CHECKSET_FLAG(PACKED); if (parent != null) parent.REPACK();
-        case "align": clear(ALIGNS); setAlign(value == null ? "center" : value); REPLACE();
-        case "cursor": setCursor(value);
-        case "fill": setFill(value);
+
+        // FIXME: remove
         case "mouse":
             int mousex = toInt(((JS)value).get("x"));
             int mousey = toInt(((JS)value).get("y"));
             getSurface()._mousex = localToGlobalX(mousex);
             getSurface()._mousey = localToGlobalY(mousey);
+
         case "Minimized": if (parent == null && getSurface() != null) getSurface().minimized = toBoolean(value);  // FEATURE
         case "Maximized": if (parent == null && getSurface() != null) getSurface().maximized = toBoolean(value);  // FEATURE
-        case "Close": if (parent == null && getSurface() != null) getSurface().dispose(true);
+        case "Close":     if (parent == null && getSurface() != null) getSurface().dispose(true);
         case "redirect":
-            if (value == null) { redirect = null; return; }
-            for(Box cur = (Box)value; cur != null; cur = cur.parent)
-                if (cur == redirect) {
-                    redirect = (Box)value;
-                    return;
-                }
+            for(Box cur = (Box)value; cur != null || cur == redirect; cur = cur.parent)
+                if (cur == redirect) { redirect = (Box)value; return; }
             JS.error("redirect can only be set to a descendant of its current value");
+        case "fontsize": font = Font.getFont(font == null ? null : font.stream, toInt(value)); RECONSTRAIN(); DIRTY;
         case "font":
             if(!(value instanceof Stream)) throw new JSExn("You can only put streams to the font property");
+            if (font == value) return;  // FIXME: unclone()
             font = value == null ? null : Font.getFont((Stream)value, font == null ? 10 : font.pointsize);
             RECONSTRAIN();
             DIRTY;
-        case "fontsize": font = Font.getFont(font == null ? null : font.stream, toInt(value)); RECONSTRAIN(); DIRTY;
         case "x": if (parent==null && Surface.fromBox(this)!=null) {
             CHECKSET_INT(x);
         } else {
@@ -603,9 +597,8 @@ public final class Box extends JSScope implements Scheduler.Task {
             CHECKSET_INT(ay);
             REPLACE();
         }
-        case "titlebar":
-            if (getSurface() != null && value != null) getSurface().setTitleBarText(JS.toString(value));
-            super.put(name,value);
+        case "titlebar": if (getSurface()!=null) getSurface().setTitleBarText(toString(value)); super.put(name,value);
+        // FIXME: icon
 
         case "Press1":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "Press2":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
@@ -622,7 +615,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "KeyPressed":    if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "KeyReleased":   if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.putAndTriggerTraps(name, value);
         case "Move":          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)
@@ -651,8 +643,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "Enter":         return;
         case "Leave":         return;
 
-        case "thisbox":       if (value == null) removeSelf();
-
         default:              super.put(name, value);
         //#end
     }
@@ -673,8 +663,8 @@ public final class Box extends JSScope implements Scheduler.Task {
     }
 
     private void setAlign(Object value) {
+        clear(ALIGNS);
         //#switch(value)
-        case "center": clear(ALIGNS);
         case "topleft": set(ALIGN_TOP | ALIGN_LEFT);
         case "bottomleft": set(ALIGN_BOTTOM | ALIGN_LEFT);
         case "topright": set(ALIGN_TOP | ALIGN_RIGHT);
@@ -693,25 +683,27 @@ public final class Box extends JSScope implements Scheduler.Task {
         set(CURSOR);
         boxToCursor.put(this, value);
         Surface surface = getSurface();
-        if (surface != null) {
-            String tempcursor = surface.cursor;
-            propagateDownward(null, null, false);
-            if (surface.cursor != tempcursor) surface.syncCursor();
-        }
+        if (surface == null) return;
+        String tempcursor = surface.cursor;
+        propagateDownward(null, null, false);
+        if (surface.cursor != tempcursor) surface.syncCursor();
     }
 
     private void setFill(Object value) throws JSExn {
         if (value == null) {
-            // FIXME: Check this... does this make it transparent? 
+            if (texture == null && fillcolor == 0) return;
             texture = null;
             fillcolor = 0;
         } else if (value instanceof String) {
-            // FIXME check double set
             int newfillcolor = stringToColor((String)value);
             if (newfillcolor == fillcolor) return;
             fillcolor = newfillcolor;
-        } else if(value instanceof JS) {
-            texture = Picture.load((JS)value, this);
+            texture = null;
+        } else if (value instanceof JS) {
+            Picture newtex = Picture.load((JS)value, this);
+            if (texture == newtex) return;
+            texture = newtex;
+            fillcolor = 0;
             if (texture != null && texture.isLoaded) perform();
         } else {
             throw new JSExn("fill must be null, a String, or a stream, not a " + value.getClass());
@@ -719,7 +711,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         DIRTY;
     }
 
-    // FIXME: mouse move/release still needs to propagate to boxen in which the mouse was pressed and is still held down
     /**
      *  Handles events which propagate down the box tree.  If obscured
      *  is set, then we merely check for Enter/Leave.
@@ -965,45 +956,3 @@ public final class Box extends JSScope implements Scheduler.Task {
 
 
 
-        /*
-        offset_x = 0;
-        if (path != null) {
-            if (rpath == null) rpath = path.realize(transform == null ? VectorGraphics.Affine.identity() : transform);
-            if ((flags & HSHRINK) != 0) contentwidth = max(contentwidth, rpath.boundingBoxWidth());
-            if ((flags & VSHRINK) != 0) contentheight = max(contentheight, rpath.boundingBoxHeight());
-            // FIXME: separate offset_x needed for the path
-        }
-        // #repeat x1/y1 x2/y2 x3/y3 x4/y4 contentwidth/contentheight left/top right/bottom
-        int x1 = transform == null ? 0 : (int)transform.multiply_px(0, 0);
-        int x2 = transform == null ? 0 : (int)transform.multiply_px(contentwidth, 0);
-        int x3 = transform == null ? contentwidth : (int)transform.multiply_px(contentwidth, contentheight);
-        int x4 = transform == null ? contentwidth : (int)transform.multiply_px(0, contentheight);
-        int left = min(min(x1, x2), min(x3, x4));
-        int right = max(max(x1, x2), max(x3, x4));
-        contentwidth = max(contentwidth, right - left);
-        offset_x = -1 * left;
-        // #end
-        */
-
-
-                    /*
-        if (path != null) {
-            if (rtransform == null) rpath = null;
-            else if (!rtransform.equalsIgnoringTranslation(a)) rpath = null;
-            else {
-                rpath.translate((int)(a.e - rtransform.e), (int)(a.f - rtransform.f));
-                rtransform = a.copy();
-            }
-            if (rpath == null) rpath = path.realize((rtransform = a) == null ? VectorGraphics.Affine.identity() : a);
-            if ((strokecolor & 0xff000000) != 0) rpath.stroke(buf, 1, strokecolor);
-            if ((fillcolor & 0xff000000) != 0) rpath.fill(buf, new VectorGraphics.SingleColorPaint(fillcolor));
-        }
-*/
-
-
-/*
-            VectorGraphics.Affine a2 = VectorGraphics.Affine.translate(b.x, b.y);
-            if (transform != null) a2.multiply(transform);
-            a2.multiply(VectorGraphics.Affine.translate(offset_x, offset_y));
-            a2.multiply(a);
-*/