/*
 * Created on Jan 5, 2004
 *
 */
package LumiSoft.UI.Controls;

import java.awt.*;
import java.awt.event.*;

/**
 * PictureBox control.
 */
public class WPictureBox extends WControl
{
	private Image     m_pImage             = null;
	private boolean   m_Strech             = true;
	private Point     m_MousePos           = new Point(-1,-1);
//	private int       m_MouseModifiers     = 0;
	
	/**
	 * Default constructor.
	 *
	 */
	public WPictureBox()
	{
		super();
		
		this.addMouseListener(new MouseAdapter(){			
			public void mouseEntered(MouseEvent e){
				OnMouseEntered(e);
			}
			
			public void mouseExited(MouseEvent e){
				OnMouseExited(e);
			}
		});
		this.addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseMoved(MouseEvent e){
				OnMouseMoved(e);
			}
		});
	}
	
	/**
	 * Is called by system when control need to repaint.
	 */
	public void paintComponent(Graphics g)
	{
		Rectangle clientRect = new Rectangle(0,0,this.getSize().width,this.getSize().height);
		boolean hot = this.hasFocus() || (clientRect.contains(m_MousePos) && this.isEnabled());		
		DrawControl((Graphics2D)g,hot);
	}
	
	private void DrawControl(Graphics2D g,boolean hot)
	{
		g.setColor(ViewStyle.GetColorFromRgb(212,208,200));
		g.fill(new Rectangle(0,0,this.getWidth() - 1,this.getHeight() - 1));
						
		//--- Draw image ---------------------------------------------------------------------//
		if(m_pImage != null){
			// Draw streched image
			if(m_Strech){
				g.drawImage(m_pImage,1,1,this.getWidth() - 2,this.getHeight() - 2,this);
			}
			// Just center image. If image is bigger than visible area, partial image is shown. 
			else{
				int x = 1;
				if(m_pImage.getWidth(this) < this.getWidth() - 2){
					x = (this.getWidth() - m_pImage.getWidth(this)) / 2;
				}
				int y = 1;
				if(m_pImage.getHeight(this) < this.getHeight() - 2){
					y = (this.getHeight() - m_pImage.getHeight(this)) / 2;
				}
				g.drawImage(m_pImage,x,y,this);			
			}
		}
		//-------------------------------------------------------------------------------------//
		
		// Draw border around picturebox
		g.setColor(getViewStyle().GetBorderColor(hot));
		g.draw(new Rectangle(0,0,this.getWidth() - 1,this.getHeight() - 1));
	}
	
		
	/**
	 * Is called when mouse cursor has entered to this control.
	 * @param e
	 */
	private void OnMouseEntered(MouseEvent e)
	{
		this.repaint();
	}
	
	/**
	 * Is called when mouse cursor has exited this control.
	 * @param e
	 */
	private void OnMouseExited(MouseEvent e)
	{
		m_MousePos = new Point(-1,-1);
		
		this.repaint();
	}
	
	/**
	 * Is called when mouse has moved.
	 * @param e
	 */
	public void OnMouseMoved(MouseEvent e)
	{
		m_MousePos = e.getPoint();
	}
	
	
	/**
	 * Gets image.
	 * @return
	 */
	public Image getImage()
	{
		return m_pImage;
	}
	/**
	 * Sets image.
	 * @param value
	 */
	public void setImage(Image value)
	{
		if(m_pImage != value){
			m_pImage = value;
			
			this.repaint();
		}
	}
	
	/**
	 * Gets if to strech image to fix picturebox.
	 * @return
	 */
	public boolean getStretch()
	{
		return m_Strech;
	}
	/**
	 * Sets if to strech image to fix picturebox.
	 * @param value
	 */
	public void setStrech(boolean value)
	{
		if(m_Strech != value){
			m_Strech = value;
			
			this.repaint();
		}
	}

}
