/////////////////////////////////////////////////////////
// Copyright 1997 A.A. Butterfield (DCompose)
/////////////////////////////////////////////////////////
package com.dcompose.slideshow;

import java.util.*;
import java.awt.*;

/*************************************************
* Toolbar component to comtrol applet
*************************************************/
class Toolbar extends Panel
{
   /** parent application **/
	private Slideshow mParent;
	/** the three image buttons up/focused/down **/
   private Image mButtons[]=new Image[3];
   /** the index of the pressed button **/
   private int mPressed=-1;
   /** the index of the focused button **/
   private int mOver=-1;
   /** the width of each button **/
	final int mBWidth=40;
	/** true if ready to accept another press */
	private boolean mReady=true;

   /** used by double-buffering **/
   private Image offScreenImage;
   /** used by double-buffering **/
	private Dimension offScreenSize;
   /** used by double-buffering **/
	private Graphics offScreenGraphics;

   /*************************************************
   * double buffering
   *************************************************/
	public final synchronized void update(Graphics g)
	{	Dimension d=size();
		if ((offScreenImage==null) || (d.width!=offScreenSize.width) || (d.height!=offScreenSize.height))
		{	offScreenImage=createImage(d.width,d.height);
			offScreenSize=d;
			offScreenGraphics=offScreenImage.getGraphics();
		}
		offScreenGraphics.setColor(getBackground());
		offScreenGraphics.fillRect(0,0,size().width,size().height);
		paint(offScreenGraphics);
		g.drawImage(offScreenImage,0,0,null);
	}

   /*************************************************
   * constructs a toolbar
   * @param aParent the parent application
   *************************************************/
	public Toolbar(Slideshow aParent)
	{
		mParent=aParent;

      MediaTracker mt=new MediaTracker(mParent);
		for (int i=0; i<3; i++)
		{
			mButtons[i]=mParent.getImage(mParent.getDocumentBase(),"but"+(i+1)+".gif");
			mt.addImage(mButtons[i],i);
		}
		try
		{
      	mt.waitForAll();
     	}
     	catch (InterruptedException e)
     	{
     		System.out.println(e);
     	}
	}

   /*************************************************
   * set the busy state of the toolbar- when busy buttons
   * cannot be pressed
   * @param aBusy true if to be busy
   *************************************************/
	public void setBusy(boolean aBusy)
	{
		mReady=!aBusy;
	}

   /*************************************************
   * paint the toolbar
   * @see java.awt.Component
   *************************************************/
	public void paint(Graphics g)
	{
		g.drawImage(mButtons[0],0,0,this);

		Image im=null;
		int k=-1;
		if (mPressed>=0)
		{
			im=mButtons[2];
			k=mPressed;
		}
		else if (mOver>=0)
		{
			im=mButtons[1];
			k=mOver;
		}

		if (k>=0)
		{
			Graphics j=offScreenImage.getGraphics();
			j.clipRect(k*mBWidth,0,mBWidth,mBWidth);
			j.drawImage(im,0,0,this);
		}
	}

   /*************************************************
   * JDK1.0.2 method to handle all events.
   * detects mouse events and maps to control of toolbar
   *************************************************/
   public boolean handleEvent(Event event)
   {
		int over;
		switch(event.id)
		{
			case Event.MOUSE_EXIT:
				if (mOver!=-1)
				{
					mOver=-1;
					repaint();
				}
				break;
			case Event.MOUSE_MOVE:
				over=getTool(event.x, event.y);
				if (over!=mOver)
				{
					mOver=over;
					repaint();
				}
				break;
			case Event.MOUSE_DRAG:
				over=getTool(event.x, event.y);
				if (over!=mOver)
				{
					mOver=over;
					mPressed=-1;
					repaint();
				}
				break;
			case Event.MOUSE_DOWN:
				if (mReady)
				{
					mPressed=getTool(event.x, event.y);
					if (mPressed>=0) repaint();
				}
				break;

			case Event.MOUSE_UP:
				if (mReady)
				{
					int k=getTool(event.x, event.y);
					if (k==mPressed)
					{
						switch(k)
						{
							case 0:
								mParent.first();
								break;
							case 1:
								mParent.previous();
								break;
							case 2:
								mParent.next();
								break;
							case 3:
								mParent.slideshow();
								break;
							case 4:
								mParent.list();
								break;
							case 5:
								mParent.minimize();
								break;
							default:
								break;
						}
					}
				}
				mPressed=-1;
				repaint();
				break;
		}
      return super.handleEvent(event);
   }

   /*************************************************
   * maps a mouse coordinate to a button index
   * @param x the x coordinate
   * @param y the ycoordinate
   * @return the button
   *************************************************/
	private int getTool(int x, int y)
	{
			return (x/mBWidth);
	}


}
