#include "RegisterAddRemove.h"
#include "RuntimeData2.h"

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	400
#define ipdWidth1		390

void TRegisterAddRemove::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	(319,		Top,		480,   CH, 0, 0, clWhite, clMaroon);
	
	Content0.Set(Left, 	Top+CH, 	Left + ipdWidth0 -1, 	Bottom);
	Left = Left + ipdWidth0 ;
	Content1.Set(Left,  	Top+CH, 	Right, 	Bottom);
	
	VScrollBarBox.Set(Right -20 +1, Top + CH, Right, Bottom);
	Color = aColor;
	#undef CH

	ActiveLineNum=0;
	
	ListView.ColHeight = 33;
	ListView.RowCount = 9;
	ListView.FixColTop = Content0.Top;
	ListView.FixColBottom = ListView.FixColTop + ListView.ColHeight;
	ListView.ColTop[0] = ListView.FixColBottom + 1;
	ListView.ColBottom[0] = ListView.ColTop[0] + ListView.ColHeight -1;
	ListView.ColLeft[0] = 1;
	ListView.ColRight[0] = ListView.ColWidth[0] +1;
	for(i=1; i<12; i++){
		ListView.ColTop[i] = ListView.ColTop[i -1] + ListView.ColHeight;
		ListView.ColBottom[i] =  ListView.ColBottom[i-1] + ListView.ColHeight; 
	}
	for(i=1;i<ListView.RowCount;i++){
		ListView.ColLeft[i] = ListView.ColWidth[i-1] + ListView.ColLeft[i-1];
		ListView.ColRight[i] = ListView.ColWidth[i] + ListView.ColLeft[i];
	}
	
	Caption.Border.Color = clGray;
	VScrollBar.Color = clFrmFace;
	VScrollBar.RibbonColor =  clGray;
	
	FixColColor = clMaroon;
	FixColTextColor = clNearWhite;
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	LineCount = 10;
	
	CtlIndex = 0;
	IsAdd = 1;
	AddTargetLevel = 1;
	AddTargetType = 1;
			
	Level0.TopIndex = 0, 
	Level0.SelectedCol = 0; 
	Level0.SelectedIndex = 0;
	Level1.TopIndex = 0, 
	Level1.SelectedCol = 0; 
	Level1.SelectedIndex = 0;
	
	SetListColWidth(ipdWidth0,ipdWidth1,1,1,1,    180,80,80,80,80);
	
	CtlLevel = 0;
	IsSelecting = 1;
}

void TRegisterAddRemove::SetListColWidth(int w0, int w1, int w2, int w3, int w4,int w5,int w6, int w7, int w8, int w9)
{
	unsigned int i;
	ListView.ColWidth[0] = w0;
	ListView.ColWidth[1] = w1;
	ListView.ColWidth[2] = w2;
	ListView.ColWidth[3] = w3;
	ListView.ColWidth[4] = w4;
	ListView.ColWidth[5] = w5;
	ListView.ColWidth[6] = w6;
	ListView.ColWidth[7] = w7;
	ListView.ColWidth[8] = w8;
	ListView.ColWidth[9] = w9;
	
	ListView.ColLeft[0] = Content0.Left;
	ListView.ColRight[0] = ListView.ColLeft[0] + w0 -1;
	for(i=1; i<10; i++){
		ListView.ColLeft[i] = ListView.ColRight[i-1] +1;
		ListView.ColRight[i] = ListView.ColLeft[i] + ListView.ColWidth[i] -1;	
	}
}

void TRegisterAddRemove::RenderBackGround(void)
{
	RectFillRender(Content0.Left, Content0.Top, Content1.Right, Content0.Bottom, Color);
}

void TRegisterAddRemove::RenderBackGround(unsigned int aClr)
{
	RectFillRender(Content0.Left, Content0.Top, Content1.Right, Content0.Bottom, aClr);
}

void TRegisterAddRemove::RenderContent(TRect aRect, unsigned int aClr, unsigned int Index)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	RectFillRender(aRect.Left, aRect.Top, aRect.Right, aRect.Bottom, clFrmFace);
}

void TRegisterAddRemove::RenderContent0(unsigned int aClr)
{
	RenderContent(Content0, clMoneyGreen,0);
}

void TRegisterAddRemove::RenderContent1(unsigned int aClr)
{
	RenderContent(Content1, clNearWhite,1);
}

