#include "TButton.h"

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#include "gType.h"
#include "GraphLow.h"

void TButton::Init(int aX, int aY, int aW, int aH, int OwnerX, int OwnerY, u32 aBorderWidth, u32 aBorderColor)
{
	unsigned short Left,Right,Top,Bottom;
	Bound.SetBySize(aX+OwnerX, aY+OwnerY, aW, aH);
	OwnerLeftTop.x 	= OwnerX;
	OwnerLeftTop.y 	= OwnerY;
	Border.Width 	= aBorderWidth; 
	Border.Color 	= aBorderColor;
	
	Left 		= Bound.Left + 2;
	Right 	= Bound.Right - 2;
	Top 		= Bound.Top + 2;
	Bottom 	= Bound.Bottom - 2;
	
	Content.Set(Left,Top,Right,Bottom);
	
	Color = clNearWhite;
	TextColor = clNearBlack;
	
	SelectedColor = clAqua;
	SelectedTextColor = clNearBlack;
	
	DisableColor = clFrmFace;
	DisableTextColor = clSilver;
	
	SetCaption("Button xx", 24);
	
	Enable = true;
	Selected =0;
}


void TButton::SetCaption(const char *p,  unsigned int aFontSize)
{
	s16 len, i;
	s16 PixelWidth;
	
	len = sizeof(p);
	if(len>30)len = 30;
	Caption.FromStr(p);
	FontSize = aFontSize;
	PixelWidth = GetFontWidth(aFontSize);
	if(  (aFontSize==16) || (aFontSize == 24) ) PixelWidth = PixelWidth * len;
	CaptionLeftSpacing = (Bound.Width-PixelWidth) / 2; 
}

void TButton::SetColor(unsigned int aClr)
{
	Color = aClr;
}

void TButton::SetTextColor(unsigned int aClr)
{
	TextColor = aClr;
}

void TButton::SetCaptionLeftSpacing(u16 spc)
{
	CaptionLeftSpacing = spc;
}

void TButton::Draw(void)
{
	VertLineRender(Bound.Left+1,	Bound.Top+1, 	Bound.Height-2,	0xFF808080);
	VertLineRender(Bound.Right-1,	Bound.Top+1, 	Bound.Height-2,	0xFF808080);	
	VertLineRender(Bound.Left,	Bound.Top, 	Bound.Height,	0xFF000000);
	VertLineRender(Bound.Right,	Bound.Top, 	Bound.Height,	0xFF404040);
	
	HorizLineRender(Bound.Left+1, 	Bound.Top+1, 	Bound.Width - 2, 	0xFF808080);
	HorizLineRender(Bound.Left+1, 	Bound.Bottom-1, 	Bound.Width - 2, 	0xFF808080);
	HorizLineRender(Bound.Left, 	Bound.Top, 	Bound.Width, 	0xFF000000);
	HorizLineRender(Bound.Left, 	Bound.Bottom, 	Bound.Width, 	0xFF000000);	
	
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom,  Color);
	TextRender_string24(Content.Left + 1 + CaptionLeftSpacing , Content.Top + 2, TextColor, Caption.Text);
}

void TButton::Draw2(void)
{
	if(Enable){
		if(Selected)
			RectFillRender(Bound.Left, Bound.Top, Bound.Right, Bound.Bottom, SelectedColor);
		else
			RectFillRender(Bound.Left, Bound.Top, Bound.Right, Bound.Bottom, Color);
	}else{
		RectFillRender(Bound.Left, Bound.Top, Bound.Right, Bound.Bottom, DisableColor);
	}
	
	VertLineRender(Bound.Left,		Bound.Top, 		Bound.Height -1,	0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,	Bound.Height -2,	0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,		Bound.Height,	0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 		Bound.Width - 1, 	0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  Bound.Width - 2, 	0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,	Bound.Width,	0xFF404040);
	
	if(Enable){
		if(Selected)
			TextRender_string24( (Content.Left + 1 + CaptionLeftSpacing)  , (Content.Top + 2) , SelectedTextColor, Caption.Text);
		else
			TextRender_string24( (Content.Left + 1 + CaptionLeftSpacing)  , (Content.Top + 2) , TextColor, Caption.Text);
	}else{
		TextRender_string24( (Content.Left + 1 + CaptionLeftSpacing)  , (Content.Top + 2) , DisableTextColor, Caption.Text);
	}
	
}

void TButton::DrawInPress(void)
{
	if(Selected)
		RectFillRender(Bound.Left, Bound.Top, Bound.Right, Bound.Bottom, SelectedColor);
	else
		RectFillRender(Bound.Left, Bound.Top, Bound.Right, Bound.Bottom, Color);
	
	VertLineRender(Bound.Left+1,	Bound.Top+1, 	Bound.Height-2,	0xFF808080);
	VertLineRender(Bound.Right-1,	Bound.Top+1, 	Bound.Height-2,	0xFF808080);	
	VertLineRender(Bound.Left,	Bound.Top, 		Bound.Height,	0xFF000000);
	VertLineRender(Bound.Right,	Bound.Top, 		Bound.Height,	0xFF000000);
	
	HorizLineRender(Bound.Left+1, 	Bound.Top+1, 	Bound.Width - 2, 	0xFF808080);
	HorizLineRender(Bound.Left+1, 	Bound.Bottom-1, 	Bound.Width - 2, 	0xFF808080);
	HorizLineRender(Bound.Left, 		Bound.Top, 		Bound.Width, 	0xFF000000);
	HorizLineRender(Bound.Left, 		Bound.Bottom, 	Bound.Width, 	0xFF000000);
	
	if(Selected)
		TextRender_string24( (Content.Left + 1 + CaptionLeftSpacing)  , (Content.Top + 2) , SelectedTextColor, Caption.Text);
	else
		TextRender_string24( (Content.Left + 1 + CaptionLeftSpacing)  , (Content.Top + 2) , TextColor, Caption.Text);
}

void TButton::Show(void)
{
	Draw2();
}


