Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > cd38b09e3cb8d6c675b02d30393e68af > files > 43

kaya-doc-0.5.2-8.fc14.noarch.rpm

cgi shape; // -*-C-*-ish

import CGI;
import primitives;
import Image;

Void PreContent()
{
    content("<html><head><title>Shape Thingy</title></head><body>");
    content("<h1>Shape Thingy</h1>");
}

Void Default()
{
//    prim = Primitive(Rectangle(DrawColour(255,0,0),50,50),80,80);
    drawing = Drawing(480,320,DrawColour(0,0,0),[]);

    showDrawing(drawing);
    addShapeForm(drawing);
}

Void showDrawing(Drawing d) {
    content("<table><tr><td>");
    content(imageHandler(draw,d,"[Pretty picture]",d.x,d.y));
    content("</td><td>");

    idx = 0;
    for p in d.ps {
	case p.shape of {
	    Rectangle(col,rx,ry) -> shape = "Rectangle";
	  | Ellipse(col,ex,ey) -> shape = "Ellipse";
	  | Triangle(col,tb,th) -> shape = "Triangle";
	}
	content(shape+" at ("+p.x+","+p.y+") ");
	content(linkHandler(deleteShape,(d,idx),"Delete")+"<br>");
	idx++;
    }
    content("</td></table>");
}

Void deleteShape((Drawing,Int) didx) {
    d = didx.fst;
    idx = didx.snd;
    
    removeAt(d.ps,idx);
    showDrawing(d);
    addShapeForm(d);    
}

HTML colourBox(String name) 
   = selBox(name,3,["Red","Green","Yellow","Blue","White","Random"]);

Void addShapeForm(Drawing d) {
    content("<h3>Add a shape</h3>");

    content(formHandler(OnAddShape,d));
    content("<table><tr>"+
	    "<td>Rectangle</td>"+
	    "<td>X</td><td>"+textBox("rx","",5)+"</td>"+
	    "<td>Y</td><td>"+textBox("ry","",5)+"</td>"+
	    "<td>Width</td><td>"+textBox("rw","",5)+"</td>"+
	    "<td>Height</td><td>"+textBox("rh","",5)+"</td>"+
	    "<td>Colour</td><td>"+colourBox("rcol")+"</td>"+
	    "<td>"+submit("Add Rectangle")+"</td></tr>");
    content("<tr>"+
	    "<td>Ellipse</td>"+
	    "<td>X</td><td>"+textBox("ex","",5)+"</td>"+
	    "<td>Y</td><td>"+textBox("ey","",5)+"</td>"+
	    "<td>Width</td><td>"+textBox("ew","",5)+"</td>"+
	    "<td>Height</td><td>"+textBox("eh","",5)+"</td>"+
	    "<td>Colour</td><td>"+colourBox("ecol")+"</td>"+
	    "<td>"+submit("Add Ellipse")+"</td></tr>");
    content("<tr>"+
	    "<td>Triangle</td>"+
	    "<td>X</td><td>"+textBox("tx","",5)+"</td>"+
	    "<td>Y</td><td>"+textBox("ty","",5)+"</td>"+
	    "<td>Base</td><td>"+textBox("tb","",5)+"</td>"+
	    "<td>Height</td><td>"+textBox("th","",5)+"</td>"+
	    "<td>Colour</td><td>"+colourBox("tcol")+"</td>"+
	    "<td>"+submit("Add Triangle")+"</td></tr>");
    content("</table>");
    content(closeForm);
}

Void OnAddShape(Drawing d)
{
    try {
	if (submitUsed=="Add Rectangle") {
	    x = httpInt("rx");
	    y = httpInt("ry");
	    w = httpInt("rw");
	    h = httpInt("rh");
	    col = checkInput(incomingValue("rcol",DataPost), validCols, "");
	    c = mkColour(col);
	    shape = Primitive(Rectangle(c,w,h),x,y);
	}
	else if (submitUsed=="Add Ellipse") {
	    x = httpInt("ex");
	    y = httpInt("ey");
	    w = httpInt("ew");
	    h = httpInt("eh");
	    col = checkInput(incomingValue("ecol",DataPost), validCols, "");
	    c = mkColour(col);
	    shape = Primitive(Ellipse(c,w,h),x,y);
	}
	else if (submitUsed=="Add Triangle") {
	    x = httpInt("tx");
	    y = httpInt("ty");
	    b = httpInt("tb");
	    h = httpInt("th");
	    col = checkInput(incomingValue("tcol",DataPost), validCols, "");
	    c = mkColour(col);
	    shape = Primitive(Triangle(c,b,h),x,y);
	}
	push(d.ps,shape);
    }
    catch(e) {
	content("<p><strong>Invalid input!</strong></p>");
    }

    showDrawing(d);
    addShapeForm(d);
}

Void PostContent()
{
    content("<a href=\"../files/shape.k\">shape.k</a><br>");
    content("<a href=\"../files/primitives.k\">primitives.k</a><br>");
    content("Memory usage: "+gcHeapSize);
    content("</body></html>");
}