void TRegisterAddRemove::RenderContent2(unsigned int aClr)
{
	int x,y,x2,y2, aW,aH;
	x = 558;
	y = 160;
	x2 = 799;
	y2 = 400;
	aW = x2 - x +1;
	aH = y2 - y +1;
	VertLineRender(x,			y, 		aH-1,			0xFFFFFFFF);
	VertLineRender(x2 -1,		y+1,		aH -2,		0xFF808080);
	VertLineRender(x2,		y,		aH,			0xFF404040);
	
	HorizLineRender(x, 		y, 		aW - 1, 		0xFFFFFFFF);
	HorizLineRender(x +1, 		y2 -1,  	aW - 2, 		0xFF808080);
	HorizLineRender(x,		y2,		aW,			0xFF404040);
	
	RectFillRender(x+1,y+1,x2-2,y2-2,clGray);	
}

void TRegisterAddRemove::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);
	
	RenderContent0(0xFF000000);
	RenderContent1(0xFF000000);
	RenderContent2(0xFF000000);
}


void TRegisterAddRemove::DrawCaption2(void)
{	
	TMyString::sFromStr("仅删除终端设备", SText.Text);
	SText.Show();
}

void TRegisterAddRemove::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	if(IsAdd){
		TMyString::sFromStr("手动添加消防设备-本机", Caption.Text);
	}else{
		TMyString::sFromStr("手动删除消防设备-本机", Caption.Text);
	}
	Caption.Show();
	DrawCaption2();
	if(IsAdd){
		DrawCtlItemAdd();
	}else{
		DrawCtlItemRemove();
	}
}

void TRegisterAddRemove::DrawVertScrollBar0(void)
{
	TVScrollBar::sDrawBySize(VScrollBarBox.Left, VScrollBarBox.Top,
					VScrollBarBox.Width, VScrollBarBox.Height,
					VScrollBar.RibbonColor, VScrollBar.Color,
					10, 10, Level0.TopIndex);
}

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

//______________________________________________________________________________________________________________________________
void TRegisterAddRemove::Check4SelectedAdd(void)
{
	if(IsSelecting ==0){
		//Addr Mode
		if(Level0.SelectedIndex == 0){
			//Edit 0 key
			aPanel->Edit[0].Selected = 0;
			aPanel->Edit[1].Selected = 0;
			aPanel->Edit[2].Selected = 0;
			aPanel->Edit[3].Selected = 0;
			aPanel->Btn[0].Selected = 0;
			aPanel->Btn[1].Selected = 0;
		}else
		if(Level0.SelectedIndex == 1){
			//Edit 0 key
			aPanel->Edit[0].Selected = 0;
			aPanel->Edit[1].Selected = 0;
			aPanel->Edit[2].Selected = 1;
			aPanel->Edit[3].Selected = 0;
			aPanel->Btn[0].Selected = 0;
			aPanel->Btn[1].Selected = 0;
		}else
		if(Level0.SelectedIndex == 2){
			//Edit 0 key
			aPanel->Edit[0].Selected = 1;
			aPanel->Edit[1].Selected = 0;
			aPanel->Edit[2].Selected = 0;
			aPanel->Edit[3].Selected = 0;
			aPanel->Btn[0].Selected = 0;
			aPanel->Btn[1].Selected = 0;
		}else
		if(Level0.SelectedIndex == 3){
			//Edit 0 key
			aPanel->Edit[0].Selected = 0;
			aPanel->Edit[1].Selected = 1;
			aPanel->Edit[2].Selected = 0;
			aPanel->Edit[3].Selected = 0;
			aPanel->Btn[0].Selected = 0;
			aPanel->Btn[1].Selected = 0;
		}else
		if(Level0.SelectedIndex == 4){
			//Edit 0 key
			aPanel->Edit[0].Selected = 0;
			aPanel->Edit[1].Selected = 0;
			aPanel->Edit[2].Selected = 0;
			aPanel->Edit[3].Selected = 1;
			aPanel->Btn[0].Selected = 0;
			aPanel->Btn[1].Selected = 0;
		}else
		if(Level0.SelectedIndex == 5){
			//Edit 0 key
			aPanel->Edit[0].Selected = 0;
			aPanel->Edit[1].Selected = 0;
			aPanel->Edit[2].Selected = 0;
			aPanel->Edit[3].Selected = 0;
			aPanel->Btn[0].Selected = 1;
			aPanel->Btn[1].Selected = 0;
		}
	}	
}

