#include "RuntimeData.h"
#include "DetectorCurve.h"

#define TextTopOffSet	6
#define ListLineCnt	10


void TDetectorCurve::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	#define CH	30	//Caption Height
	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);
	SText.Init(Left+358,Top,434, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right, Bottom);
	RectPanel.Set(Left,Top+CH, Right, Bottom);
	RectCanvas.Set(6,102,616,392);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("系统调试->探测器曲线", Caption.Text);
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	CtlIndex = 0;
	FastIndex = 0;
			
	OnActive = 0;
	IsFastCmd = 0;
}

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

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

void TDetectorCurve::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 TDetectorCurve::DrawCaption2(void)
{	
	TMyString::sFromStr("/////", SText.Text);
	//SText.Show();
}

void TDetectorCurve::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawCaption2();
}

void TDetectorCurve::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		Caption.Show();
		DrawCaption2();
		aPanel->Show();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		aPanel->Show();
	}
}

void TDetectorCurve::SetAndShowCaption(unsigned char aType)
{
	TMyString::sFromStr(" zzzzzzz",  Caption.Text);
}

//______________________________________________________________________________________________________________________________

void TDetectorCurve::Check4Selected(void)
{
	unsigned int i;
	for(i=0; i<2;i++)aPanel->Btn[i].Selected = 0;
	for(i=0; i<7;i++)aPanel->Edit[i].Selected = 0;

		switch(CtlIndex){
			case 0:
				aPanel->Edit[0].Selected = 1;
				break;
			case 1:
				aPanel->Edit[1].Selected = 1;
				break;
			case 2:
				aPanel->Edit[2].Selected = 1;
				break;
			case 3:
				aPanel->Edit[3].Selected = 1;
				break;
			case 4:
				aPanel->Edit[4].Selected = 1;
				break;
			case 5:
				aPanel->Edit[5].Selected = 1;
				break;
			case 6:
				aPanel->Edit[6].Selected = 1;
				break;
			case 7:
				aPanel->Btn[0].Selected = 1;
				break;
			case 8:
				aPanel->Btn[1].Selected = 1;
				break;
			default:

				break;
		}	
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TDetectorCurve::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;

	if( 	(aKey == VK_DELETE) || 
		(aKey == VK_0) ||	
		(aKey == VK_1) ||
		(aKey == VK_2) ||
		(aKey == VK_3) ||
		(aKey == VK_4) ||
		(aKey == VK_5) ||
		(aKey == VK_6) ||	
		(aKey == VK_7) ||
		(aKey == VK_8) ||
		(aKey == VK_9) ||
		(aKey == VK_LEFT) ||
		(aKey == VK_RIGHT)
					){
		if(CtlIndex < 7){
			aPanel->Edit[CtlIndex].KeyIn(aKey);
			aPanel->Edit[CtlIndex].ReDraw();
		}
	}
	
	if(aKey == VK_UP){
		if(CtlIndex >0){
			CtlIndex--;
			Check4Selected();
			aPanel->ReDrawItems();
		}
	}else
	if(aKey == VK_DOWN){
		if(CtlIndex <8){
			CtlIndex++;
			Check4Selected();
			aPanel->ReDrawItems();
		}
	}else
	if(aKey == VK_TAB){
		
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		SendCmd();
	}

	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}

void TDetectorCurve::PanelCanvasOut()
{
	VertLineRender(RectCanvas.Left,		RectCanvas.Top,			RectCanvas.Height,			clSilver);
	VertLineRender(RectCanvas.Right, 		RectCanvas.Top, 			RectCanvas.Height, 		clSilver);
	HorizLineRender(RectCanvas.Left,		RectCanvas.Top,			RectCanvas.Width,			clSilver);
	HorizLineRender(RectCanvas.Left, 		RectCanvas.Bottom, 		RectCanvas.Width, 			clSilver);
	
	//VertLineRender(RectPanel.Left + 374,		RectPanel.Top + 3, 			320,			clSilver);
}

