#include "RuntimeData.h"
#include "UnRegister.h"

void TUnRegister::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	(429,		Top,		370,   CH, 0, 0, clWhite, clMaroon);
	
	Content.Set(Left, 	Top+CH, 	Right, 	Bottom);
	
	Color = aColor;
	#undef CH

	Caption.Border.Color = clGray;

	CtlIndex = 0;
	ListIndex = 0;
	IsViewList = 0;	
	IsAskingRemoveWhat = 1;
}

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

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

void TUnRegister::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);
	
	RenderBackGround();
}


void TUnRegister::DrawCaption2(void)
{	
	TMyString::sFromStr("TAB键在编辑与类型列表之间切换", SText.Text);
	SText.Show();
}

void TUnRegister::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	TMyString::sFromStr("删除消防设备-本机", Caption.Text);
	Caption.Show();
}

void TUnRegister::ShowCaption()
{
	TMyString::sFromStr(" zzzzzzz",  Caption.Text);
	Caption.Show();
}

//______________________________________________________________________________________________________________________________
void TUnRegister::Check4Selected4Part(void)
{
	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;
	if(IsViewList == 0){
		if(CtlIndex == 0){
			aPanel->Edit[0].Selected = 1;
		}else
		if(CtlIndex == 1){
			aPanel->Edit[1].Selected = 1;
		}else
		if(CtlIndex == 2){
			aPanel->Edit[2].Selected = 1;
		}else
		if(CtlIndex == 3){
			aPanel->Edit[3].Selected = 1;
		}else
		if(CtlIndex == 4){
			aPanel->Btn[0].Selected = 1;
		}
	}
}

void TUnRegister::InitPanel(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(200,40,400,200,x,y,clFrmFace, bvRaised);
	aPanel->Caption.Init(200+1,40+1,400-3,32,x,y,clNearWhite,clDeepBlue);
	TMyString::sFromStr("选择清除方法", aPanel->Caption.Text);
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;

	
	aPanel->Btn[0].Init(80,40,240,36,x,y,1,clFrmFace);
	aPanel->Btn[1].Init(80,90,240,36,x,y,1,clFrmFace);
	aPanel->Btn[0].Caption.FromStr(" 解除全部注册信息");
	aPanel->Btn[1].Caption.FromStr("  根据地址段清除");
	aPanel->Btn[0].SetCaptionLeftSpacing(6);
	aPanel->Btn[0].Enable =1;
	aPanel->Btn[0].Visible =1;
	aPanel->Btn[1].SetCaptionLeftSpacing(6);
	aPanel->Btn[1].Enable =1;
	aPanel->Btn[1].Visible =1;
	
	CtlIndex = 1;
	aPanel->Btn[0].Selected =0;
	aPanel->Btn[1].Selected =1;

	aPanel->Show();
	aPanel->ReDrawItems();
}

