#include"MessageBox.h"

#define CH	30	//Caption Height

void TMessageBox::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{

	unsigned short Left,Right, Top, Bottom, Width, Height, i;
	Bound.SetBySize(OwnerX + x1, OwnerY + y1, aW, aH);
	Left = Bound.Left +3;
	Top = Bound.Top+3;
	Width = aW - 7;
	Height = aH - 7;
	Right = Bound.Right -4;
	Bottom = Bound.Bottom -4;
	Caption.Init(Left,Top,Width, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right, Bottom);
	BoxBound.Set(Left+200,Top+CH+50, Right-200, Bottom-180);
	SText.Init(60,24,BoxBound.Width - 60 - 7, CH, BoxBound.Left, BoxBound.Top, clNearBlack, clFrmFace);
	
	Caption.Border.Color = clGray;
	
	Color = aColor;
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	BoxColor = clFrmFace;
	
	sIndex = 2;
	
	DoWhatDone = 0;
	ReturnVal[0] = guiMsgReturn;
	ReturnVal[1] = guiMsgReturn;
	ReturnVal[2] = guiMsgReturn;
	ReturnVal[3] = guiMsgReturn;
	
	Btn[0].Init(0,0,90,36,		BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[1].Init(200,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[2].Init(310,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);	
	Btn[3].Init(310,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	
	Caption.TextClear();
	SText.TextClear();
	Btn[0].Caption.Clear();
	Btn[1].Caption.Clear();
	Btn[2].Caption.Clear();
	Btn[3].Caption.Clear();
}

void TMessageBox::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

void TMessageBox::RenderBackGround(unsigned int aClr)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, aClr);
}

void TMessageBox::DrawSelf(void)
{
	unsigned int x, y, x2, y2, w, h;
	
	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);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);
}