void TDetectorCurve::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	x = RectPanel.Left;
	y = RectPanel.Top;
	x2 = RectPanel.Right;
	y2 = RectPanel.Bottom;
	aW = RectPanel.Width;
	aH = RectPanel.Height;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);
	
	yt = y+6;
	aPanel->SText[0].Init(654,3,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(658,35,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(616,67,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(616,99,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(616,131,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(616,163,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(616,195,70,30,x,yt,clNearBlack,clFrmFace);
	
	yt = y+2;
	aPanel->SText[11].Init(360,0,		70,30,	x,yt,clNearBlack,clFrmFace);
	aPanel->SText[12].Init(  6,0,		70,30,	x,yt,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(0,  330,	70,30,	x,yt,clNearBlack,clFrmFace);
	aPanel->SText[14].Init(175,330,	70,30,	x,yt,clNearBlack,clFrmFace);
	aPanel->SText[15].Init(350,330,	70,30,	x,yt,clNearBlack,clFrmFace);
	aPanel->SText[16].Init(525,330,	70,30,	x,yt,clNearBlack,clFrmFace);

	TMyString::sFromStr("回路号", aPanel->SText[0].Text);
	TMyString::sFromStr("地址", aPanel->SText[1].Text);
	TMyString::sFromStr("数据1比例", aPanel->SText[2].Text);
	TMyString::sFromStr("数据2比例", aPanel->SText[3].Text);
	TMyString::sFromStr("数据3比例", aPanel->SText[4].Text);
	TMyString::sFromStr("数据4比例", aPanel->SText[5].Text);
	TMyString::sFromStr("时间比例", aPanel->SText[6].Text);
	
	TMyString::sFromStr("正在探测", aPanel->SText[11].Text);
	TMyString::sFromStr("类型", 	  aPanel->SText[12].Text);
	TMyString::sFromStr("数据名称1", aPanel->SText[13].Text);
	TMyString::sFromStr("数据名称2", aPanel->SText[14].Text);
	TMyString::sFromStr("数据名称3", aPanel->SText[15].Text);
	TMyString::sFromStr("数据名称4", aPanel->SText[16].Text);
	
	for(i=0; i<6;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	for(i=11; i<17;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	yt = y+6;
	aPanel->Edit[0].Init(736,3, 42,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(736,35,42,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(736,67,42,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[3].Init(736,99,42,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[4].Init(736,131,42,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[5].Init(736,163,42,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[6].Init(736,195,42,30,x,yt,clNearBlack,clNearWhite);
	yt = y+1;
	aPanel->Btn[0].Init(666,240,116,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(666,280,116,36,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("开始探测");
	aPanel->Btn[1].Caption.FromStr("停止探测");

	
	for(i=0; i<2;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(2);
		aPanel->Btn[i].SelectedColor = clBlue;
	}
	for(i=0; i<2;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(3);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("001");
		aPanel->Edit[i].Pst = 2;
	}
	for(i=2; i<7;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(5);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("10");
		aPanel->Edit[i].Pst = 1;
	}
	aPanel->Edit[6].Enable = 0;
	
	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	PanelCanvasOut();
}

void TDetectorCurve::SendCmd()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aP0, aP1;
	
	Len =5;
	
	iVal = aPanel->Edit[0].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aP0 = cVal;
	
	iVal = aPanel->Edit[2].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aP1 = cVal;
	
	if( (aP0<1) || (aP0 >40) ){
		return;
	}
	if( (aP1<1) || (aP0 >250) ){
		return;
	}
	
	Path0 = aP0;
	Path1 = aP1;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = Path0;
	Data[3] = Path1;
	
	aIde.BitF.Cmd = cVal;
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.CmdSendCustom(aIde, Data, Len);
}

void TDetectorCurve::SendCmdAbort()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aP0, aP1;
	
	Len =5;
	
	iVal = aPanel->Edit[0].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aP0 = cVal;
	
	iVal = aPanel->Edit[2].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aP1 = cVal;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[1] = 0;
	Data[2] = Path0;
	Data[3] = Path1;
	
	aIde.BitF.Cmd = cmQueryAbout;
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	Data[0] = 0;
	
	InnerCan.CmdSendCustom(aIde, Data, Len);
}

int TDetectorCurve::ExtRequst(unsigned char Prm)
{
	IsShowing = 0;
	IsOnGoing =0;
	SendCmdAbort();
	return 0;
}




