2003/12/13 00:38:34
[org.ibex.core.git] / src / org / xwt / Surface.java
index 3a083cc..0977ada 100644 (file)
@@ -2,6 +2,7 @@
 package org.xwt;
 
 import org.bouncycastle.util.encoders.Base64;
+import org.xwt.js.*;
 import org.xwt.util.*;
 import java.io.*;
 import java.util.*;
@@ -19,9 +20,6 @@ import java.util.*;
  */
 public abstract class Surface extends PixelBuffer {
 
-    public int getWidth() { return root == null ? 0 : root.width; }
-    public int getHeight() { return root == null ? 0 : root.height; }
-        
     // Static Data ////////////////////////////////////////////////////////////////////////////////
 
     /**< the most recently enqueued Move message; used to throttle the message rate */
@@ -76,22 +74,31 @@ public abstract class Surface extends PixelBuffer {
     public abstract void setTitleBarText(String s);      ///< Sets the surface's title bar text, if applicable
     public abstract void setIcon(Picture i);      ///< Sets the surface's title bar text, if applicable
     public abstract void _dispose();      ///< Destroy the surface
+
+
+
+    // Sizing /////////////////////////////////////////////////////////////////////////////////
+
+    private int width = 0;
+    private int height = 0;
+    public int getWidth() { return width; }
+    public int getHeight() { return height; }
+
     public void setLimits(int min_width, int min_height, int max_width, int max_height) { }
-    protected abstract void _setSize(int width, int height);  ///< Sets the surface's width and height.
 
+    /// should be overriden by subclasses; programmatically sets the height
+    protected abstract void _setSize(int width, int height); 
 
-    private int platform_window_width = 0;
-    private int platform_window_height = 0;
+    public final void setWidth(int width) { setSize(width, this.height); }
+    public final void setHeight(int height) { setSize(this.width, height); }
     protected final void setSize(int width, int height) {
-        if (root.width != width || root.height != height) {
-            root.dirty(0, root.height - Main.scarImage.getHeight(), Main.scarImage.getWidth(), Main.scarImage.getHeight());
-            root.width = Math.max(Main.scarImage.getWidth(), width);
-            root.height = Math.max(Main.scarImage.getHeight(), height);
-        }
-        if (root.width > 0 && root.height > 0 && platform_window_width != root.width && platform_window_height != root.height)
-            _setSize(root.width, root.height);
+        if (this.width == width && this.height == height) return;
+        this.width = Math.max(Main.scarImage.getWidth(), width);
+        this.height = Math.max(Main.scarImage.getHeight(), height);
+        _setSize(width, height);
     }
 
+
     // Helper methods for subclasses ////////////////////////////////////////////////////////////
 
     protected final void Press(final int button) {
@@ -108,7 +115,7 @@ public abstract class Surface extends PixelBuffer {
             final Box who = Box.whoIs(root, mousex, mousey);
             Scheduler.add(new Scheduler.Task() { public void perform() {
                 Platform.clipboardReadEnabled = true;
-                root.putAndTriggerJSTraps("Press3", Boolean.TRUE);
+                root.putAndTriggerTraps("Press3", Boolean.TRUE);
                 Platform.clipboardReadEnabled = false;
             }});
         }
@@ -175,7 +182,7 @@ public abstract class Surface extends PixelBuffer {
                 Box b = (Box)keywatchers.elementAt(i);
                 for(Box cur = b; cur != null; cur = cur.parent)
                     if (!cur.test(cur.VISIBLE)) continue outer;
-                b.putAndTriggerJSTraps("KeyPressed", key);
+                b.putAndTriggerTraps("KeyPressed", key);
             }
             Platform.clipboardReadEnabled = false;
         }
@@ -195,7 +202,7 @@ public abstract class Surface extends PixelBuffer {
                 Box b = (Box)keywatchers.elementAt(i);
                 for(Box cur = b; cur != null; cur = cur.parent)
                     if (!cur.test(cur.VISIBLE)) continue outer;
-                b.putAndTriggerJSTraps("KeyReleased", key);
+                b.putAndTriggerTraps("KeyReleased", key);
             }
         }});
     }
@@ -223,7 +230,7 @@ public abstract class Surface extends PixelBuffer {
 
                 // Root gets motion events outside itself (if trapped, of course)
                 if (!root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
-                    root.putAndTriggerJSTraps("Move", Boolean.TRUE);
+                    root.putAndTriggerTraps("Move", Boolean.TRUE);
 
                 root.Move(oldmousex, oldmousey, mousex, mousey);
                 if (!cursor.equals(oldcursor)) syncCursor();
@@ -233,11 +240,9 @@ public abstract class Surface extends PixelBuffer {
 
     protected final void SizeChange(final int width, final int height) {
         Scheduler.add(new Scheduler.Task() { public void perform() {
-            if (width == root.width && height == root.height) return;
             root.set(root.REFLOW);
-            platform_window_width = width;
-            platform_window_height = height;
-            do { abort = false; root.reflow(width, height); } while(abort);
+            Surface.this.width = width;
+            Surface.this.height = height;
         }});
         abort = true;
     }
@@ -246,7 +251,7 @@ public abstract class Surface extends PixelBuffer {
         Scheduler.add(new Scheduler.Task() { public void perform() {
             root.x = x;
             root.y = y;
-            root.putAndTriggerJSTraps("PosChange", Boolean.TRUE);
+            root.putAndTriggerTraps("PosChange", Boolean.TRUE);
         }});
     }
 
@@ -298,12 +303,11 @@ public abstract class Surface extends PixelBuffer {
         this.root = root;
         Surface old = fromBox(root);
         if (old != null) old.dispose(false);
-        else root.remove();
-
-        // make sure the root is properly sized
-        do { abort = false; root.reflow(root.width, root.height); } while(abort);
-
-        root.dirty();
+        else try {
+            root.put("thisbox", null);
+        } catch (JSExn e) {
+            throw new Error("this should never happen");
+        }
         Refresh();
     }
 
@@ -312,11 +316,15 @@ public abstract class Surface extends PixelBuffer {
     /** runs the prerender() and render() pipelines in the root Box to regenerate the backbuffer, then blits it to the screen */
     public synchronized void render() {
 
+        // FIXME ugly
+        if (root.width != width || root.height != height)
+            root.dirty(0, root.height - Main.scarImage.getHeight(), Main.scarImage.getWidth(), Main.scarImage.getHeight());
+
         // make sure the root is properly sized
         do {
             abort = false;
-            root.reflow(root.width, root.height);
-            setSize(root.width, root.height);
+            root.reflow(width, height);
+            setSize(width, height);
             // update mouseinside and trigger Enter/Leave as a result of box size/position changes
             String oldcursor = cursor;
             cursor = "default";
@@ -371,7 +379,7 @@ public abstract class Surface extends PixelBuffer {
             Scheduler.add(this);
         }
         
-        public void perform() { boxContainingMouse.putAndTriggerJSTraps(name, value); }
+        public void perform() { boxContainingMouse.putAndTriggerTraps(name, value); }
         public String toString() { return "SimpleMessage [name=" + name + ", value=" + value + "]"; }
 
     }