/**
 File:      KaiButton.java
 Author:    Angus McIntyre <angus@pobox.com>
 Date:      10.06.96
 Updated:   26.08.96
 
 A KaiButton is a button that shows itself in three different states,
 depending on where the mouse is relative to it, and whether the mouse
 button is up or down. If the mouse is outside the button's region, the
 button is said to be inactive, and displays an inactive image. If the
 mouse enters the button region, a different image is displayed, and if
 the user clicks on it, then a third different image is shown.
 
 The name comes from Kai Krause's software Kai's Power Tools, and KPT
 Bryce, which have buttons that exhibit similar behaviours.
 
 Note that this is a minimal implementation, so that it can be used in
 different contexts. It doesn't even do anything when clicked, and it
 doesn't fetch the images it needs itself. This is left to subclasses
 and higher-level container objects, and is done deliberately, so that
 the core functionality can be reused in different ways.

    HISTORY
    -------
    
    11.06.99	Documented.
    05.07.98	Added code to fill the button with its parent's
    			background color before drawing. 
    26.08.96    First released version
    
    TO-DO
    -----
    
    
    LEGAL
    -----
    
    This software is free. It can be used and modified in any way you 
    choose, but it may not be sold, either separately or as part of a 
    collection without explicit prior permission from the author. The 
    author assumes no liability for any loss, damage or mental or 
    physical trauma you may incur through use of or inability to use 
    this software. This disclaimer must appear on any modified or 
    unmodified version of the software in which the name of the author
    also appears.
    
*/

/* ----------------------------------------------------------------------
 *                              IMPORTS
 * ---------------------------------------------------------------------- */

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.util.*;

/**
 * A class of three-state buttons that show different images dependent
 * on the mouse position/behaviour.
 * @author Angus McIntyre <angus@pobox.com>
 */

public  class KaiButton extends Canvas
{

	/**
	 * Possible states of the button.
	 */  
    
    static  final   int kInactiveState = 0;
    static  final   int kActiveState = 1;
    static  final   int kClickedState = 2;
    
	/**
	 * An internal variable used to track the current state
	 * of the button.
	 */  
    
    protected   int     state;
    
	/**
	 * Array of images used for the different button states.
	 */  

    protected   Image   image[];
    
	/**
	 * Construct the button. This basically involves setting the images
	 * up appropriately, fitting the button to the space indicated, and
	 * setting it to the inactive state.
	 */
    
    public  KaiButton(Image inactive,Image active,Image clicked,
                      int x, int y, int width, int height) {
        image = new Image[3];
        
        image[kInactiveState] = inactive;
        image[kActiveState] = active;
        image[kClickedState] = clicked;
        this.reshape(x,y,width,height);
        state = kInactiveState;
    }
    
    /**
     *
     * This is the key method used to mediate visible changes in the
     * state. Basically, you call it with the state you want it to go
     * into, and it updates the button's internal variables and forces
     * the button to be redrawn.
     */
     
    protected   void    changeState(int new_state) {
        state = new_state;
        this.paint(this.getGraphics());
    }
    
    /**
     * When the mouse enters the button, it becomes active.
     */
     
    public  boolean mouseEnter(Event event, int x, int y) {
        this.changeState(kActiveState);
        return true;
    }
    
    /**
     * When the mouse exits from the button, it's deactivated again.
     */
     
    public  boolean mouseExit(Event event, int x, int y) {
        this.changeState(kInactiveState);
        return true;
    }
    
    /**
     * When the mouse goes down, the button is shown as clicked.
     */
     
    public  boolean mouseDown(Event event, int x, int y) {
        this.changeState(kClickedState);
        return true;
    }
    
	/**
     * When the mouse goes up, the button goes back to being active
     * (because presumably the mouse is still within the button; if
     * it isn't, a queued 'mouseExit' event should still take care
     * of getting everything sorted out; it seems to work, anyway).
     */
     
    public  boolean mouseUp(Event event, int x, int y) {
        this.changeState(kActiveState);
        return true;
    }
    
	/**
	 * To paint the button, simply draw the correct image for 
     * whichever state the button is currently in.
     */
     
    public  void    paint(Graphics g) {
    	Rectangle	bounds = this.bounds();
    	g.setColor(this.getParent().getBackground());
    	g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
        g.drawImage(image[state],0,0,this.getParent());
    }
}
        

