package com.uvigo.mhp.looks;

import java.awt.*;
import org.havi.ui.*;
import com.uvigo.mhp.ui.*;

public class HPanelLook1 implements HPanelLook
{
    public HPanelLook1()
    {
    }
    
    public Insets getInsets(HPanel panel)
    {
        if ((panel.getLabel()==null) || (panel.getLabel()==""))
            return new Insets(2,2,2,2);
        
        Font f=panel.getFont();
        FontMetrics fm=panel.getGraphics().getFontMetrics(f);
        int barHeight=fm.getMaxAscent()+fm.getMaxDescent()+4;
        return new Insets(barHeight,2,2,2);
    }
    
    public void showLook(Graphics g, HPanel panel)
    {
        Dimension dim=panel.getSize();
        int width=dim.width;
        int height=dim.height;
        
        g.setColor(panel.getBackground());
        g.drawRect(0,0,width-1,height-1);
        g.drawRect(1,1,width-3,height-3);
        
        if ((panel.getLabel()==null) || (panel.getLabel()==""))
            return;
        
        Font f=panel.getFont();
        FontMetrics fm=g.getFontMetrics(f);
        int barHeight=fm.getMaxAscent()+fm.getMaxDescent()+4;
        
        // Anadir ajustes para cadenas demasiado largas!
        
        g.fillRect(2, 2, width-4, barHeight-2);
        g.setColor(panel.getForeground());
        g.drawString(panel.getLabel(), 6, barHeight-fm.getMaxDescent());
    }
    
    public int hitTest(HPanel panel, Point pt)
    {
        Dimension dim=panel.getSize();
        int width=dim.width;
        int height=dim.height;
        
        if ((panel.getLabel()!=null) && (panel.getLabel()!=""))
        {
            Font f=panel.getFont();
            FontMetrics fm=panel.getGraphics().getFontMetrics(f);
            int barHeight=fm.getMaxAscent()+fm.getMaxDescent()+4;
            if (new Rectangle(2, 2, width-4, barHeight).contains(pt))
                return HPanelLook.CLICK_BAR;
            if (new Rectangle(2,barHeight,width-4,height-barHeight).contains(pt))
                return HPanelLook.CLICK_INSIDE;
            else return HPanelLook.CLICK_OUTSIDE;
        }
        else
        {
            if (new Rectangle(2,2,width-4,height-4).contains(pt))
                return HPanelLook.CLICK_INSIDE;
            else return HPanelLook.CLICK_OUTSIDE;        
        }
    }
}