void TRegisterAddRemove::Check4SelectedRemove(void)
{
	if(IsSelecting ==0){
		if(CtlIndex){
			//User Code Mode
			if(Level0.SelectedIndex == 0){
				//Edit 0 key
				aPanel->Edit[0].Selected = 1;
				aPanel->Btn[0].Selected = 0;
				aPanel->Btn[1].Selected = 0;
			}else
			if(Level0.SelectedIndex == 1){
				aPanel->Edit[0].Selected = 0;
				aPanel->Btn[0].Selected = 1;
				aPanel->Btn[1].Selected = 0;
			}else
			if(Level0.SelectedIndex == 2){
				aPanel->Edit[0].Selected = 0;
				aPanel->Btn[0].Selected = 0;
				aPanel->Btn[1].Selected = 1;
			}
		}else{
			//Addr Mode
			if(Level0.SelectedIndex == 0){
				//Edit 0 key
				aPanel->Edit[0].Selected = 1;
				aPanel->Edit[1].Selected = 0;
				aPanel->Btn[0].Selected = 0;
				aPanel->Btn[1].Selected = 0;
			}else
			if(Level0.SelectedIndex == 1){
				//Edit 0 key
				aPanel->Edit[0].Selected = 0;
				aPanel->Edit[1].Selected = 1;
				aPanel->Btn[0].Selected = 0;
				aPanel->Btn[1].Selected = 0;
			}else
			if(Level0.SelectedIndex == 2){
				//Edit 0 key
				aPanel->Edit[0].Selected = 0;
				aPanel->Edit[1].Selected = 0;
				aPanel->Btn[0].Selected = 1;
				aPanel->Btn[1].Selected = 0;
			}else
			if(Level0.SelectedIndex == 3){
				//Edit 0 key
				aPanel->Edit[0].Selected = 0;
				aPanel->Edit[1].Selected = 0;
				aPanel->Btn[0].Selected = 0;
				aPanel->Btn[1].Selected = 0;
			}
		}
	}	
}




void TRegisterAddRemove::DrawBtn(unsigned int x, unsigned int y, unsigned int x2, unsigned int y2,  unsigned int aClr)
{
	RectFillRender(x, y, x2, y2, aClr);
	
	VertLineRender(x,		y, 		y2-y+1-1,	0xFFFFFFFF);
	VertLineRender(x2 -1,	y +1,		y2-y+1-2,	0xFF808080);
	VertLineRender(x2,	y,		y2-y+1-2,	0xFF404040);
	
	HorizLineRender(x, 	y, 		x2-x+1-1, 	0xFFFFFFFF);
	HorizLineRender(x +1, 	y2 -1,  	x2-x+1 - 2, 0xFF808080);
	HorizLineRender(x,	y2,		x2-x+1,	0xFF404040);
}


void TRegisterAddRemove::DrawCtlItemRemove()
{
	unsigned int x, y, x2, y2, aClr, aClrText;
	
	RenderBackGround(clMoneyGreen);
	
	x =100; y =100; x2 =380; y2 = y+36;	//0
	if(CtlIndex == 0){aClr = clNavy; aClrText =clNearWhite;}
	else{aClr = clFrmFace; aClrText =clNearBlack;}
	DrawBtn(x,y,x2,y2,aClr);
	TextRender_string24(x+10,y+6, aClrText, "根据地址号删除设备");
	
	x =100; y =160; x2 =380; y2 = y+36;	//1
	if(CtlIndex == 1){aClr = clNavy; aClrText =clNearWhite;}
	else{aClr = clFrmFace; aClrText =clNearBlack;}
	DrawBtn(x,y,x2,y2,aClr);
	TextRender_string24(x+10,y+6, aClrText, "根据用户码删除设备");
}

void TRegisterAddRemove::ShowItemDetailRemove()
{
	unsigned int aP0, aP1, Found;
	Found =0;
	RenderContent0(0xFF000000);
	
	Selected.iType = 0;
	Selected.Path0 = 0;
	Selected.Path1 = 0;
	Selected.Path2 = 0;
	Selected.Ava = 0;
	
	TextRender_string24	( 136, 130, clNearBlack, "类型");
	TextRender_string24	( 136, 170, clNearBlack, "回路");
	TextRender_string24	( 136, 210, clNearBlack, "地址");
	TextRender_string24	( 136, 250, clNearBlack, "用户码");
	TextRender_string24	( 6, 296, clNearBlack, "描述:");
	
	if(CtlIndex){
		//User Code Mode
		aP0 = aPanel->Edit[0].Str.ToInteger();
		if(  (aP0 >0) && (aP0 < 1000000)  ){

		}
	}else{
		//Addr Mode
		aP0 = aPanel->Edit[0].Str.ToInteger();
		aP1 = aPanel->Edit[1].Str.ToInteger();
		if(  (aP0 >0) && (aP0 < 41)  ){
			if(  (aP1 >0) && (aP1 < 256)  ){
				aP0--;
				aP1--;
				if(MainCtl.fData.Split.ExistTablePort[aP0] == 0xA5){
					if(Port[aP0].ExistTable[aP1] == 0xA5){
						Found = 1;
	
					}
				}
			}
		}
	}
	
	if(Found){
		TMyString::sFromStr("注册设备已找到", aPanel->Caption.Text);
		Selected.Ava = 1;
		aPanel->Btn[0].Enable = 1;
		aPanel->Btn[1].Enable = 1;
		
		TextRender_string24	( 220, 130, clNearBlack, StrType[EpDev[aP0][aP1].fData.Split.dType]);
		TextDigitRender2Right24	( 284, 170, clNearBlack, aP0 +1);
		TextDigitRender3Right24	( 272, 210, clNearBlack, aP1 +1);
		TextDigitRender6Right24	( 236, 250, clNearBlack, EpDev[aP0][aP1].fData.Split.UserCode);
		aPanel->Edit[3].Str.FromStr("未定义描述字");
		aPanel->Edit[3].Show();
		
	}else{
		TMyString::sFromStr("未找到设备", aPanel->Caption.Text);
		Selected.Ava = 0;
		aPanel->Btn[0].Enable = 0;
		aPanel->Btn[1].Enable = 0;
		
		TextRender_string24	( 220, 130, clFrmFace,  clFrmFace, "           ");
		TextRender_string24	( 284, 170, clFrmFace,  clFrmFace, "           ");
		TextRender_string24	( 272, 210, clFrmFace,  clFrmFace, "           ");
		TextRender_string24	( 236, 250, clFrmFace,  clFrmFace, "           ");
		aPanel->Edit[3].Str.Clear();
		aPanel->Edit[3].Show();
	}
}