void TMessageBox::SetBtnOkCancel()
{
	sIndex = 2;

	Btn[0].Visible = 0;
	Btn[1].Visible = 0;
	Btn[2].Visible = 0;
	
	Btn[0].Init(0,0,90,36,		BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[1].Init(200,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[2].Init(310,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);

	Btn[1].Enable = 0;
	Btn[1].Visible = 1;
	Btn[1].SetCaptionLeftSpacing(6);
	Btn[1].SelectedColor = clBlue;
	Btn[2].Enable = 1;
	Btn[2].Visible = 1;
	Btn[2].SetCaptionLeftSpacing(6);
	Btn[2].SelectedColor = clBlue;
	
	Btn[1].Caption.FromStr("确定");
	Btn[2].Caption.FromStr("返回");
}

void TMessageBox::SetBtnOk1Ok2Cancel()
{
	sIndex = 2;

	Btn[0].Visible = 0;
	Btn[1].Visible = 0;
	Btn[2].Visible = 0;
	Btn[3].Visible = 0;

	
	Btn[0].Init(90, 90,70,36,		BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[1].Init(200,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[2].Init(310,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[3].Init(310,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	
	Btn[0].Enable = 0;
	Btn[0].Visible = 1;
	Btn[0].SetCaptionLeftSpacing(6);
	Btn[0].SelectedColor = clBlue;
	Btn[1].Enable = 0;
	Btn[1].Visible = 1;
	Btn[1].SetCaptionLeftSpacing(6);
	Btn[1].SelectedColor = clBlue;
	Btn[2].Enable = 1;
	Btn[2].Visible = 1;
	Btn[2].SetCaptionLeftSpacing(6);
	Btn[2].SelectedColor = clBlue;
}

void TMessageBox::DrawBox(void)
{
	RectFillRender(BoxBound.Left, 	BoxBound.Top, 		BoxBound.Right, 	BoxBound.Bottom, 	BoxColor);
	
	VertLineRender(BoxBound.Left,		BoxBound.Top, 		BoxBound.Height -1,			0xFFFFFFFF);
	VertLineRender(BoxBound.Right -1,	BoxBound.Top +1,		BoxBound.Height -2,			0xFF808080);
	VertLineRender(BoxBound.Right,	BoxBound.Top,		BoxBound.Height,			0xFF404040);
	
	HorizLineRender(BoxBound.Left, 	BoxBound.Top, 		BoxBound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(BoxBound.Left +1, 	BoxBound.Bottom -1,  	BoxBound.Width - 2, 			0xFF808080);
	HorizLineRender(BoxBound.Left,	BoxBound.Bottom,		BoxBound.Width,			0xFF404040);
}

void TMessageBox::DrawBtn()
{
	if(Btn[0].Visible){
		Btn[0].Show();
	}
	if(Btn[1].Visible){
		Btn[1].Show();
	}
	if(Btn[2].Visible){
		Btn[2].Show();
	}
	if(Btn[3].Visible){
		Btn[3].Show();
	}
}

void TMessageBox::SetBoxColor(unsigned int aClr)
{
	BoxColor = aClr;
}

void TMessageBox::CheckSelect()
{
	Btn[0].Selected=0;
	Btn[1].Selected=0;
	Btn[2].Selected=0;
	Btn[3].Selected=0;
	if(sIndex == 0){
		Btn[0].Selected = 1;
	}else
	if(sIndex == 1){
		Btn[1].Selected = 1;
	}
	if(sIndex == 2){
		Btn[2].Selected = 1;
	}else
	if(sIndex == 3){
		Btn[3].Selected = 1;
	}
}

void TMessageBox::DrawMessage()
{
	SText.Show();
}

void TMessageBox::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawBox();
	DrawMessage();
	CheckSelect();
	DrawBtn();
}

int TMessageBox::FindNextBtn(int aLR)
{
	int i, aInx;
	aInx = sIndex;
	if(DoWhatDone){
		if(aLR <0){
			for(i=0; i<4; i++){
				aInx--;
				if(aInx < 0)aInx = 3;
				if(Btn[aInx].Enable){
					sIndex = aInx;
					return 1;
				}
			}
		}else
		if(aLR >0){
			for(i=0; i<4; i++){
				aInx++;
				if(aInx >3)aInx = 0;
				if(Btn[aInx].Enable){
					sIndex = aInx;
					return 1;
				}
			}
		}
	}
	return 0;
}

TGuiMsgReturn TMessageBox::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aCh;

	if(aKey == VK_LEFT){
		if(DoWhatDone){
			if(FindNextBtn(-1)){
				CheckSelect();
				DrawBtn();
			}
		}
	}else
	if(aKey == VK_RIGHT){
		if(FindNextBtn(1)){
			CheckSelect();
			DrawBtn();
		}
	}	

	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(DoWhatDone){
			aMsg = ReturnVal[sIndex];
		}
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}


void TMessageBox::SetReturnValue(TGuiMsgReturn aV0, TGuiMsgReturn aV1, TGuiMsgReturn aV2, TGuiMsgReturn aV3)
{
	ReturnVal[0] = aV0;
	ReturnVal[1] = aV1;
	ReturnVal[2] = aV2;
	ReturnVal[3] = aV3;
}

void TMessageBox::DoWait4ExitLogin()
{
	volatile unsigned int i;
	SText.Init(100,24,BoxBound.Width - 40 -7, CH, BoxBound.Left, BoxBound.Top, clNearBlack, clFrmFace);
	SText.SetText("确认退出登录", 24);
	SText.TextColor = clRed;
	Btn[1].Visible = 1;
	Btn[2].Visible = 1;
	DrawMessage();
	DoWhatDone = 1;
	Btn[1].Enable = 1;
	DrawBtn();	
}

void TMessageBox::DoWait4RunMode()
{
	volatile unsigned int i;
	Btn[0].Visible = 0;
	Btn[1].Visible = 0;
	Btn[2].Visible = 0;
	Btn[3].Visible = 0;

	
	Btn[0].Init(70, 90,110,36,		BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[1].Init(190,90,110,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[2].Init(310,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	Btn[3].Init(310,90,70,36,	BoxBound.Left,BoxBound.Top,clNearBlack,clFrmFace);
	
	Btn[0].Enable = 0;
	Btn[0].Visible = 1;
	Btn[0].SetCaptionLeftSpacing(6);
	Btn[0].SelectedColor = clBlue;
	Btn[1].Enable = 0;
	Btn[1].Visible = 1;
	Btn[1].SetCaptionLeftSpacing(6);
	Btn[1].SelectedColor = clBlue;
	Btn[2].Enable = 1;
	Btn[2].Visible = 1;
	Btn[2].SetCaptionLeftSpacing(6);
	Btn[2].SelectedColor = clBlue;
	
	SText.Init(25,24,300, CH, BoxBound.Left, BoxBound.Top, clNearBlack, clFrmFace);
	SText.SetText("选择要运行的状态,按确定执行", 24);
	SText.TextColor = clRed;
	Btn[0].Visible = 1;
	Btn[1].Visible = 1;
	Btn[2].Visible = 1;
	Btn[0].Caption.FromStr("正常运行");
	Btn[1].Caption.FromStr("调试运行");
	Btn[2].Caption.FromStr("返回");
	Btn[0].Enable = 1;
	Btn[1].Enable = 1;
	SText.TextColor = clNearBlack;
	
	DoWhatDone = 1;
}

void TMessageBox::DoWait4EnterSelfCheck()
{
	volatile unsigned int i;
	SText.Init(60,24,BoxBound.Width - 60 -7, CH, BoxBound.Left, BoxBound.Top, clNearBlack, clFrmFace);
	SText.SetText("按确定键开始系统自检", 24);
	SText.TextColor = clRed;
	Btn[1].Visible = 1;
	Btn[2].Visible = 1;
	DrawMessage();
	for(i=0; i< 40000000; i++);
	DoWhatDone = 1;
	Btn[1].Enable = 1;
	SText.TextColor = clNearBlack;
	DrawBtn();	
}

void TMessageBox::DoWait4SaveCurrent()
{
	volatile unsigned int i;
	SText.Init(40,24,BoxBound.Width - 40 -7, CH, BoxBound.Left, BoxBound.Top, clNearBlack, clFrmFace);
	SText.SetText("按确定键保存,按返回键返回", 24);
	SText.TextColor = clRed;
	Btn[1].Visible = 1;
	Btn[2].Visible = 1;
	DrawMessage();
	for(i=0; i< 40000000; i++);
	DoWhatDone = 1;
	Btn[1].Enable = 1;
	SText.TextColor = clNearBlack;
	DrawBtn();	
}

void TMessageBox::DoReadWriteBusPad()
{
	volatile unsigned int i;
	SText.Init(25,24,BoxBound.Width - 25 -7, CH, BoxBound.Left, BoxBound.Top, clNearBlack, clFrmFace);
	SText.SetText("选择要运行的状态,按确定执行", 24);
	SText.TextColor = clRed;
	Btn[0].Visible = 1;
	Btn[1].Visible = 1;
	Btn[2].Visible = 1;
	Btn[0].Caption.FromStr("读取编程");
	Btn[1].Caption.FromStr("保存编程");
	Btn[2].Caption.FromStr("返回");
	DrawMessage();
	for(i=0; i< 40; i++);
	DoWhatDone = 1;
	Btn[0].Enable = 1;
	Btn[1].Enable = 1;
	SText.TextColor = clNearBlack;
	DrawMessage();
	DrawBtn();
}

void TMessageBox::ReqAdjMode()
{
	volatile unsigned int i;
	SText.Init(100,24,200, CH, BoxBound.Left, BoxBound.Top, clNearBlack, clFrmFace);
	SText.SetText("进入调试状态", 24);
	SText.TextColor = clRed;
	Btn[1].Visible = 1;
	Btn[2].Visible = 1;
	DrawMessage();
	DoWhatDone = 1;
	Btn[1].Enable = 1;
	DrawBtn();	
}









