2003/10/31 09:50:08
[org.ibex.core.git] / src / org / xwt / Box.java.pp
index 429c7a4..c58eebc 100644 (file)
@@ -84,15 +84,19 @@ public final class Box extends JS.Scope {
 
     // Flags ///////////////////////////////////////////////////////////////////////////////
     short flags = 0;
-    static int MOUSEINSIDE_FLAG  = 0x00000001;
-    static int INVISIBLE_FLAG    = 0x00000002;
-    static int NOTPACKED_FLAG    = 0x00000004;
-    static int HSHRINK_FLAG      = 0x00000008;
-    static int VSHRINK_FLAG      = 0x00000010;
-    static int TILE_FLAG         = 0x00000020;
-    static int FONT_CHANGED_FLAG = 0x00000040;  // set when font changes, cleared during repack
-    static int ISROOT_FLAG       = 0x00000080;
-    static int NOCLIP_FLAG       = 0x00000100;
+    static final int MOUSEINSIDE_FLAG  = 0x00000001;
+    static final int INVISIBLE_FLAG    = 0x00000002;
+    static final int NOTPACKED_FLAG    = 0x00000004;
+    static final int HSHRINK_FLAG      = 0x00000008;
+    static final int VSHRINK_FLAG      = 0x00000010;
+    static final int FONT_CHANGED_FLAG = 0x00000040;  // set when font changes, cleared during repack
+    static final int ISROOT_FLAG       = 0x00000080;
+    static final int NOCLIP_FLAG       = 0x00000100;
+    static final int ALIGN_TOP_FLAG    = 0x00001000;
+    static final int ALIGN_BOTTOM_FLAG = 0x00002000;
+    static final int ALIGN_LEFT_FLAG   = 0x00004000;
+    static final int ALIGN_RIGHT_FLAG  = 0x00008000;
+    static final int ALIGN_FLAGS       = 0x0000f000;
 
 
     // Geometry ////////////////////////////////////////////////////////////////////////////
@@ -209,10 +213,16 @@ public final class Box extends JS.Scope {
 
     // Reflow ////////////////////////////////////////////////////////////////////////////////////////
 
+    /** only for use on the root box */
     void reflow() { reflow(width, height); }
     void reflow(int new_width, int new_height) {
         repack();
         if (Surface.abort) return;
+        //#repeat width/height new_width/new_height HSHRINK_FLAG/VSHRINK_FLAG contentwidth/contentheight minwidth/minheight maxwidth/maxheight
+        new_width = bound(max(contentwidth, minwidth),
+                          new_width,
+                          ((flags & HSHRINK_FLAG) != 0) ? max(contentwidth, minwidth) : maxwidth);
+        //#end
         resize(x, y, new_width, new_height);
     }
 
@@ -236,7 +246,7 @@ public final class Box extends JS.Scope {
         // --- Phase 1 ----------------------------------------------------------------------
         // assign children to their row/column positions (assuming constrained columns)
         if ((rows == 0 && cols == 0) || (rows != 0 && cols != 0)) throw new Error("rows == " + rows + "   cols == " + cols);
-        //#repeat x/y y/x width/height col/row row/col cols/rows rows/cols colspan/rowspan rowspan/colspan colWidth/rowHeight numRowsInCol/numColsInRow INNER/INNER2 maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight OUTER/OUTER2 INNER/INNER2
+        //#repeat x/y y/x width/height col/row row/col cols/rows rows/cols colspan/rowspan rowspan/colspan colWidth/rowHeight numRowsInCol/numColsInRow INNER/INNER2 maxwidth/maxheight minwidth/minheight colMaxWidth/rowMaxHeight OUTER/OUTER2 INNER/INNER2
         if (rows == 0 && numChildren() != 0) {
             int[] numRowsInCol = new int[cols];           // the number of cells occupied in each column
             Box child = getChild(0);
@@ -263,8 +273,8 @@ public final class Box extends JS.Scope {
 
         // --- Phase 2 ----------------------------------------------------------------------
         // compute the min/max sizes of the columns and rows and set our contentwidth
+        //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight numCols/numRows textwidth/textheight
         if (numChildren() != 0) {
-            //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight numCols/numRows
             int numCols = cols;
             if (numCols == 0)
                 for(Box child = getChild(0); child != null; child = child.nextSibling())
@@ -274,10 +284,10 @@ public final class Box extends JS.Scope {
                 if (!(((child.flags & NOTPACKED_FLAG) != 0) || ((child.flags & INVISIBLE_FLAG) != 0)))
                     colWidth[child.col] = max(colWidth[child.col], child.contentwidth / child.colspan);
             for(int col=0; col<numCols; col++) contentwidth += colWidth[col];
-            contentwidth = max(textwidth, contentwidth);
-            contentwidth = bound(minwidth, contentwidth, maxwidth);
-            //#end               
         }
+        contentwidth = max(textwidth, contentwidth);
+        contentwidth = bound(minwidth, contentwidth, maxwidth);
+        //#end               
 
         offset_x = 0;
         if ((flags & NOTPACKED_FLAG) == 0) {
@@ -397,10 +407,13 @@ public final class Box extends JS.Scope {
                 child_height = ((child.flags & VSHRINK_FLAG) != 0) ? child.contentheight : min(child.maxheight, height - child.y);
             } else {
                 int diff;
-                //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight HSHRINK_FLAG/VSHRINK_FLAG marginWidth/marginHeight child_x/child_y child_width/child_height
+                //#repeat x/y y/x width/height col/row cols/rows colspan/rowspan colWidth/rowHeight maxwidth/maxheight minwidth/minheight contentwidth/contentheight colMaxWidth/rowMaxHeight HSHRINK_FLAG/VSHRINK_FLAG marginWidth/marginHeight child_x/child_y child_width/child_height ALIGN_LEFT_FLAG/ALIGN_TOP_FLAG ALIGN_RIGHT_FLAG/ALIGN_BOTTOM_FLAG
                 child_width = 0; for(int i=child.col; i<child.col+child.colspan && i<colWidth.length; i++) child_width += colWidth[i];
                 diff = bound(child.contentwidth, child_width, ((child.flags & HSHRINK_FLAG) != 0) ? child.contentwidth : child.maxwidth) - child_width;
-                if (transform == null) child_x = marginWidth / 2;
+                if (transform == null) {
+                    if ((flags & ALIGN_RIGHT_FLAG) != 0) child_x = marginWidth;
+                    else if ((flags & ALIGN_LEFT_FLAG) == 0) child_x = marginWidth / 2;
+                }
                 for(int i=0; i<child.col; i++) child_x += colWidth[i];
                 if (diff < 0) child_x += -1 * (diff / 2);
                 child_width += diff;
@@ -422,6 +435,7 @@ public final class Box extends JS.Scope {
         int globaly = parenty + (parent == null ? 0 : y);
 
         // intersect the x,y,w,h rendering window with ourselves; quit if it's empty
+
         if ((flags & NOCLIP_FLAG) == 0) {
             clipw = min(max(clipx, parent == null ? 0 : globalx) + clipw, (parent == null ? 0 : globalx) + width) - globalx;
             cliph = min(max(clipy, parent == null ? 0 : globaly) + cliph, (parent == null ? 0 : globaly) + height) - globaly;
@@ -436,11 +450,19 @@ public final class Box extends JS.Scope {
         }
 
         if (image != null)
-            if ((flags & TILE_FLAG) != 0) renderTiledImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
-            else renderStretchedImage(globalx, globaly, clipx, clipy, clipw, cliph, buf);
+            for(int x = globalx; x < clipx + clipw; x += image.getWidth())
+                for(int y = globaly; y < clipy + cliph; y += image.getHeight())
+                    buf.drawPicture(image, x, y, clipx, clipy, clipx + clipw, clipy + cliph);
 
        if (text != null && !text.equals(""))
-            renderText(globalx, globaly, clipx, clipy, clipw, cliph, buf);
+            Glyph.rasterizeGlyphs(font, fontsize, text, buf, textcolor, globalx, globaly, clipx, clipy, clipw + clipx, clipy + cliph,
+                                  new Callback() { public Object call(Object arg) {
+                                      Box.this.dirty();
+                                      Box b = Box.this;
+                                      MARK_FOR_REFLOW_b;
+                                      b.dirty();
+                                      return null;
+                                  }});
 
         if (path != null) {
             if (rtransform == null) rpath = null;
@@ -466,50 +488,6 @@ public final class Box extends JS.Scope {
         }
     }
 
-    void renderStretchedImage(int globalx, int globaly, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
-        // FIXME: wrong
-        buf.drawPicture(image, clipx, clipy, clipx + clipw, clipy + cliph, 0, 0, image.getWidth(), image.getHeight());
-    }
-
-    void renderTiledImage(int globalx, int globaly, int x, int y, int w, int h, PixelBuffer buf) {
-        int iw = image.getWidth();
-        int ih = image.getHeight();
-        for(int i=(x - x)/iw; i <= (x + w - x)/iw; i++) {
-            for(int j=(y - y)/ih; j<= (y + h - y)/ih; j++) {
-                
-                int dx1 = max(i * iw + x, x);
-                int dy1 = max(j * ih + y, y);
-                int dx2 = min((i+1) * iw + x, x + w);
-                int dy2 = min((j+1) * ih + y, y + h);
-                
-                int sx1 = dx1 - (i*iw) - x;
-                int sy1 = dy1 - (j*ih) - y;
-                int sx2 = dx2 - (i*iw) - x;
-                int sy2 = dy2 - (j*ih) - y;
-
-                if (dx2 - dx1 > 0 && dy2 - dy1 > 0 && sx2 - sx1 > 0 && sy2 - sy1 > 0)
-                    buf.drawPicture(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
-            }
-        }
-    }
-
-    void renderText(int x, int y, int clipx, int clipy, int clipw, int cliph, PixelBuffer buf) {
-        for(int i=0; i<text.length(); i++) {
-            // FIXME: clipping
-            char c = text.charAt(i);
-            Glyph g = Glyph.getGlyph(font, fontsize, c);
-           buf.drawPictureAlphaOnly(g.p,
-                                     x,
-                                     y + g.max_ascent - g.baseline,
-                                     x + g.p.getWidth(),
-                                     y + g.max_ascent - g.baseline + g.p.getHeight(),
-                                     0, 0,
-                                     g.p.getWidth(), g.p.getHeight(),
-                                     textcolor);
-            x += g.advance;
-        }
-    }
-
 
     // Methods to implement org.xwt.js.JS //////////////////////////////////////
 
@@ -828,8 +806,25 @@ public final class Box extends JS.Scope {
         return parent.getRoot();
     }
 
+    public void recompute_font() {
+        if (text == null) { textwidth = textheight = 0; return; }
+        if (font == null) { /* FIXME */ }
+        long widthheight = Glyph.rasterizeGlyphs(font, fontsize, text, null, textcolor, 0, 0, 0, 0, 0, 0,
+                                                 new Callback() { public Object call(Object arg) {
+                                                     Box b = Box.this;
+                                                     recompute_font();
+                                                     MARK_FOR_REFLOW_b;
+                                                     b.dirty();
+                                                     return null;
+                                                 } });
+        if (widthheight == -1) return;
+        textwidth = (int)((widthheight & 0xffff0000) >> 16);
+        textheight = (int)(widthheight & 0x0000ffff);
+        MARK_FOR_REFLOW_this;
+    }
 
-    // Trivial Helper Methods (should be inlined) /////////////////////////////////////////
+
+    // Trivial Helper Method s(should be inlined) /////////////////////////////////////////
 
     static final int min(int a, int b) { if (a<b) return a; else return b; }
     static final float min(float a, float b) { if (a<b) return a; else return b; }
@@ -900,6 +895,18 @@ public final class Box extends JS.Scope {
 
         static {
             specialBoxProperties.put("fill", new ColorBoxProperty() {
+                    public void put(final Box b, final Object value) {
+                        if (value == null || !(value instanceof Res)) super.put(b, value);
+                        else Picture.fromRes((Res)value, new Callback() { public Object call(Object pic) {
+                            if (pic == b.image) return null;
+                            b.image = (Picture)pic;
+                            b.minwidth = b.image.getWidth();
+                            b.minheight = b.image.getHeight();
+                            MARK_FOR_REFLOW_b;
+                            b.dirty();
+                            return null;
+                        } });
+                    }
                     public int getColor(Box b) { return b.fillcolor; }
                     public void putColor(Box b, int argb) { b.fillcolor = argb; }
                 });
@@ -918,23 +925,8 @@ public final class Box extends JS.Scope {
                         String t = value == null ? "null" : value.toString();
                         if (t.equals(b.text)) return;
                        b.text = t;
-                       if (t == null) {
-                            if (b.textwidth != 0 || b.textheight != 0) MARK_FOR_REFLOW_b;
-                           b.textwidth = b.textheight = 0;
-                       } else {
-                           try {
-                                MARK_FOR_REFLOW_b;
-                                b.textwidth = 0;
-                                for(int i=0; i<b.text.length(); i++) {
-                                    Glyph g = Glyph.getGlyph(b.font, b.fontsize, b.text.charAt(i));
-                                    b.textwidth += g.advance;
-                                    b.textheight = g.max_ascent + g.max_descent;
-                                }
-                           } catch (Exception e) {
-                               Log.log(this, e);
-                           }
-                       }
                        b.dirty();
+                        b.recompute_font();
                     } });
 
             specialBoxProperties.put("path", new SpecialBoxProperty() {
@@ -1023,6 +1015,7 @@ public final class Box extends JS.Scope {
                         MARK_FOR_REFLOW_b;
                         b.flags |= FONT_CHANGED_FLAG;
                         b.dirty();
+                        b.recompute_font();
                     } });
         
             specialBoxProperties.put("fontsize", new SpecialBoxProperty() {
@@ -1033,6 +1026,7 @@ public final class Box extends JS.Scope {
                         MARK_FOR_REFLOW_b;
                         b.flags |= FONT_CHANGED_FLAG;
                         b.dirty();
+                        b.recompute_font();
                     } });
         
             specialBoxProperties.put("strokewidth", new SpecialBoxProperty() {
@@ -1043,11 +1037,41 @@ public final class Box extends JS.Scope {
                         b.dirty();
                     } });
         
+            specialBoxProperties.put("align", new SpecialBoxProperty() {
+                    public Object get(Box b) {
+                        switch(b.flags & ALIGN_FLAGS) {
+                        case (ALIGN_TOP_FLAG | ALIGN_LEFT_FLAG): return "topleft";
+                        case (ALIGN_BOTTOM_FLAG | ALIGN_LEFT_FLAG): return "bottomleft";
+                        case (ALIGN_TOP_FLAG | ALIGN_RIGHT_FLAG): return "topright";
+                        case (ALIGN_BOTTOM_FLAG | ALIGN_RIGHT_FLAG): return "bottomright";
+                        case ALIGN_TOP_FLAG: return "top";
+                        case ALIGN_BOTTOM_FLAG: return "bottom";
+                        case ALIGN_LEFT_FLAG: return "left";
+                        case ALIGN_RIGHT_FLAG: return "right";
+                        case 0: return "center";
+                        default: throw new Error("invalid alignment flags: " + (b.flags & ALIGN_FLAGS));
+                        }
+                    }
+                    public void put(Box b, Object value) {
+                        b.flags &= ~ALIGN_FLAGS;
+                        if (value == null || value.equals("center")) { b.flags &= ALIGN_FLAGS; }
+                        else if (value.equals("topleft")) { b.flags |= ALIGN_TOP_FLAG | ALIGN_LEFT_FLAG; }
+                        else if (value.equals("bottomleft")) { b.flags |= ALIGN_BOTTOM_FLAG | ALIGN_LEFT_FLAG; }
+                        else if (value.equals("topright")) { b.flags |= ALIGN_TOP_FLAG | ALIGN_RIGHT_FLAG; }
+                        else if (value.equals("bottomright")) { b.flags |= ALIGN_BOTTOM_FLAG | ALIGN_RIGHT_FLAG; }
+                        else if (value.equals("top")) { b.flags |= ALIGN_TOP_FLAG; }
+                        else if (value.equals("bottom")) { b.flags |= ALIGN_BOTTOM_FLAG; }
+                        else if (value.equals("left")) { b.flags |= ALIGN_LEFT_FLAG; }
+                        else if (value.equals("right")) { b.flags |= ALIGN_RIGHT_FLAG; }
+                        else Log.logJS("invalid alignment specifier \"" + value + "\"");
+                        MARK_FOR_REFLOW_b;
+                        b.dirty();
+                    } });
+        
             specialBoxProperties.put("thisbox", new SpecialBoxProperty() {
                     public Object get(Box b) { return b; }
                     public void put(Box b, Object value) {
                         if (value == null) b.remove();
-                        else if (value.equals("window") || value.equals("frame")) Platform.createSurface(b, value.equals("frame"), true);
                         else if (Log.on) Log.log(this, "put invalid value to 'thisbox' property: " + value);
                     }
                 });
@@ -1102,7 +1126,7 @@ public final class Box extends JS.Scope {
                         return new Integer(b.x);
                     }
                     public void put(Box b, Object value) {
-                        if (!((b.flags & NOTPACKED_FLAG) != 0)) return;
+                        if (!((b.flags & NOTPACKED_FLAG) != 0) && (b.parent != null || b.surface == null)) return;
                         int x = stoi(value);
                         if (x == b.x) return;
                         b.dirty();
@@ -1114,17 +1138,20 @@ public final class Box extends JS.Scope {
                 });
             //#end
         
-            //#repeat width/height minwidth/minheight maxwidth/maxheight
+            //#repeat width/height height/width minwidth/minheight maxwidth/maxheight true/false
             specialBoxProperties.put("width", new SpecialBoxProperty() {
                     public Object get(Box b) { return new Integer(b.width); }
                     public void put(Box b, Object value) {
-                        b.width = stoi(value);
+                        int newval = stoi(value);
                         if (b.parent == null && b.surface != null) {
-                            b.surface.setSize();
+                            // hack to circumvent #repeat
+                            int other = b.height;
+                            if (true) b.surface.setSize(newval, other);
+                            else b.surface.setSize(other, newval);
                             MARK_FOR_REFLOW_b;
                         } else {
-                            if (b.minwidth == b.width && b.maxwidth == b.width) return;
-                            b.minwidth = b.maxwidth = b.width;
+                            if (b.minwidth == newval && b.maxwidth == newval) return;
+                            b.minwidth = b.maxwidth = newval;
                             MARK_FOR_REFLOW_b;
                         }
                     } });
@@ -1153,14 +1180,6 @@ public final class Box extends JS.Scope {
                 });
             //#end
         
-            specialBoxProperties.put("tile", new SpecialBoxProperty() {
-                    public Object get(Box b) { return ((b.flags & TILE_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
-                    public void put(Box b, Object value) {
-                        if (((b.flags & TILE_FLAG) != 0) == stob(value)) return;
-                        if (stob(value)) b.flags |= TILE_FLAG; else b.flags &= ~TILE_FLAG;
-                        b.dirty();
-                    } });
-        
             specialBoxProperties.put("noclip", new SpecialBoxProperty() {
                     public Object get(Box b) { return ((b.flags & NOCLIP_FLAG) != 0) ? Boolean.TRUE : Boolean.FALSE; }
                     public void put(Box b, Object value) {
@@ -1197,26 +1216,6 @@ public final class Box extends JS.Scope {
                         if (b.parent != null) MARK_FOR_REFLOW_b_parent;
                     } });
         
-            specialBoxProperties.put("image", new SpecialBoxProperty() {
-                    /* FIXME
-                    public Object get(Box b) { return b.image == null ? null : ImageDecoder.imageToNameMap.get(b.image); }
-                    */
-                    public void put(Box b, Object value) {
-                        //if (image != null) System.out.println("hit");
-                        if (value == null) {
-                            b.image = null;
-                        } else if (value instanceof Res) {
-                            b.image = Picture.fromRes((Res)value);
-                        } else {
-                            // FIXME
-                        }
-                        b.minwidth = min(b.maxwidth, max(b.minwidth, b.image == null ? 0 : b.image.getWidth()));
-                        b.minheight = min(b.maxheight, max(b.minheight, b.image == null ? 0 : b.image.getHeight()));
-                        MARK_FOR_REFLOW_b;
-                        b.dirty();
-                    }
-                });
-
             //#repeat globalx/globaly x/y
             specialBoxProperties.put("globalx", new SpecialBoxProperty() {
                     public Object get(Box b) { return new Integer(b.parent == null || b.surface == null ? 0 : b.x); }