void TRegisterAddRemove::InitPanel4Remove(void)
{
	int x,y,x2,y2, aW,aH;
	x = 420;
	y = 140;
	x2 = 680;
	y2 = 325;
	aW = x2 - x +1;
	aH = y2 - y +1;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("未选定设备", aPanel->Caption.Text);
	
	aPanel->SText[0].Init(16,46,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(16,82,60,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("回路", aPanel->SText[0].Text);
	TMyString::sFromStr("地址", aPanel->SText[1].Text);
	
	aPanel->Edit[0].Init(120,46,60,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(120,82,60,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[1].SetMaxLen(3);
	aPanel->Edit[0].SetInputMaskEn();
	aPanel->Edit[1].SetInputMaskEn();
	aPanel->Edit[0].Str.FromStr("01");
	aPanel->Edit[1].Str.FromStr("001");
	aPanel->Edit[0].Pst = 1;
	aPanel->Edit[1].Pst = 2;
	
	aPanel->Btn[0].Init(68,130,120,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("确认删除");
	aPanel->Btn[1].Init(68,180,120,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Caption.FromStr("取消");
	
	aPanel->Caption.Visible = 1;
	aPanel->SText[0].Visible = 1;
	aPanel->SText[1].Visible = 1;
	aPanel->Edit[0].Visible = 1;
	aPanel->Edit[1].Visible = 1;
	aPanel->Btn[0].Visible = 1;
	aPanel->Btn[1].Visible = 0;
	aPanel->Btn[0].Enable = 1;
	aPanel->Btn[1].Enable = 1;
	aPanel->Btn[0].SetCaptionLeftSpacing(8);
	aPanel->Btn[1].SetCaptionLeftSpacing(28);
	
	aPanel->Btn[0].SelectedColor = clRed;
	aPanel->Btn[0].SelectedTextColor = clNearWhite;
	
	aPanel->Edit[3].Init(6,330,394,32,0,0,clNearBlack,clNearWhite);
	aPanel->Edit[3].SetMaxLen(32);
	aPanel->Edit[3].Str.FromStr("未定义描述字");
	aPanel->Edit[3].Pst = 13;
	aPanel->Edit[3].Visible = 1;
	
	ShowItemDetailRemove();
	aPanel->Show();
	
	TextRender_string24	( 136, 130, clNearBlack, "类型");
	TextRender_string24	( 136, 170, clNearBlack, "回路");
	TextRender_string24	( 136, 210, clNearBlack, "地址");
	TextRender_string24	( 136, 250, clNearBlack, "用户码");
	TextRender_string24	( 6, 296, clNearBlack, "描述:");
}


void TRegisterAddRemove::InitPanel4RemoveViaUserCode(void)
{
	int x,y,x2,y2, aW,aH;
	x = 420;
	y = 140;
	x2 = 680;
	y2 = 325;
	aW = x2 - x +1;
	aH = y2 - y +1;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("未选定设备", aPanel->Caption.Text);
	
	aPanel->SText[0].Init(16,46,60,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("用户码", aPanel->SText[0].Text);
	
	aPanel->Edit[0].Init(120,46,120,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[0].SetMaxLen(6);
	aPanel->Edit[0].SetInputMaskEn();
	aPanel->Edit[0].Str.FromStr("000001");
	aPanel->Edit[0].Pst = 5;
	
	aPanel->Btn[0].Init(68,130,120,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("确认删除");
	aPanel->Btn[1].Init(68,180,120,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Caption.FromStr("取消");	
	
	aPanel->Caption.Visible = 1;
	aPanel->SText[0].Visible = 1;
	aPanel->Edit[0].Visible = 1;
	aPanel->Btn[0].Visible = 1;
	aPanel->Btn[1].Visible = 0;
	aPanel->Btn[0].Enable = 1;
	aPanel->Btn[1].Enable = 0;
	aPanel->Btn[0].SetCaptionLeftSpacing(8);
	aPanel->Btn[1].SetCaptionLeftSpacing(28);
	
	
	aPanel->Btn[0].SelectedColor = clRed;
	aPanel->Btn[0].SelectedTextColor = clNearWhite;
	
	ShowItemDetailRemove();
	aPanel->Show();
	
}

void TRegisterAddRemove::DrawCtlItemAdd()
{
	unsigned int x, y, x2, y2, aClr, aClrText;
	
	TMyString::sFromStr("手动添加消防设备-本机", Caption.Text);
	Caption.Show();
	
	RenderBackGround(clMoneyGreen);
	
	x =100; y =100; x2 =380; y2 = y+36;	//0
	if(CtlIndex == 0){aClr = clNavy; aClrText =clNearWhite;}
	else{aClr = clFrmFace; aClrText =clNearBlack;}
	DrawBtn(x,y,x2,y2,aClr);
	TextRender_string24(x+10,y+6, aClrText, "退出手动添加设备");
	
	x =100; y =160; x2 =380; y2 = y+36;	//1
	if(CtlIndex == 1){aClr = clNavy; aClrText =clNearWhite;}
	else{aClr = clFrmFace; aClrText =clNearBlack;}
	DrawBtn(x,y,x2,y2,aClr);
	TextRender_string24(x+10,y+6, aClrText, "开始手动添加消防设备");
}

void TRegisterAddRemove::InitPanel4Add(void)
{
	int x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = 420;
	y = 100;
	x2 = 680;
	y2 = 370;
	aW = x2 - x +1;
	aH = y2 - y +1;
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clNearBlack,clFrmFace);
		
	if(AddTargetLevel == 0){
		TMyString::sFromStr("添加根设备", aPanel->Caption.Text);
	}else
	if(AddTargetLevel == 1){
		TMyString::sFromStr("添加终端设备", aPanel->Caption.Text);
	}
	
	aPanel->SText[0].Init(16,128,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(16,168,60,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("回路", aPanel->SText[0].Text);
	TMyString::sFromStr("地址", aPanel->SText[1].Text);
	
	aPanel->Edit[0].Init(120,128,60,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(120,168,60,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[1].SetMaxLen(3);
	aPanel->Edit[0].SetInputMaskEn();
	aPanel->Edit[1].SetInputMaskEn();
	aPanel->Edit[0].Str.FromStr("0");
	aPanel->Edit[1].Str.FromStr("00");
	aPanel->Edit[0].Pst = 0;
	aPanel->Edit[1].Pst = 1;
	
	aPanel->Btn[0].Init(68,218,120,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("确认添加");
	aPanel->Btn[1].Init(68,258,120,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Caption.FromStr("取消");
	
	aPanel->Caption.Visible = 1;
	aPanel->SText[0].Visible = 1;
	aPanel->SText[1].Visible = 1;
	aPanel->Edit[0].Visible = 1;
	aPanel->Edit[1].Visible = 1;
	aPanel->Btn[0].Visible = 1;
	aPanel->Btn[1].Visible = 0;
	aPanel->Btn[0].Enable = 1;
	aPanel->Btn[1].Enable = 0;
	aPanel->Btn[0].SetCaptionLeftSpacing(8);
	aPanel->Btn[1].SetCaptionLeftSpacing(8);
	
	aPanel->Edit[2].Init(120,88,60,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[2].SetMaxLen(2);
	aPanel->Edit[2].SetInputMaskEn();
	aPanel->Edit[2].Str.FromStr("00");
	aPanel->Edit[2].Pst = 1;
	aPanel->Edit[2].Visible = 1;
	
	aPanel->Edit[3].Init(6,330,394,32,0,0,clNearBlack,clNearWhite);
	aPanel->Edit[3].SetMaxLen(32);
	aPanel->Edit[3].Str.FromStr("在此输入描述字");
	aPanel->Edit[3].Pst = 13;
	aPanel->Edit[3].Visible = 1;
	
	
	aPanel->Btn[0].SelectedColor = clRed;
	aPanel->Btn[0].SelectedTextColor = clNearWhite;
	
	ShowItemDetailAdd();
	aPanel->Show();
	
	TextRender_string24(436, 148, clNearBlack, clFrmFace, "设备层");
	TextRender_string24(436, 188, clNearBlack, clFrmFace, "类型号");
	if(Level0.SelectedIndex == 0){
		aClr = clNearWhite;
		bClr = clBlue;
	}else{
		aClr = clNearBlack;
		bClr = clFrmFace;
	}
	if(AddTargetLevel == 0){
		TextRender_string24(540, 154, aClr, bClr, "<板卡>");
	}else
	if(AddTargetLevel == 1){
		TextRender_string24(540, 154, aClr, bClr, "<终端>");
	}
}


void TRegisterAddRemove::ShowItemDetailAdd()
{
	unsigned int aP0, aP1, Found;
	unsigned int aClr, bClr, aType;
	Found =0;
	RenderContent0(0xFF000000);
	
	Selected.iType = 0;
	Selected.Path0 = 0;
	Selected.Path1 = 0;
	Selected.Path2 = 0;
	Selected.Ava = 0;
	
	if(1){
		//Addr Mode
		aP0 = aPanel->Edit[0].Str.ToInteger();
		aP1 = aPanel->Edit[1].Str.ToInteger();
		aType = aPanel->Edit[2].Str.ToInteger();
	}
	
	if(Level0.SelectedIndex == 0){
		aClr = clNearWhite;
		bClr = clBlue;
	}else{
		aClr = clNearBlack;
		bClr = clFrmFace;
	}	
	
	if(aType < 16){
		AddTargetType = aType;
	}else{
		AddTargetType = 0;
	}
	if(AddTargetLevel == 0){
		TMyString::sFromStr("添加根设备", aPanel->Caption.Text);
		TextRender_string24(540, 154, aClr, bClr, "<板卡>");
		
		if(AddTargetType < 16){
			TextRender_string24	( 136, 130, clNearBlack, "类型");
			TextRender_string24	( 230, 130, clNearBlack, StrRootType[AddTargetType]);
			
			TextRender_string24	( 136, 170, clNearBlack, "序号");
			TextDigitRender2Right24	( 284, 170, clNearBlack, aP0 +1);
			
			TextRender_string24	( 136, 210, clNearBlack, "根路径");
			TextDigitRender3Right24	( 272, 210, clNearBlack, aP1 +1);
			
			TextRender_string24	( 136, 290, clNearBlack, "描述:");
			TextRender_string24	( 220, 290, clNearBlack, "请在以下定义");
		}
	}else
	if(AddTargetLevel == 1){
		TMyString::sFromStr("添加终端设备", aPanel->Caption.Text);
		TextRender_string24(540, 154, aClr, bClr, "<终端>");
		
		TextRender_string24	( 136, 130, clNearBlack, "类型");
		TextRender_string24	( 230, 130, clNearBlack, StrType[AddTargetType]);
		
		TextRender_string24	( 136, 170, clNearBlack, "回路");
		TextDigitRender2Right24	( 284, 170, clNearBlack, aP0 +1);
		
		TextRender_string24	( 136, 210, clNearBlack, "地址");
		TextDigitRender3Right24	( 272, 210, clNearBlack, aP1 +1);
		
		TextRender_string24	( 136, 290, clNearBlack, "描述:");
		TextRender_string24	( 220, 290, clNearBlack, "请在以下定义");
	}
	
	aPanel->Btn[0].Enable = 1;
}


//_____________________________________________________________________________________________________________________________


TGuiMsgReturn TRegisterAddRemove::KeyInAddStart(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	TDMix aFullPath;
	unsigned char aType;
	Updata = 0;
	
	if(aKey == VK_UP){

	}else
	if(aKey == VK_DOWN){

	}
	if(aKey == VK_EXECUTE){
		//Save Here
		IsSelecting = 0;
		//Addr Mode
		InitPanel4Add();
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	if(Updata){
		ShowItemDetailAdd();
	}
	
	return aMsg;	
}


TGuiMsgReturn TRegisterAddRemove::KeyInRemoveStart(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	TDMix aFullPath;
	unsigned char aType;
	Updata = 0;
	
	if(aKey == VK_LEFT){

	}else
	if(aKey == VK_RIGHT){
		
	}else
	if(aKey == VK_UP){

	}else
	if(aKey == VK_DOWN){

	}else
	if(aKey == VK_F1){

	}else
	if(aKey == VK_TAB){

	}
	if(aKey == VK_EXECUTE){
		IsSelecting = 0;
		InitPanel4Remove();
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	if(Updata){
		ShowItemDetailAdd();
	}
	
	return aMsg;	
}



TGuiMsgReturn TRegisterAddRemove::KeyInAdd(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	TDMix aFullPath;
	unsigned char aType;
	Updata = 0;
	
	Check4SelectedAdd();
	
	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)
					){
		if(1){
			//Addr Mode
			if(aPanel->Edit[0].Selected){
				aPanel->Edit[0].KeyIn(aKey);
				Updata = 1;
			}
			if(aPanel->Edit[1].Selected){
				aPanel->Edit[1].KeyIn(aKey);
				Updata = 1;
			}
			if(aPanel->Edit[2].Selected){
				aPanel->Edit[2].KeyIn(aKey);
				Updata = 1;
			}
			if(aPanel->Edit[3].Selected){
				aPanel->Edit[3].KeyIn(aKey);
				Updata = 1;
			}
		}		
	}
	
	if(aKey == VK_LEFT){
		if(IsSelecting){

		}else{
			if(1){
				//Addr Mode
				if(Level0.SelectedIndex == 0){
					if(AddTargetLevel){
						AddTargetLevel = 0;
					}else{
						AddTargetLevel = 1;
					}
					Updata = 1;
				}
				if(aPanel->Edit[0].Selected){
					aPanel->Edit[0].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[1].Selected){
					aPanel->Edit[1].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[2].Selected){
					aPanel->Edit[2].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[3].Selected){
				aPanel->Edit[3].KeyIn(aKey);
				Updata = 1;
			}
			}
		}
	}else
	if(aKey == VK_RIGHT){
		if(IsSelecting){
			
		}else{
			if(1){
				//Addr Mode
				if(Level0.SelectedIndex == 0){
					if(AddTargetLevel){
						AddTargetLevel = 0;
					}else{
						AddTargetLevel = 1;
					}
					Updata = 1;
				}
				if(aPanel->Edit[0].Selected){
					aPanel->Edit[0].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[1].Selected){
					aPanel->Edit[1].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[2].Selected){
					aPanel->Edit[2].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[3].Selected){
					aPanel->Edit[3].KeyIn(aKey);
					Updata = 1;
				}
			}
		}			
	}else
	if(aKey == VK_UP){
		if(IsSelecting){
			if(CtlIndex >0){
				CtlIndex =0;
				DrawCtlItemAdd();
			}				
		}else{
			if(1){
				//Addr Mode
				if(Level0.SelectedIndex >0){
					Level0.SelectedIndex--;
					Updata = 1;
				}
			}
		}
	}else
	if(aKey == VK_DOWN){
		if(IsSelecting){
			if(CtlIndex ==0 ){
				CtlIndex =1;
				DrawCtlItemAdd();
			}
		}else{
			if(1){
				//Addr Mode
				if(Level0.SelectedIndex <5){
					Level0.SelectedIndex++;
					Updata = 1;
				}
			}
		}
	}else
	if(aKey == VK_F1){
		//Go Modify
		if(IsSelecting){

		}
	}else
	if(aKey == VK_TAB){
		if(IsSelecting){
			//User Not Save Here
			//if(1){
				//Addr Mode
			//	Level0.SelectedIndex ++;
			//	if(Level0.SelectedIndex >3){
			//		Level0.SelectedIndex = 0;
			//		Updata = 1;
			//	}
			//}
		}
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(IsSelecting){
			//Save Here
			IsSelecting = 0;
			//Addr Mode
			InitPanel4Add();
		}else{
			if(1){
				//Addr Mode
				if(Level0.SelectedIndex == 4){
					//Mask Here
					aFullPath.D8[0] = 0;
					aFullPath.D8[1] = Selected.Path0;
					aFullPath.D8[2] = Selected.Path1;
					aFullPath.D8[3] = Selected.Path2;
					Record.OperateAdd_Mask(aFullPath, Selected.iType);
				}
			}
		}
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	Check4SelectedAdd();
	if(Updata){
		ShowItemDetailAdd();
		aPanel->ReDrawItems();
	}
	
	return aMsg;	
}



TGuiMsgReturn TRegisterAddRemove::KeyInRemove(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	TDMix aFullPath;
	unsigned char aType;
	Updata = 0;
	
	Check4SelectedRemove();
	
	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)
					){
		if(CtlIndex){
			//User Code Mode
			if(aPanel->Edit[0].Selected){
				aPanel->Edit[0].KeyIn(aKey);
				Updata = 1;
			}
		}else{
			//Addr Mode
			if(aPanel->Edit[0].Selected){
				aPanel->Edit[0].KeyIn(aKey);
				Updata = 1;
			}
			if(aPanel->Edit[1].Selected){
				aPanel->Edit[1].KeyIn(aKey);
				Updata = 1;
			}
		}		
	}
	
	if(aKey == VK_LEFT){
		if(IsSelecting){

		}else{
			if(CtlIndex){
				//User Code Mode
				if(aPanel->Edit[0].Selected){
					aPanel->Edit[0].KeyIn(aKey);
					Updata = 1;
				}
			}else{
				//Addr Mode
				if(aPanel->Edit[0].Selected){
					aPanel->Edit[0].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[1].Selected){
					aPanel->Edit[1].KeyIn(aKey);
					Updata = 1;
				}
			}
		}
	}else
	if(aKey == VK_RIGHT){
		if(IsSelecting){
			
		}else{
			if(CtlIndex){
				//User Code Mode
				if(aPanel->Edit[0].Selected){
					aPanel->Edit[0].KeyIn(aKey);
					Updata = 1;
				}
			}else{
				//Addr Mode
				if(aPanel->Edit[0].Selected){
					aPanel->Edit[0].KeyIn(aKey);
					Updata = 1;
				}
				if(aPanel->Edit[1].Selected){
					aPanel->Edit[1].KeyIn(aKey);
					Updata = 1;
				}
			}
		}			
	}else
	if(aKey == VK_UP){
		if(IsSelecting){
			if(CtlIndex >0){
				CtlIndex =0;
				DrawCtlItemRemove();
			}				
		}else{
			if(CtlIndex){
				//User Code Mode
				if(Level0.SelectedIndex >0){
					Level0.SelectedIndex--;
					Updata = 1;
				}
			}else{
				//Addr Mode
				if(Level0.SelectedIndex >0){
					Level0.SelectedIndex--;
					Updata = 1;
				}
			}
		}
	}else
	if(aKey == VK_DOWN){
		if(IsSelecting){
			if(CtlIndex ==0 ){
				CtlIndex =1;
				DrawCtlItemRemove();
			}
		}else{
			if(CtlIndex){
				//User Code Mode
				if(Level0.SelectedIndex <2){
					if(Selected.Ava){
						Level0.SelectedIndex++;
						Updata = 1;
					}
				}
			}else{
				//Addr Mode
				if(Level0.SelectedIndex <1){
					Level0.SelectedIndex++;
					Updata = 1;
				}else
				if(Level0.SelectedIndex <2){
					if(Selected.Ava){
						Level0.SelectedIndex++;
						Updata = 1;
					}
				}
			}
		}
	}else
	if(aKey == VK_F1){
		//Go Modify
		if(IsSelecting){

		}
	}else
	if(aKey == VK_TAB){
		if(IsSelecting){
			//User Not Save Here
			if(CtlIndex){
				//User Code Mode
				Level0.SelectedIndex ++;
				if(Level0.SelectedIndex >2){
					Level0.SelectedIndex = 0;
					Updata = 1;
				}
			}else{
				//Addr Mode
				Level0.SelectedIndex ++;
				if(Level0.SelectedIndex >3){
					Level0.SelectedIndex = 0;
					Updata = 1;
				}
			}
		}
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(IsSelecting){
			//Save Here
			IsSelecting = 0;
			if(CtlIndex){
				//User Code Mode
				InitPanel4RemoveViaUserCode();
			}else{
				//Addr Mode
				InitPanel4Remove();
			}
		}else{
			if(CtlIndex){
				//User Code Mode
				if(Level0.SelectedIndex == 1){
					//Mask Here
					aFullPath.D8[0] = 0;
					aFullPath.D8[1] = Selected.Path0;
					aFullPath.D8[2] = Selected.Path1;
					aFullPath.D8[3] = Selected.Path2;
					Record.OperateAdd_Mask(aFullPath, Selected.iType);
				}else
				if(Level0.SelectedIndex == 2){
					//UnMask Here
					aFullPath.D8[0] = 0;
					aFullPath.D8[1] = Selected.Path0;
					aFullPath.D8[2] = Selected.Path1;
					aFullPath.D8[3] = Selected.Path2;
					Record.OperateAdd_UnMask(aFullPath, Selected.iType);
				}
			}else{
				//Addr Mode
				if(Level0.SelectedIndex == 2){
					//Mask Here
					aFullPath.D8[0] = 0;
					aFullPath.D8[1] = Selected.Path0;
					aFullPath.D8[2] = Selected.Path1;
					aFullPath.D8[3] = Selected.Path2;
					Record.OperateAdd_Mask(aFullPath, Selected.iType);
				}else
				if(Level0.SelectedIndex == 3){
					//UnMask Here
					aFullPath.D8[0] = 0;
					aFullPath.D8[1] = Selected.Path0;
					aFullPath.D8[2] = Selected.Path1;
					aFullPath.D8[3] = Selected.Path2;
					Record.OperateAdd_UnMask(aFullPath, Selected.iType);
				}
			}
		}
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	Check4SelectedRemove();
	if(Updata){
		ShowItemDetailRemove();
		aPanel->ReDrawItems();
	}
	
	return aMsg;	
}


TGuiMsgReturn TRegisterAddRemove::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	TDMix aFullPath;
	unsigned char aType;
	Updata = 0;
	
	unsigned char IsAdd;
		unsigned char IsSelecting;
	
	if(IsAdd){
		if(IsSelecting){
			
		}else{
			
		}
	}else{
		if(IsSelecting){
			
		}else{
			
		}
	}	
	return aMsg;	
}




