import java.awt.*;
import java.net.*;

public class rectangle extends Panel implements tool
{
 asciiJam aj;
 int ox;
 int oy;
 int plantx;
 int planty;
 int zoom;
 int fill = 2;
 boolean defCurs = false;
 charChooser chCh = null;
 Label lbl = null;

    public void show(){
		setFont(env.dialogFont);
		super.show();
	}
    public rectangle(asciiJam ajam){ 
		this.aj = ajam;
        this.resize(123,290);
        this.setLayout(null);
        this.setBackground(Color.black);
        Panel prefs = new Panel();
        prefs.setLayout(new GridLayout(5,1,0,0));
        prefs.setBackground(Color.lightGray);
        prefs.add(new Label(" Rect Prefs"));
        CheckboxGroup group1 = new CheckboxGroup();
		Checkbox tmp = null;
        prefs.add(new Checkbox("Fill",group1,false));
        prefs.add(new Checkbox("Outline",group1,false));
        prefs.add(new Checkbox("Thin Line",group1,true));
        prefs.add(lbl=new Label(" Fill letter:"));
        chCh = new charChooser(aj);
        chCh.reshape(0,0,123,170);
        Panel inner = new Panel();
        inner.resize(size().width-2,size().height-2);
        inner.move(1,1);
        inner.setLayout(new BorderLayout());
        inner.add(prefs,BorderLayout.NORTH);
        inner.add(chCh,BorderLayout.CENTER);
        add(inner);
        lbl.hide();
	}

    public boolean mouseDrag(int x, int y)
    {
	    int offX = aj.mapF.getX();
        int offY = aj.mapF.getY();
        x-= offX;
        y-=offY;

        Graphics g = aj.ta.getGraphics();
        if(g!=null){
            int z2 = aj.ta.zoom<<2;
            int z3 = aj.ta.zoom<<3;
            g.setColor(Color.black);
            g.setXORMode(Color.white);
			if(y<planty){ //correct for another stupid VM bug (OSX)
			    if(oy!=planty) g.drawLine(ox+z2,oy+z3,ox+z2,planty+z3);
			    if(oy!=planty) g.drawLine(plantx+z2,oy+z3,plantx+z2,planty+z3);
			}else{
			    if(oy!=planty) g.drawLine(ox+z2,planty+z3,ox+z2,oy+z3);
			    if(oy!=planty) g.drawLine(plantx+z2,planty+z3,plantx+z2,oy+z3);
			}

			if(x<plantx){ //correct for another stupid VM bug (OSX)
			    if(ox!=plantx) g.drawLine(ox+z2,oy+z3,plantx+z2,oy+z3);
			    if(ox!=plantx) g.drawLine(ox+z2,planty+z3,plantx+z2,planty+z3);
			}else{
			    if(ox!=plantx) g.drawLine(plantx+z2,oy+z3,ox+z2,oy+z3);
			    if(ox!=plantx) g.drawLine(plantx+z2,planty+z3,ox+z2,planty+z3);
			}

			if(y<planty){ //correct for another stupid VM bug (OSX)
			   if(y!=planty) g.drawLine(x+z2,y+z3,x+z2,planty+z3);
			   if(y!=planty) g.drawLine(plantx+z2,y+z3,plantx+z2,planty+z3);
			}else{
			   if(y!=planty) g.drawLine(x+z2,planty+z3,x+z2,y+z3);
			   if(y!=planty) g.drawLine(plantx+z2,planty+z3,plantx+z2,y+z3);
			}
			if(x<plantx){ //correct for another stupid VM bug (OSX)
			   if(x!=plantx) g.drawLine(x+z2,y+z3,plantx+z2,y+z3);
			   if(x!=plantx) g.drawLine(x+z2,planty+z3,plantx+z2,planty+z3);
			}else{
			   if(x!=plantx) g.drawLine(plantx+z2,y+z3,x+z2,y+z3);
			   if(x!=plantx) g.drawLine(plantx+z2,planty+z3,x+z2,planty+z3);
			}
            g.dispose();
        }
        ox = x;
        oy = y;
        return true;
    }
/*  faster computers perhaps...
	public boolean mouseDrag(int x, int y){ 
		boolean draw=false;
		int z3 = zoom<<3;
		int z4 = zoom<<4;
		int ulx = ox;
		int uly = oy;
		ox = x;
		oy = y;
		if(plantx>x){ ulx = x; x=plantx; }
		else { ulx = plantx; }
		if(planty>y){ uly = y; y=planty; }
		else { uly = planty; }
        Graphics g = aj.ta.getGraphics();
        if(g != null){
		    aj.ta.paint(g);
            g.setFont(env.font[zoom]);
			String s = (new Character((char)fillChar)).toString();
            for(int xpos = ulx; xpos <= x; xpos+=z3){
            	for(int ypos = uly; ypos <= y; ypos+=z4){
					if(fill==1 || xpos == ulx || xpos == x || ypos == uly || ypos == y){
                	   g.setColor(Color.black);
                	   g.fillRect( xpos+ 1, ypos + 1, z3 - 1, z4 - 2);
                	   g.setColor(aj.curCol);
                	   g.drawString(s, xpos +zoom, ypos + 11*zoom);
                	   g.setColor(Color.darkGray);
                	   g.drawRect(xpos, ypos, z3, z4);
					}
				}
            }
        }
        g.dispose();
		return true; 
	}
*/
    public boolean mouseDown(int x, int y, int z, asciiJam ajam){
        int offX = aj.mapF.getX();
        int offY = aj.mapF.getY();
        ox = plantx = x -offX;
        oy = planty = y -offY;
        zoom = z;
        aj = ajam;
		System.arraycopy(aj.ta.text,0,ajam.tmptxt,0,ajam.tmptxt.length);
		return true;
    }

