// Copyright Andy Deck 2003, GNU Public License 2
// With the notable provision that any commercial use
// must be approved by the copyright holder.
import java.awt.*;
import java.io.*;

public class toolbar extends Canvas { // implements MouseListener{
 asciiJam aj;
 int oldTool=-1;
 int curRollover = 0;

  public toolbar(asciiJam i){
        this.aj = i;
	 	//setBackground(Color.white);
	 	setBackground(Color.lightGray);
	 	setForeground(Color.black);
  }
  public void update(Graphics g){
	paint(g);
  }
  public void paint(Graphics g){
	if(oldTool != aj.tool){
       	g.fillRect(1,aj.tool*21,29,21); // selected tool
		//g.setColor(Color.white);
		g.setColor(Color.lightGray);
       	if(oldTool>=0) g.fillRect(1,oldTool*21,29,21); 
        g.setColor(Color.black);
	}
    for(int i=0;i<env.MAXTOOL;i++){ 
		       if(i==aj.tool) g.setColor(Color.white);
	           else g.setColor(Color.black); //g.setColor(Color.gray);

		       if(i==env.RECT_TOOL)    rectTool(g);
		  else if(i==env.OVAL_TOOL)    ovalTool(g);
		  else if(i==env.LINE_TOOL)    lineTool(g);
		  else if(i==env.FILL_TOOL)    bucketTool(g);
		  else if(i==env.PENCIL_TOOL)  pencilTool(g);
		  else if(i==env.DROPPER_TOOL) dropperTool(g);
		  else if(i==env.TEXT_TOOL)    textTool(g);
		  else if(i==env.MOVE_TOOL)    moveTool(g);
		  else if(i==env.ERASER_TOOL)  eraserTool(g);
		  else if(i==env.SAVE_TOOL)    fileTool(g);

	      g.setColor(Color.black); //g.setColor(Color.gray);
		  g.drawRect(0,i*21,30,21); // selected tool
	}
  }

