#include <string.h>
#include <graphics.h>

#include "..\comp\Buttons\Button.h"

CButton::CButton(int X, int Y, int Width, int Height,char* Text)
{
	ClickHandler 			= NULL;
	iX						= X;
	iY 						= Y;
	iWidth					= Width;
	iHeight					= Height;
	colColor				= DefaultBackgroundColor;
	colClickedColor			= DefaultBackgroundColor;
	colTextColor			= DefaultTextColor;
	colClickedTextColor		= DefaultTextColor;
	bsBorder				= DefaultBorderStyle;
	taText					= DefaultTextAlign;
	bState					= 0;
	strcpy(szText,Text);
	Draw();
}

CButton::~CButton()
{


}

void CButton::SetText(char* pzText)
{
	strcpy(szText, pzText);
	Draw();
}

void CButton::Draw(void)
{
	setfillstyle(SOLID_FILL,(bState ? colClickedColor : colColor));
	bar(iX, iY,iX + iWidth,iY + iHeight);
	switch(bsBorder)
	{
		case Border3D:		Draw3DBorder();
								break;
		case FlatBorder:  DrawFlatBorder();
								break;
		default:				break;
	}
	DrawText();
}
void CButton::DrawText(void) 
{
	setcolor((bState ? colClickedTextColor : colTextColor));
	settextjustify(taText,Center);
	settextstyle(0,0,0); 
	switch(taText)
	{
		case Center:
			outtextxy(3+iX + (iWidth / 2),iY + (iHeight / 2),szText);
			break;
		case Left:
			outtextxy(3+iX,iY + (iHeight /2),szText);
			break;
		case Right:
			outtextxy(3+iX + iWidth, iY + (iHeight / 2),szText);
			break;
	}
}
void CButton::Draw3DBorder(void)
{
	setcolor((bState ? Black : White));
	line(iX,iY,iX + iWidth,iY);
	line(iX,iY,iX,iY + iHeight);
	setcolor((bState ? White : Black));
	line(iX + iWidth,iY + 1,iX + iWidth,iY + iHeight);
	line(iX,iY + iHeight,iX + iWidth,iY + iHeight);
}
void CButton::DrawFlatBorder(void)
{
	setcolor(Black);
	line(iX,iY,iX + iWidth,iY);
	line(iX,iY,iX,iY+ iHeight);
	line(iX + iWidth,iY,iX + iWidth,iY + iHeight);
	line(iX,iY + iHeight,iX + iWidth,iY + iHeight);
}
void CButton::SetColor(Colors Color)
{
	colColor = Color;
	Draw();
}
void CButton::SetClickedColor(Colors Color)
{
	colClickedColor = Color;
	Draw();
}
void CButton::SetTextColor(Colors Color)
{
	colTextColor = Color;
	Draw();
}
void CButton::SetClickedTextColor(Colors Color)
{
	colClickedTextColor = Color;
	Draw();
}
void CButton::SetTextAlign(TextAlign Align)
{
	taText = Align;
	Draw();
}
void CButton::SetBorderStyle(BorderStyle Border)
{
	bsBorder = Border;
	Draw();
}
void CButton::Click(void)
{
	bState = (bState ? 0 : 1);
	Draw();
	if(ClickHandler != NULL)
		ClickHandler();
}