	public boolean mouseUp(int x, int y){ 
        int offX = aj.mapF.getX();
        int offY = aj.mapF.getY();
		x-=offX;
		y-=offY;
		boolean draw=false;
		int z3 = aj.ta.zoom<<3;
		int z4 = aj.ta.zoom<<4;
		if(plantx>x){ ox = x; x=plantx; }
		else { ox = plantx; }
		if(planty>y){ oy = y; y=planty; }
		else { oy = planty; }
		if(fill==2){
            Graphics g = aj.img.getGraphics();
            char fc = aj.fillChar;
			if(g!=null){
            	for(int xpos = ox; xpos <= x; xpos+=z3){
					int tmpx = (xpos/z3)*z3;
           			for(int ypos = oy; ypos <= y; ypos+=z4){
						if(ypos == oy  && (xpos == ox || xpos == x)) fc = '.';
						else if(ypos == y && (xpos == ox || xpos == x)) fc = '\'';
						else if(ypos == oy || ypos == y) fc = '-';
						else if(xpos == ox || xpos == x) fc = '|';
						else continue;
					    aj.ta.setchar(fc,tmpx,(ypos/z4)*z4,aj.curColorIndex,aj.curCol,g,false);
					}
				}
            	g.dispose();
				aj.ta.repaint();
           	}
		}else{
            Graphics g = aj.img.getGraphics();
            if(g != null){
                g.setFont(env.font[aj.ta.zoom]);
            	for(int xpos = ox; xpos <= x; xpos+=z3){
					int tmpx = (xpos/z3)*z3;
            		for(int ypos = oy; ypos <= y; ypos+=z4){
						if(fill==1 || xpos == ox || xpos == x || ypos == oy || ypos == y){
					        aj.ta.setchar(aj.fillChar,tmpx,(ypos/z4)*z4,
						    	aj.curColorIndex,aj.curCol,g,false);
						}
					}
            	}
			}
			aj.ta.repaint();
            g.dispose();
        }
		return true; 
	}
	public void setchar(int c,int pos, int zoom, asciiJam aj){}
    public void add(int x, int y, int dir){}
    public void identify(int x, int y, int z, asciiJam ajam){}

    public boolean handleEvent(Event e){
	 if(e.target instanceof Button){
        if(e.id==1001)
            try{
            aj.getAppletContext().showDocument(new URL(env.helpFillURL),"_blank");
            }catch(Exception ex){}
     }
	 else if(e.target instanceof Choice){
            aj.fillChar = ((Choice)e.target).getSelectedItem().charAt(0);
            return false;
     }
     else if(e.target instanceof Checkbox){
        if(e.id==1001){
            if( ((Checkbox)e.target).getLabel().equals("Outline") ){
                  fill = 0; 
				  chCh.show();
				  lbl.show();
            }else if( ((Checkbox)e.target).getLabel().equals("Fill") ){
				  chCh.show();
				  lbl.show();
                  fill = 1;
			}else{
				  chCh.hide();
				  lbl.hide();
				  fill = 2;
			}
        }
     }
	 else if(e.id==Event.MOUSE_ENTER){
           env.setCursor(env.DEFAULT_CURSOR,aj);
     }
     else if(e.id==Event.MOUSE_MOVE){
          if(!defCurs){
              env.setCursor(env.DEFAULT_CURSOR,aj);
              defCurs = true;
          }
     }
     else if(e.id==Event.MOUSE_EXIT){
         if(e.target instanceof oval){
           env.setCursor(aj.tool,aj);
           defCurs = false;
         }
         else{
           env.setCursor(env.DEFAULT_CURSOR,aj);
           defCurs = true;
         }
     }
     return true;
  }

}