  public void dropperTool(Graphics g){
/* this is ugly because Sun's VM (1.4.1)
 * under WindowsXP wouldn't deal correctly with images
 * and Apple's Quartz rendering doesn't deal correctly
 * with non-anti-aliased diagonal lines
 */
      g.fillRect(9,97+21,1,1);
      g.fillRect(10,96+21,1,1);
      g.fillRect(11,95+21,1,1);
      g.fillRect(12,94+21,1,1);
      g.fillRect(13,93+21,1,1);
      g.fillRect(14,92+21,1,1);
      g.fillRect(15,91+21,1,1);

      g.fillRect(17,93+21,1,1);
      g.fillRect(16,94+21,1,1);
      g.fillRect(15,95+21,1,1);
      g.fillRect(14,96+21,1,1);
      g.fillRect(13,97+21,1,1);
      g.fillRect(12,98+21,1,1);
      g.fillRect(11,99+21,1,1);

      g.fillRect(8,101+21,1,1);
      g.fillRect(7,100+21,1,1);

      g.fillRect(8,98+21,1,2);
      g.fillRect(9,100+21,2,1);
      g.fillRect(16,92+21,1,1);
      g.fillRect(14,90+21,4,1);
      g.fillRect(18,91+21,1,4);
      g.fillRect(19,91+21,2,1);
      g.fillRect(20,90+21,2,1);
      g.fillRect(21,88+21,1,2);
      g.fillRect(18,87+21,3,1);
      g.fillRect(17,88+21,2,1);
      g.fillRect(17,89+21,1,2);
  }
  public void pencilTool(Graphics g){
      g.fillRect(12,78+21,3,2);
      g.fillRect(12,77+21,2,1);
      g.fillRect(17,67+21,3,1);
      g.fillRect(17,70+21,1,1);

	  g.fillRect(16,77+21,1,1);
	  g.fillRect(16,76+21,1,1);
	  g.fillRect(17,75+21,1,1);
	  g.fillRect(17,74+21,1,1);
	  g.fillRect(18,73+21,1,1);
	  g.fillRect(18,72+21,1,1);
	  g.fillRect(19,71+21,1,1);
	  g.fillRect(19,70+21,1,1);
	  g.fillRect(20,69+21,1,1);
	  g.fillRect(20,68+21,1,1);

	  g.fillRect(12,76+21,1,1);
	  g.fillRect(12,75+21,1,1);
	  g.fillRect(13,74+21,1,1);
	  g.fillRect(13,73+21,1,1);
	  g.fillRect(14,72+21,1,1);
	  g.fillRect(14,71+21,1,1);
	  g.fillRect(15,70+21,1,1);
	  g.fillRect(15,69+21,1,1);
	  g.fillRect(16,68+21,1,1);
	  g.fillRect(16,67+21,1,1);

      g.fillRect(12,78+21,4,1);
      g.fillRect(12,79+21,3,1);
      g.fillRect(12,80+21,2,1);
      g.fillRect(12,81+21,1,1);
  }
  public void rectTool(Graphics g){
      g.drawRect(8,6,15,10);
  }
  public void ovalTool(Graphics g){
      g.drawOval(9,26,13,10);
  }
  public void lineTool(Graphics g){
/* this is ugly because Sun's VM (1.4.1)
 * under WindowsXP wouldn't deal correctly with images
 * and Apple's Quartz rendering doesn't deal correctly
 * with non-anti-aliased diagonal lines
 */
	for(int i=0;i<11;i++){
          g.fillRect(10+i,58-i,1,1);
	}
  }
  void bucketTool(Graphics g){
      g.fillRect(20,51+21,2,6);
      g.drawLine(20,50+21,20,57+21);
      g.drawLine(11,47+21,11,51+21);

      g.fillRect(14,52+21,1,1);
      g.fillRect(15,53+21,1,1);
      g.fillRect(16,52+21,1,1);

      g.drawLine(15,47+21,15,51+21);
      g.drawLine(12,46+21,14,46+21);

/* this is ugly because Sun's VM (1.4.1)
 * under WindowsXP wouldn't deal correctly with images
 * and Apple's Quartz rendering doesn't deal correctly
 * with non-anti-aliased diagonal lines
 */
      //g.drawLine(16,48,20,52);
      g.fillRect(16,48+21,1,1);
      g.fillRect(17,49+21,1,1);
      g.fillRect(18,50+21,1,1);
      g.fillRect(19,51+21,1,1);
      g.fillRect(20,52+21,1,1);

      //g.drawLine(20,52,13,59);
      g.fillRect(20,52+21,1,1);
      g.fillRect(19,53+21,1,1);
      g.fillRect(18,54+21,1,1);
      g.fillRect(17,55+21,1,1);
      g.fillRect(16,56+21,1,1);
      g.fillRect(15,57+21,1,1);
      g.fillRect(14,58+21,1,1);
      g.fillRect(13,59+21,1,1);

      //g.drawLine(8,54,15,47);
      g.fillRect(8,54+21,1,1);
      g.fillRect(9,53+21,1,1);
      g.fillRect(10,52+21,1,1);
      g.fillRect(11,51+21,1,1);
      g.fillRect(12,50+21,1,1);
      g.fillRect(13,49+21,1,1);
      g.fillRect(14,48+21,1,1);
      g.fillRect(15,47+21,1,1);

      //g.drawLine(8,55,12,59);
      g.fillRect(8,55+21,1,1);
      g.fillRect(9,56+21,1,1);
      g.fillRect(10,57+21,1,1);
      g.fillRect(11,58+21,1,1);
      g.fillRect(12,59+21,1,1);

      //g.drawLine(18,49,20,51);
      g.fillRect(18,49+21,1,1);
      g.fillRect(19,50+21,1,1);
      g.fillRect(20,51+21,1,1);
  }
  public void textTool(Graphics g){
  	  int y = 129;
	  int x = 9;
   	  g.drawLine(x+5,y+2,x,y+13);
   	  g.drawLine(x+5,y+2,x+10,y+13);
   	  g.drawLine(x+3,y+9,x+7,y+9);
   	  g.drawLine(x-1,y+13,x+1,y+13);
   	  g.drawLine(x+9,y+13,x+11,y+13);
		x++;
   	  g.drawLine(x+5,y+2,x+10,y+13);
   	  g.drawLine(x-1,y+13,x+1,y+13);
   	  g.drawLine(x+9,y+13,x+11,y+13);
   	  g.drawLine(x+3,y+2,x+5,y+2);

  }
  public void moveTool(Graphics g){
  	  int y = 158;
	  int x = 15;
      //g.fillRect(x-6,y,13,1);
   	  g.drawLine(x-6,y,x+6,y);
      //g.fillRect(x,y-6,1,13);
   	  g.drawLine(x,y+6,x,y-6);

      g.fillRect(x-5,y-1,1,3);
   	  //g.drawLine(x-5,y-1,x-5,y-1);
   	  //g.drawLine(x-5,y+1,x-5,y+1);

   	  //g.drawLine(x+5,y+1,x+5,y+1);
   	  //g.drawLine(x+5,y-1,x+5,y-1);
      g.fillRect(x+5,y-1,1,3);

   	  //g.drawLine(x-1,y-5,x-1,y-5);
   	  //g.drawLine(x+1,y-5,x+1,y-5);
      g.fillRect(x-1,y-5,3,1);

   	  //g.drawLine(x-1,y+5,x-1,y+5);
   	  //g.drawLine(x+1,y+5,x+1,y+5);
      g.fillRect(x-1,y+5,3,1);
  }
  public void fileTool(Graphics g){
  	  //int y = 158;
  	  int y = 285;
	  int x = 6;
	  float d = 1.5f;
   	  g.drawLine((int)(x+25/d),(int)((y+6)/d),(int)(x+25/d),(int)((y+25)/d));
      g.drawLine((int)(x+8/d),(int)((y+16)/d),(int)(x+21/d),(int)((y+16)/d));
      g.drawLine((int)(x+21/d),(int)((y+6)/d),(int)(x+21/d),(int)((y+15)/d));
      g.drawLine((int)(x+8/d),(int)((y+20)/d),(int)(x+21/d),(int)((y+20)/d));
      g.drawLine((int)(x+5/d),(int)((y+25)/d),(int)(x+7/d),(int)((y+25)/d));
      g.drawLine((int)(x+8/d),(int)((y+26)/d),(int)(x+21/d),(int)((y+26)/d));
      g.drawLine((int)(x+22/d),(int)((y+25)/d),(int)(x+24/d),(int)((y+25)/d));
      //g.setColor(Color.gray);
      g.drawLine((int)(x+5/d),(int)((y+6)/d),(int)(x+5/d),(int)((y+25)/d));
      g.drawLine((int)(x+8/d),(int)((y+6)/d),(int)(x+8/d),(int)((y+16)/d));
      g.drawLine((int)(x+5/d),(int)((y+6)/d),(int)(x+25/d),(int)((y+6)/d));
      g.drawLine((int)(x+8/d),(int)((y+20)/d),(int)(x+8/d),(int)((y+25)/d));
      g.drawLine((int)(x+21/d),(int)((y+20)/d),(int)(x+21/d),(int)((y+25)/d));
      g.fillRect((int)(x+11/d),(int)((y+22)/d),(int)(2/d),(int)(3/d));
      g.fillRect((int)(x+23/d),(int)((y+8)/d),(int)(2/d),(int)(2/d));
  }
  public void eraserTool(Graphics g){
  	  int y = 172;
	  int x = 7;
	  g.fillRect(x+8,y+0,8,1);
      g.fillRect(x+14,y+1,2,1);
      g.fillRect(x+15,y+2,1,3);
      g.fillRect(x+13,y+2,1,1);
      g.fillRect(x+14,y+5,1,1);
      g.fillRect(x+12,y+3,1,1);
      g.fillRect(x+13,y+6,1,1);
      g.fillRect(x+11,y+4,1,1);
      g.fillRect(x+12,y+7,1,1);
      g.fillRect(x+10,y+5,1,1);
      g.fillRect(x+11,y+8,1,1);
      g.fillRect(x+9,y+6,1,1);
      g.fillRect(x+10,y+9,1,1);
      g.fillRect(x+8,y+7,1,1);
      g.fillRect(x+9,y+10,1,1);
      g.fillRect(x+8,y+11,1,1);
      g.fillRect(x+7,y+8,1,5);
      g.fillRect(x+1,y+12,6,1);
      g.fillRect(x+0,y+8,1,5);
      g.fillRect(x+1,y+8,6,1);
      g.fillRect(x+1,y+7,1,1);
      g.fillRect(x+2,y+6,1,1);
      g.fillRect(x+3,y+5,1,1);
      g.fillRect(x+4,y+4,1,1);
      g.fillRect(x+5,y+3,1,1);
      g.fillRect(x+6,y+2,1,1);
      g.fillRect(x+7,y+1,1,1);
  }
  public boolean handleEvent(Event e){
      if(e.id==Event.MOUSE_DOWN){
	 if(e.x>0 && e.x<31 && e.y>0 && e.y<212){
                if(e.y<21){
				        oldTool=aj.tool;
                        aj.tool = env.RECT_TOOL;
						aj.svr.hide();
						aj.ff.hide();
						aj.rect.show();
						aj.oval.hide();
						aj.ersr.hide();
						aj.line.hide();
						aj.kbd.hide();
                }else if(e.y<42){
				        oldTool=aj.tool;
                        aj.tool = env.OVAL_TOOL;
						aj.svr.hide();
						aj.ff.hide();
						aj.oval.show();
						aj.ersr.hide();
						aj.rect.hide();
						aj.line.hide();
						aj.kbd.hide();
                }else if(e.y<63){
				        oldTool=aj.tool;
                        aj.tool = env.LINE_TOOL;
						aj.svr.hide();
						aj.ff.hide();
						aj.rect.hide();
						aj.oval.hide();
						aj.ersr.hide();
						aj.line.show();
						aj.kbd.hide();
                }else if(e.y<84){
				        oldTool=aj.tool;
                        aj.tool = env.FILL_TOOL;
						aj.svr.hide();
						aj.ff.show();
						aj.rect.hide();
						aj.ersr.hide();
						aj.oval.hide();
						aj.line.hide();
						aj.kbd.hide();
                }else if(e.y<105){
				        oldTool=aj.tool;
                        aj.tool = env.PENCIL_TOOL;
						aj.ff.hide();
						aj.svr.hide();
						aj.oval.hide();
						aj.rect.hide();
						aj.ersr.hide();
						aj.line.hide();
						aj.kbd.hide();
                }else if(e.y<126){
				        oldTool=aj.tool;
                        aj.tool = env.DROPPER_TOOL;
						aj.svr.hide();
						aj.ff.hide();
						aj.oval.hide();
						aj.ersr.hide();
						aj.rect.hide();
						aj.line.hide();
						aj.kbd.hide();
                }else if(e.y<147){
				        oldTool=aj.tool;
					    aj.ta.requestFocus();
                        aj.tool = env.TEXT_TOOL;
						aj.svr.hide();
						aj.ff.hide();
						aj.rect.hide();
						aj.oval.hide();
						aj.ersr.hide();
						aj.line.hide();
						aj.kbd.show();
                }else if(e.y<168){
				        oldTool=aj.tool;
                        aj.tool = env.MOVE_TOOL;
						aj.svr.hide();
						aj.ff.hide();
						aj.oval.hide();
						aj.rect.hide();
						aj.ersr.hide();
						aj.line.hide();
						aj.kbd.hide();
                }else if(e.y<190){
				        oldTool=aj.tool;
                        aj.tool = env.ERASER_TOOL;
						//aj.ersr.show();
						aj.svr.hide();
						aj.ff.hide();
						aj.oval.hide();
						aj.rect.hide();
						aj.line.hide();
						aj.ersr.hide();
						aj.kbd.hide();
                }else if(e.y<212){
				        oldTool=aj.tool;
                        aj.tool = env.SAVE_TOOL;
						aj.svr.show();
						aj.ff.hide();
						aj.oval.hide();
						aj.rect.hide();
						aj.line.hide();
						aj.ersr.hide();
						aj.kbd.hide();
				}
		        repaint();
        }
      }else if(e.id==Event.MOUSE_UP||e.id==Event.MOUSE_EXIT){
	  		env.setCursor(aj.tool,aj);
			repaint();
      }else if(e.id==Event.MOUSE_ENTER){
        	    env.setCursor(env.DEFAULT_CURSOR,aj);
      }else if(e.id==Event.MOUSE_MOVE){
	  		int y = e.y/21;
	        if(y==env.RECT_TOOL){
                        if(curRollover!=env.RECT_TOOL) aj.usrLab.setText("Rectangle tool");
                        curRollover = y;
			}else if(y==env.OVAL_TOOL){
                        if(curRollover!=env.OVAL_TOOL) aj.usrLab.setText("Oval tool");
                        curRollover = y;
            }else if(y==env.LINE_TOOL){
                        if(curRollover!=env.LINE_TOOL) aj.usrLab.setText("Line tool");
                        curRollover = y;
            }else if(y==env.FILL_TOOL){
                        if(curRollover!=env.FILL_TOOL) aj.usrLab.setText("Fill tool");
                        curRollover = y;
            }else if(y==env.PENCIL_TOOL){
                        if(curRollover!=env.PENCIL_TOOL) aj.usrLab.setText("Draw  tool");
                        curRollover = y;
            }else if(y==env.DROPPER_TOOL){
                        if(curRollover!=env.DROPPER_TOOL) aj.usrLab.setText("Color picker tool");
                        curRollover = y;
            }else if(y==env.TEXT_TOOL){
                        if(curRollover!=env.TEXT_TOOL) aj.usrLab.setText("Text tool");
                        curRollover = y;
            }else if(y==env.MOVE_TOOL){
                        if(curRollover!=env.MOVE_TOOL) aj.usrLab.setText("Move tool");
                        curRollover = y;
            }else if(y==env.ERASER_TOOL){
                        if(curRollover!=env.ERASER_TOOL) aj.usrLab.setText("Eraser tool");
                        curRollover = y;
            }else if(y==env.SAVE_TOOL){
                        if(curRollover!=env.SAVE_TOOL) aj.usrLab.setText("Save file");
                        curRollover = y;
            }
      }else if(e.id==Event.MOUSE_EXIT){
            aj.usrLab.setText("");
	  }
      return true;
  }

}
