/*
 * Copyright (c) 1997, Subrahmanyam Allamaraju. All Rights Reserved.
 * 
 * Permission to use, copy, modify, and distribute this software for
 * NON-COMMERCIAL purposes and without fee is hereby granted provided that this
 * copyright notice appears in all copies.
 *
 * This software is intended for demonstration purposes only, and comes without
 * any explicit or implicit warranty.
 *
 * Please report any bugs to subrahmanyam@geocities.com
 *
 */


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



public class TitleBar extends Canvas 
{

    /*
     * Static variables common to all buttons.
     */
    static ButtonBar parent;
    static Font font;
    static Color textColor;
    static int buttonWidth = 0;
    String position = "left";
    String align = "left";

    String title;
    
    boolean gotFocus = false;
    boolean pressed = false;
    
    ThreeDBorder buttonBorder;
    int borderWidth = 2;
    
    Image blackImage = null;
    Image redImage = null;
    Image curImage = null;
    
    ImageFilter filter = null;
    Graphics gr = null;
 
    int xpos = 10, ypos;
    
    double angle = (double)0.5 * Math.PI;
    double fScaleFactor = 0.75;
    boolean toUpper = false;
    boolean mFocus = false;
    
    String message = "Welcome";
    
    /*
     * A dummy constructor.
     */
    TitleBar() 
	{
	    super();
	}
    

    TitleBar(ButtonBar parent, String title, String titleFont, int titleSize,
	     String message, String position) 
	{
	    this.title = (title == null) ? "My Home" : title;
	    if(toUpper) this.title = this.title.toUpperCase();
	    
	    this.parent = parent;
	    this.font =  new Font(titleFont, Font.BOLD, titleSize);
	    this.position = position;
	    
	    if(position.equals("top")) angle = 0.0;
	    else if(position.equals("right")) angle = (double)1.5 * Math.PI;
	    else angle = (double)0.5 * Math.PI;

	    if(message != null) this.message = message;
	}
    
    public void setFont(String font, int size) 
	{
	    this.font = new Font(font, Font.BOLD, size);
	}
    
    public void setAlign(String align) 
	{
	    this.align = align;
	}
    
    public void createImages() 
	{
	    blackImage = createImage(Color.black);
	    
	    redImage = createImage(Color.red);

	    curImage = blackImage;
	    
	}
    
    /*
     * This method creates a rotated image for vertical title.
     */
    private Image createImage(Color c) 
	{
	    /*
	     * Create a dummy image.
	     */
	    Image image = parent.createImage(parent.size().height, 100);
	    
	    /*
	     * Create an imagefilter to rotate the title.
	     */
	    ImageFilter filter = new RotateFilter(angle);

	    /*
	     * Get graphics and fontmetrics.
	     */
	    Graphics gr = image.getGraphics();
	    //gr.setBackground(Color.white);
	    gr.setColor(Color.white);
	    gr.fillRect(0, 0, image.getWidth(parent), image.getHeight(parent));
	    
	    gr.setFont(font);

	    FontMetrics fm = gr.getFontMetrics();
	    int width;
	    if(toUpper) width = this.getUCWidth(title, font, gr);
	    else width = fm.stringWidth(title);
	    
	    int height = fm.getHeight();
	    int ascent = fm.getAscent();
	    int leading = fm.getLeading();
	    int verMarzin = (size().height - height)/2;
	    
	    int h = verMarzin + ascent + leading;
	    parent.setBackground(Color.white);
	    
	    /*
	     * Create the image.
	     */
	    image = parent.createImage(width + 6, height);
	    
	    /*
	     * Set graphics attributes and draw the string.
	     */
	    gr = image.getGraphics();
	    gr.setFont(font);
	    
	    gr.setColor(c);
	    
	    if(toUpper) this.drawUCString(title, gr, font, 4, ascent + leading);
	    else gr.drawString(title, 4, ascent + leading);

	    ImageProducer producer = new FilteredImageSource(image.getSource(),
							     filter);
	    image = createImage(producer);

	    return image;
	}
    
    public int getHeight() 
	{
	    return blackImage.getHeight(parent);
	}

    public int getWidth() 
	{
	    return blackImage.getWidth(parent);
	}
        
    public void paint(Graphics g) 
	{
	    update(g);
	}
    
    public void update(Graphics g) 
	{
	    int xpos;
	    if(position.equals("left")) {
		g.drawImage(curImage, 0, parent.size().height -
			    curImage.getHeight(this), this); 
	    }
	    else if(position.equals("right")) {
		g.drawImage(curImage, 0, 0, this);
	    }
	    
	}

    public boolean mouseEnter(Event evt, int x, int y) 
	{
	    parent.showStatus(message);
	    curImage = redImage;
	    repaint();
	    
	    return true;
	} 

    public boolean mouseExit(Event evt, int x, int y) 
	{
	    parent.showStatus(" ");
	    curImage = blackImage;
	    repaint();
	    
	    return true;
	}

    private int getUCWidth(String uString, Font bigFont, Graphics g) 
	{

	    int i = 0;		
	    int x = 0;		
	    char c;		
	    StringBuffer next;		
	    String nextString;		
	    boolean upper;				

	    Font smallFont = new Font(bigFont.getName(), bigFont.getStyle(),
				      (int)(bigFont.getSize() * fScaleFactor));
	    FontMetrics bigMetrics = getFontMetrics(bigFont);
	    FontMetrics smallMetrics = getFontMetrics(smallFont);

	    int length = 0;
	    g.setFont(bigFont);
	    length = bigMetrics.stringWidth(uString.substring(0, 1));
	    
	    g.setFont(smallFont);
	    length += smallMetrics.stringWidth(uString.substring(1));
	    
	    return length;
	}

    public void drawUCString(String title, Graphics gr, Font bigFont, int x, int
			     y) 
	{
	    Font smallFont = new Font(bigFont.getName(), bigFont.getStyle(),
				      (int)(bigFont.getSize() * fScaleFactor));
	    FontMetrics bigMetrics = getFontMetrics(bigFont);
	    FontMetrics smallMetrics = getFontMetrics(smallFont);
	    
	    int deltaX;
	    
	    
	    gr.setFont(bigFont);
	    gr.drawString(title.substring(0, 1), x, y);
	    
	    gr.setFont(smallFont);
	    
	    deltaX = smallMetrics.stringWidth(title.substring(0, 1));
	    
	    gr.drawString(title.substring(1), x + deltaX, y);
	}
}