void TUnRegister::InitPanel4All(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(200,40,400,200,x,y,clFrmFace, bvRaised);
	aPanel->Caption.Init(201,41,400-3,32,x,y,clNearWhite,clDeepBlue);
	TMyString::sFromStr("准备清除全部信息", aPanel->Caption.Text);
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->SText[0].Init(146,6,100,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("        ", aPanel->SText[0].Text);
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->Btn[0].Init(80,40,240,34,x,y,1,clFrmFace);
	aPanel->Btn[1].Init(80,90,240,34,x,y,1,clFrmFace);
	aPanel->Btn[0].Caption.FromStr(" 确定执行清除全部");
	aPanel->Btn[1].Caption.FromStr("     取消清除");
	aPanel->Btn[0].SetCaptionLeftSpacing(6);
	aPanel->Btn[0].Enable =1;
	aPanel->Btn[0].Visible =1;
	aPanel->Btn[0].SelectedColor = clRed;
	aPanel->Btn[0].SelectedTextColor = clNearWhite;
	aPanel->Btn[1].SetCaptionLeftSpacing(6);
	aPanel->Btn[1].Enable =1;
	aPanel->Btn[1].Visible =1;
	
	CtlIndex = 1;
	aPanel->Btn[0].Selected =0;
	aPanel->Btn[1].Selected =1;

	aPanel->Show();
	aPanel->ReDrawItems();
}

void TUnRegister::InitPanel4Part(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	TextRender_string24(x+86,y + 93,clBlack,"设置键减1,查询键加1");
	TextRender_string24(x+400,y + 93,clRed,"按确认键直接解除");
	if(aPanel == nullptr)return;
	
	aPanel->Init(4,6,784,82,x,y,clFrmFace, bvRaised);
	aPanel->Caption.Init(4+1,6+1,781,32,x,y,clFrmFace,clFrmFace);
	
	x = aPanel->Bound.Left;
	y = aPanel->Bound.Top;
	aPanel->SText[0].Init(0,4,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(107,4,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(308,4,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(0, 47,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(231,4,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(608, 47,102,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("回路", aPanel->SText[0].Text);
	TMyString::sFromStr("地址段", aPanel->SText[1].Text);
	TMyString::sFromStr("类型", aPanel->SText[2].Text);
	TMyString::sFromStr("地址段1~250时解除整回路,地址段始末相等则解除单地址", aPanel->SText[3].Text);
	TMyString::sClear(aPanel->SText[5].Text);
	TMyString::sFromStr("-", aPanel->SText[4].Text);
	aPanel->SText[3].TextColor = clDeepGray;
	
	aPanel->Edit[0].Init(52,6,40,30,x,y,1,clNearWhite);
	aPanel->Edit[1].Init(184,6,50,30,x,y,1,clNearWhite);
	aPanel->Edit[2].Init(248,6,50,30,x,y,1,clNearWhite);
	aPanel->Edit[3].Init(360,6,50,30,x,y,1,clNearWhite);
	aPanel->Edit[4].Init(420,6,270,30,x,y,1,clNearWhite);
	
	for(i=0; i<5; i++){
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].Enable = 1;
	}
	
	aPanel->Edit[0].Str.FromStr("01");
	aPanel->Edit[0].SetInputMaskEn();
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[0].Pst = 1;
	aPanel->Edit[1].Str.FromStr("001");
	aPanel->Edit[1].SetMaxLen(3);
	aPanel->Edit[1].Pst = 2;
	aPanel->Edit[2].Str.FromStr("001");
	aPanel->Edit[2].SetInputMaskEn();
	aPanel->Edit[2].SetMaxLen(3);
	aPanel->Edit[2].Pst = 2;
	aPanel->Edit[3].Str.FromStr("002");
	aPanel->Edit[3].SetInputMaskEn();
	aPanel->Edit[3].SetMaxLen(3);
	aPanel->Edit[3].Pst = 2;
	aPanel->Edit[4].Str.FromStr("未定义");
	aPanel->Edit[4].SetInputMaskEn();
	aPanel->Edit[4].SetMaxLen(3);
	aPanel->Edit[4].Pst = 2;
	aPanel->Edit[4].Enable = 0;
	
	aPanel->Btn[0].Init(700,4,68,34,x,y,1,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("解除");
	aPanel->Btn[0].SetCaptionLeftSpacing(6);
	aPanel->Btn[0].Enable =1;
	aPanel->Btn[0].Visible =1;
	aPanel->Btn[0].SelectedColor = clRed;
	aPanel->Btn[0].SelectedTextColor = clNearWhite;

	DrawCaption2();

	aPanel->Show();
	Check4Selected4Part();
	aPanel->ReDrawItems();
	
	LoadListStr();
	ShowList();
	ShowType();
}

//_____________________________________________________________________________________________________________________________

void TUnRegister::ShowList()
{
	int i, x, y;
	unsigned int aClr, aAddr, Index, wc;
	if(IsViewList){
		aClr = clBlue;
	}else{
		aClr = clMedGray;
	}
	RectFillRender(6,192,10,430,aClr);
	RectFillRender(6,192,792,196,aClr);
	RectFillRender(6,430,792,434,aClr);
	RectFillRender(788,192,792,434,aClr);
	
	RectFillRender(11,197,787,429,clMoneyGreen);
	
	i = ListIndex * 14;
	wc =0;
	for(; i<EntryCount; i++){
		x = (wc % 2) * 380 + 16;
		y = wc / 2 * 32 + 203;
		aAddr = dAddrSdRam_Register + (i * 4);
		Index = *(volatile unsigned int *)(aAddr);
		if(Index < 256){
			TextDigitRender3Right24(x,y,clBlack, Index);
			TextRender_string24(x+36,y,clBlack,".");
			TextRender_string24(x+48,y,clBlack,StrType[Index]);
		}
		wc++;
		if(wc >= 14)break;
	}
	i = ListIndex * 14;
	TVScrollBar::sDrawByPoint(767, 197, 786, 428, clBlue, clGray, EntryCount, 14, i);
}

void TUnRegister::ShowType()
{
	unsigned char aType;
	int i;
	i = aPanel->Edit[3].Str.ToInteger();
	if(i>255){
		aPanel->Edit[4].Str.FromStr("类型选择超出范围");
		aPanel->Edit[4].TextColor = clRed;
	}else
	if(i == 0){
		aPanel->Edit[4].Str.FromStr("全部类型");
		aPanel->Edit[4].TextColor = clRed;
	}else{
		aType = static_cast<unsigned char>(i);
		aPanel->Edit[4].Str.FromStr(StrType[aType]);
		aPanel->Edit[4].TextColor = clNearBlack;
	}
	aPanel->Edit[4].Show();
}

void TUnRegister::ClearClearAllText()
{
	TMyString::sFromStr("        ", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TUnRegister::ShowClearAllText()
{
	TMyString::sFromStr("执行完成", aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TUnRegister::ClearOutText()
{
	TMyString::sClear(aPanel->SText[5].Text);
	aPanel->SText[5].Show();
}

void TUnRegister::ShowInputErr()
{
	TMyString::sFromStr("      输入错误", aPanel->SText[5].Text);
	aPanel->SText[5].TextColor = clRed;
	aPanel->SText[5].Show();
}

void TUnRegister::ShowExResult(int What, int aRst)
{
	if(What ==0){
		if(aRst){
			TMyString::sFromStr("  回路解除成功", aPanel->SText[5].Text);
			aPanel->SText[5].TextColor = clNearBlack;
		}else{
			
			TMyString::sFromStr("  回路解除失败", aPanel->SText[5].Text);
			aPanel->SText[5].TextColor = clNearBlack;
		}		
	}else
	if(What == 1){
		if(aRst){
			TMyString::sFromStr("地址段解除成功", aPanel->SText[5].Text);
			aPanel->SText[5].TextColor = clNearBlack;
		}else{
			
			TMyString::sFromStr("地址段解除失败", aPanel->SText[5].Text);
			aPanel->SText[5].TextColor = clNearBlack;
		}
	}else
	if(What == 2){
		if(aRst){
			TMyString::sFromStr("单地址解除成功", aPanel->SText[5].Text);
			aPanel->SText[5].TextColor = clNearBlack;
		}else{
			
			TMyString::sFromStr("单地址解除失败", aPanel->SText[5].Text);
			aPanel->SText[5].TextColor = clNearBlack;
		}
	}

	aPanel->SText[5].Show();
}

void TUnRegister::TryIncValue()
{
	int i;
	if(IsViewList)return;
	if(CtlIndex == 0){
		i = aPanel->Edit[0].Str.ToInteger();
		if(i<255){
			i++;
			aPanel->Edit[0].Str.FromUInt2Dg(i);
			aPanel->Edit[0].Show();
		}
	}else
	if(CtlIndex == 1){
		i = aPanel->Edit[1].Str.ToInteger();
		if(i<255){
			i++;
			aPanel->Edit[1].Str.FromUInt3Dg(i);
			aPanel->Edit[1].Show();
		}
	}else
	if(CtlIndex == 2){
		i = aPanel->Edit[2].Str.ToInteger();
		if(i<255){
			i++;
			aPanel->Edit[2].Str.FromUInt3Dg(i);
			aPanel->Edit[2].Show();
		}
	}else
	if(CtlIndex == 3){
		i = aPanel->Edit[3].Str.ToInteger();
		if(i<255){
			i++;
			aPanel->Edit[3].Str.FromUInt3Dg(i);
			aPanel->Edit[3].Show();
			ShowType();
		}
	}	
}


void TUnRegister::TryDecValue()
{
	int i;
	if(IsViewList)return;
	if(CtlIndex == 0){
		i = aPanel->Edit[0].Str.ToInteger();
		if(i>0){
			i--;
			aPanel->Edit[0].Str.FromUInt2Dg(i);
			aPanel->Edit[0].Show();
		}
	}else
	if(CtlIndex == 1){
		i = aPanel->Edit[1].Str.ToInteger();
		if(i>0){
			i--;
			aPanel->Edit[1].Str.FromUInt3Dg(i);
			aPanel->Edit[1].Show();
		}
	}else
	if(CtlIndex == 2){
		i = aPanel->Edit[2].Str.ToInteger();
		if(i>0){
			i--;
			aPanel->Edit[2].Str.FromUInt3Dg(i);
			aPanel->Edit[2].Show();
		}
	}else
	if(CtlIndex == 3){
		i = aPanel->Edit[3].Str.ToInteger();
		if(i>0){
			i--;
			aPanel->Edit[3].Str.FromUInt3Dg(i);
			aPanel->Edit[3].Show();
			ShowType();
		}
	}	
}

void TUnRegister::LoadListStr()
{
	int i;
	unsigned int aAddr;
	char cText[4] = "未";
	EntryCount = 0;
	for(i=0; i<256; i++){
		aAddr = dAddrSdRam_Register + (i * 4);
		*(volatile unsigned int *)(aAddr) = 0;

	}
	for(i=0; i<256; i++){
		if(StrType[i][0] != cText[0]){
			aAddr = dAddrSdRam_Register + (EntryCount * 4);
			*(volatile unsigned int *)(aAddr) = i;
			EntryCount++;
		}else{
			continue;
		}
	}
	PageCount = EntryCount / 14;
	if(EntryCount % 14){
		PageCount++;
	}
}



TGuiMsgReturn TUnRegister::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	TDMix aFullPath;
	if(IsAskingRemoveWhat){
		switch(aKey){
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					aPanel->Btn[0].Selected = 1;
					aPanel->Btn[1].Selected = 0;
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <1){
					CtlIndex++;
					aPanel->Btn[0].Selected = 0;
					aPanel->Btn[1].Selected = 1;
					aPanel->ReDrawItems();
				}
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					IsAskingRemoveWhat = 0;
					IsRemoveAll = 1;
					CtlIndex = 0;
					InitPanel4All();
				}else{
					IsAskingRemoveWhat = 0;
					IsRemoveAll = 0;
					CtlIndex = 0;
					RenderBackGround();
					InitPanel4Part();
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
		}
		return aMsg;
	}

	if(IsRemoveAll){
		switch(aKey){
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					aPanel->Btn[0].Selected = 1;
					aPanel->Btn[1].Selected = 0;
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <1){
					CtlIndex++;
					aPanel->Btn[0].Selected = 0;
					aPanel->Btn[1].Selected = 1;
					aPanel->ReDrawItems();
				}
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					//Start to Clear All
					ClearClearAllText();
					
					SendCmdClearAll();
					
					ShowClearAllText();
				}else{
					//Cancel and Return
					aMsg = guiMsgReturn;
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
			
		}
		return aMsg;		
	}else{
		if(IsViewList == 0){
			switch(aKey){
				case VK_0:
				case VK_1:
				case VK_2:
				case VK_3:
				case VK_4:
				case VK_5:
				case VK_6:
				case VK_7:
				case VK_8:
				case VK_9:
				case VK_DELETE:
					if(aPanel->Edit[0].Selected){
						aPanel->Edit[0].KeyIn(aKey);
						aPanel->Edit[0].Show();
						
					}else
					if(aPanel->Edit[1].Selected){
						aPanel->Edit[1].KeyIn(aKey);
						aPanel->Edit[1].Show();
					}else
					if(aPanel->Edit[2].Selected){
						aPanel->Edit[2].KeyIn(aKey);
						aPanel->Edit[2].Show();
					}else
					if(aPanel->Edit[3].Selected){
						aPanel->Edit[3].KeyIn(aKey);
						aPanel->Edit[3].Show();
						ShowType();
					}
					break;
				case VK_LEFT:
					if(CtlIndex == 0){
						if(aPanel->Edit[0].Pst <0){
							CtlIndex = 4;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[0].KeyIn(aKey);
							aPanel->Edit[0].Show();
						}
					}else
					if(CtlIndex == 1){
						if(aPanel->Edit[1].Pst <0){
							CtlIndex--;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[1].KeyIn(aKey);
							aPanel->Edit[1].Show();
						}
					}else
					if(CtlIndex == 2){
						if(aPanel->Edit[2].Pst <0){
							CtlIndex--;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[2].KeyIn(aKey);
							aPanel->Edit[2].Show();
						}
					}else
					if(CtlIndex == 3){
						if(aPanel->Edit[3].Pst <0){
							CtlIndex--;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[3].KeyIn(aKey);
							aPanel->Edit[3].Show();
							ShowType();
						}
					}else
					if(CtlIndex == 4){
						CtlIndex--;
						Check4Selected4Part();
						aPanel->ReDrawItems();
					}
					break;
				case VK_RIGHT:
					if(CtlIndex == 0){
						if(aPanel->Edit[0].Pst > 0){
							CtlIndex ++;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[0].KeyIn(aKey);
							aPanel->Edit[0].Show();
						}
					}else
					if(CtlIndex == 1){
						if(aPanel->Edit[1].Pst >1){
							CtlIndex ++;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[1].KeyIn(aKey);
							aPanel->Edit[1].Show();
						}
					}else
					if(CtlIndex == 2){
						if(aPanel->Edit[2].Pst >1){
							CtlIndex++;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[2].KeyIn(aKey);
							aPanel->Edit[2].Show();
						}
					}else
					if(CtlIndex == 3){
						if(aPanel->Edit[3].Pst >1){
							CtlIndex++;
							Check4Selected4Part();
							aPanel->ReDrawItems();
						}else{
							aPanel->Edit[3].KeyIn(aKey);
							aPanel->Edit[3].Show();
							ShowType();
						}
					}else
					if(CtlIndex == 4){
						CtlIndex =0;
						Check4Selected4Part();
						aPanel->ReDrawItems();
					}
					break;
				case VK_UP:
					if(CtlIndex >0){
						CtlIndex--;
						Check4Selected4Part();
						aPanel->ReDrawItems();
					}else{
						CtlIndex=3;
						Check4Selected4Part();
						aPanel->ReDrawItems();
					}
					break;
				case VK_DOWN:
					if(CtlIndex <4){
						CtlIndex++;
						Check4Selected4Part();
						aPanel->ReDrawItems();
					}else{
						CtlIndex=0;
						Check4Selected4Part();
						aPanel->ReDrawItems();
					}
					break;
				case VK_TAB:
					IsViewList = 1;
					Check4Selected4Part();
					aPanel->ReDrawItems();
					ShowList();
					break;
				case dfKEY_SETUP:
					TryDecValue();
					break;
				case dfKEY_QUERY:
					TryIncValue();
					break;
				case VK_EXECUTE:
					//Do Remove Here
					if(1){
						ClearOutText();
						SendCmdClear();
					}
					break;
				case VK_RETURN:
					aMsg = guiMsgReturn;
					break;
			}
		}else{
			switch(aKey){
				case VK_UP:
					if(ListIndex >0){
						ListIndex--;
						ShowList();
					}
					break;
				case VK_DOWN:
					if(ListIndex < (PageCount -1)){
						ListIndex++;
						ShowList();
					}
					break;
				case VK_TAB:
					IsViewList = 0;
					CtlIndex=2;
					Check4Selected4Part();
					aPanel->ReDrawItems();
					ShowList();
					break;
				case VK_EXECUTE:
					
					break;
				case VK_RETURN:
					aMsg = guiMsgReturn;
					break;
			}			
		}
	}
	return aMsg;	
}

void TUnRegister::SendCmdClearAll()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearAllRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = 0xFe;
	
	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 TUnRegister::SendCmdClearOnePort(unsigned char aCc)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearSinglePortRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	
	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 TUnRegister::SendCmdClearMultAddr(unsigned char aCc, unsigned char sAddr, unsigned char eAddr, unsigned char aType)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearSinglePortRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	Data[3] = sAddr;
	Data[4] = eAddr;
	Data[5] = aType;
	
	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 TUnRegister::SendCmdClearOneAddr(unsigned char aCc, unsigned char aAddr, unsigned char aType)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =3;
	
	aIde.BitF.Cmd = cmClearSinglePortAddrRegist;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aCc;
	Data[3] = aAddr;
	Data[4] = aAddr;
	Data[5] = aType;
	
	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 TUnRegister::SendCmdClear()
{
	unsigned int iVal;
	unsigned char aCc, sAddr, eAddr, aType;
	unsigned int Err = 0;
	
	iVal = aPanel->Edit[0].Str.ToInteger();
	if(iVal ==0)Err=1;
	if(iVal > 40)Err=1;
	aCc = static_cast<unsigned char>(iVal);
	
	iVal = aPanel->Edit[1].Str.ToInteger();
	if(iVal == 0)Err=1;
	if(iVal > 250)Err=1;
	sAddr = static_cast<unsigned char>(iVal);
	
	iVal = aPanel->Edit[2].Str.ToInteger();
	if(iVal == 0)Err=1;
	if(iVal > 250)Err=1;
	eAddr = static_cast<unsigned char>(iVal);
	
	iVal = aPanel->Edit[3].Str.ToInteger();
	if(iVal > 255)Err=1;
	aType = static_cast<unsigned char>(iVal);
	
	if(Err == 0){
		if(sAddr == eAddr){
			//Remove One Addr
			SendCmdClearOneAddr(aCc,sAddr,aType);
			ShowExResult(2,1);
		}else{
			if( (sAddr == 1) && (eAddr == 250)  ){
				//Remove One Port
				SendCmdClearOnePort(aCc);
				ShowExResult(0,1);
			}else{
				if(sAddr < eAddr){
					//Remove From Addr to Addr
					SendCmdClearMultAddr(aCc, sAddr, eAddr, aType);
					ShowExResult(1,1);
				}else{
					Err = 1;
				}
			}
		}
	}
	
	if(Err){
		ShowInputErr();
	}
}




