#include "IoPadDefine.h"
#include "Register.h"
#include "Runtime.h"

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	54
#define ipdWidth1		40
#define ipdWidth2		30
#define ipdWidth3		40
#define ipdWidth4		30

void TIoPadDefine::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, 	Left + ipdWidth1 + ipdWidth2 -1, 	Bottom);
	Left = Left + ipdWidth1 + ipdWidth2 ;
	Content2.Set(Left, 	Top+CH, 	Left + ipdWidth3 + ipdWidth4 -1, 	Bottom);
	Left = Left + ipdWidth3 + ipdWidth4 ;
	Content3.Set(Left, 	Top+CH, 	Left + 599 -1, 	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;
	
	State = nrCtl;
	CtlIndex = 0;
	NewRegCompleted = 0;
			
	Level0.TopIndex = 0, 
	Level0.SelectedCol = 0; 
	Level0.SelectedIndex = 0;
	Level1.TopIndex = 0, 
	Level1.SelectedCol = 0; 
	Level1.SelectedIndex = 0;
	Level2.TopIndex = 0, 
	Level2.SelectedCol = 0; 
	Level2.SelectedIndex = 0;
	
	SetListColWidth(ipdWidth0,ipdWidth1,ipdWidth2,ipdWidth3,ipdWidth4,    180,80,80,80,80);
	
	IsEditing = 0;
	IsModifing = 0;
	IsWarningSave = 0;
	IsReqReturn = 0;
	CtlLevel = 0;
}

void TIoPadDefine::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 TIoPadDefine::RenderBackGround(void)
{
	RectFillRender(Content0.Left, Content0.Top, Content3.Right, Content0.Bottom, Color);
}

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

void TIoPadDefine::RenderContent(TRect aRect, unsigned int aClr, unsigned int Index)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	unsigned int x,y, y2, aSelClr, aSelCol;
	RectFillRender(aRect.Left, ListView.FixColTop, aRect.Right, ListView.FixColBottom, FixColColor);
	y = ListView.FixColBottom + 1;
	if(Index == 0){
		aSelCol = Level0.SelectedCol;
	}else
	if(Index == 1){
		aSelCol = Level1.SelectedCol;
	}else
	if(Index == 2){
		aSelCol = Level2.SelectedCol;
	}
	for(x=0; x<(LineCount+1); x++){	
		y2 =  y + ListView.ColHeight - 1;
		if(y2 > aRect.Bottom)y2 = aRect.Bottom;		
		if(x == aSelCol){
			if(Index == 0){
				if( (CtlLevel == 0) && (IsEditing ==0) )aSelClr = clBlue;
				else aSelClr = clGray;	
			}else
			if(Index == 1){
				if( (CtlLevel == 1) && (IsEditing ==0) )aSelClr = clBlue;
				else aSelClr = clGray;	
			}
			if(Index == 2){
				if( (CtlLevel == 2) && (IsEditing ==0) )aSelClr = clBlue;
				else aSelClr = clGray;	
			}
			RectFillRender(aRect.Left, y, aRect.Right, y2, aSelClr);			
		}else{
			if(x & 1){
				RectFillRender(aRect.Left, y, aRect.Right, y2, OddColor);
			}else{
				RectFillRender(aRect.Left, y, aRect.Right, y2, Color);
			}							
		}
		y = y2 + 1;
		if(y>aRect.Bottom)break;
	}	
}

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

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

void TIoPadDefine::RenderContent2(unsigned int aClr)
{
	RenderContent(Content2, clMoneyGreen,2);
}

void TIoPadDefine::RenderContent3(unsigned int aClr)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	unsigned int x,y;
	unsigned int aRow;
	RectFillRender(Content3.Left, ListView.FixColTop, Content3.Right, ListView.FixColBottom, FixColColor);
	for(x=5; x<10; x++){
		aRow = x -5;
		for(y=0; y<LineCount; y++){
			if( (aRow == PrmEdit.SelectedRow) && (y == PrmEdit.SelectedCol) && (IsEditing) ){
				if(IsModifing)
					RectFillRender(ListView.ColLeft[x],ListView.ColTop[y], ListView.ColRight[x], ListView.ColBottom[y], clRed);
				else 
					RectFillRender(ListView.ColLeft[x],ListView.ColTop[y], ListView.ColRight[x], ListView.ColBottom[y], clBlue);
			}else
				RectFillRender(ListView.ColLeft[x],ListView.ColTop[y], ListView.ColRight[x], ListView.ColBottom[y], clNearWhite);
		}
	}	
}

void TIoPadDefine::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);
	RenderContent3(0xFF000000);
}


void TIoPadDefine::DrawCaption2(void)
{	
	if(ParentType ==0){
		TMyString::sFromStr("设置键进入编辑值,确定键保存当前盘数据", SText.Text);
	}else
	if(ParentType ==1){
		TMyString::sFromStr("设置键进入编辑值,确定键保存当前盘数据", SText.Text);
	}else
	if(ParentType ==2){
		TMyString::sFromStr("设置键进入编辑值,确定键保存当前回路数据", SText.Text);
	}
	SText.Show();
}

void TIoPadDefine::RenderWarningSave(void)
{
	int x,y,x2,y2, aW,aH;
	x = 200;
	y = 160;
	x2 = 600;
	y2 = 290;
	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,y+30,clNavy);
	RectFillRender(x+1,y+31,x2-2,y2-2,clGray);
	TextRender_string24(x+8, y+6, clNearWhite, "提示信息");
	if(ParentType  ==0){
		TextRender_string24(x+66, y+36, clNearBlack, "按确定键保存当前盘定义");
		TextRender_string24(x+66, y+66, clNearBlack, "按返回键放弃保存");
	}
	if(ParentType  ==1){
		TextRender_string24(x+66, y+36, clNearBlack, "按确定键保存当前盘定义");
		TextRender_string24(x+66, y+66, clNearBlack, "按返回键放弃保存");
	}
	if(ParentType  ==2){
		TextRender_string24(x+66, y+36, clNearBlack, "按确定键保存当前回路定义");
		TextRender_string24(x+66, y+66, clNearBlack, "按返回键放弃保存");
	}
}

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

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

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

void TIoPadDefine::SetAndShowCaption(unsigned char aType)
{
	TMyString::sFromStr(" 定义出入口属性",  Caption.Text);
}

//______________________________________________________________________________________________________________________________
void TIoPadDefine::SetHandCtl(void)
{
	ParentType = 0;
}

void TIoPadDefine::LoadHandCtl(void)
{
	int i;
	Loaded0.Count = 0;
	for(i=0; i<256;i++){
		Loaded0.ItemsPath[i] =0;
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTableHandCtl[i] == 0xA5){
			Loaded0.Count++;
			Loaded0.Type[i] = dgTYPE_HAND_CTL;
			Loaded0.ItemsPath[i] = i+1;
		}
	}	
	
	if(Loaded0.Count == 0){
		TMyString::sFromStr("定义 多线盘数量 0", Caption.Text);
		Caption.Show();
		DrawCaption2();
	}
	
	Level1.SelectedCol = 0;
	Level1.SelectedIndex = 0;
	Level1.TopIndex = 0;	
}

void TIoPadDefine::LoadHandCtlChannel(void)
{
	unsigned int i;
	unsigned char aP0;
	aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
	Loaded1.Count = 0;
	if(aP0 == 0)return;
	aP0--;
	if(aP0 < dHANDCTL_MAX_COUNT){
		for(i=0; i<HandCtlBoard[aP0].fData.HandCtl.ResistedCount; i++){
			Loaded1.Count++;
			Loaded1.Type[i] = dgTYPE_HAND_CTL_CHANNEL;
			Loaded1.ItemsPath[i] = i +1;
		}
	}
}

void TIoPadDefine::RenderHandCtl(void)
{
	int i, aIndex, BGClr, TextClr, aTop;
	unsigned int aClr;

	RenderContent0(0xFF000000);		
	aTop = ListView.FixColTop + TextTopOffSet;

	TextRender_string24(ListView.ColLeft[0],aTop,TextColor, "盘号" );
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 按键" );

	if(Loaded0.Count < 1){
		aTop = ListView.ColTop[i] + TextTopOffSet;
		TextRender_string24(ListView.ColLeft[0], aTop, aClr, "未挂载多线盘");
		return;
	}

	for(i=0; i<LineCount; i++){
		if(i == Level0.SelectedCol)aClr = SelectedTextColor;
		else aClr = TextColor;
		aIndex = Level0.TopIndex + i;
		if(aIndex < 40){
			if(  Loaded0.ItemsPath[aIndex] !=0  ){
				aTop = ListView.ColTop[i] + TextTopOffSet;
				TextDigitRender2Right24(ListView.ColLeft[0], aTop, aClr, Loaded0.ItemsPath[aIndex]);
			}
		}
	}
	
	TMyString::sFromStr("定义  多线盘数量", Caption.Text);
	
	TMyString::sAddOn3Dg(Loaded0.Count, Caption.Text);
	Caption.Show();
	DrawCaption2();	
}

void TIoPadDefine::RenderHandCtlChannel(void)
{
	unsigned char i,x,y, aType, aIndex;
	unsigned int aClr;
	int aTop;
	
	RenderContent1(0xFF000000);
	RenderContent2(0xFF000000);
	
	aTop = ListView.FixColTop + TextTopOffSet;
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 通道" );
	TextRender_string24(ListView.ColLeft[3],aTop,TextColor, "---" );
	
	x = Level1.TopIndex;
	for(i=0; i<LineCount; i++){
		if(x >= Loaded1.Count)break;
		if(i == Level1.SelectedCol)aClr = clNearWhite; else aClr = clNearBlack;
		aIndex = Loaded1.ItemsPath[x];
		if(aIndex < 1)return;
		
		aTop = ListView.ColTop[i] + TextTopOffSet;
		TextDigitRender3Right24(ListView.ColLeft[1], aTop, aClr, aIndex);
		
		aType = Loaded1.Type[x];
		if(aType > 30)aType = 30;
		ImageList.Render(24,    aType,      ListView.ColLeft[2], aTop);
		
		aClr = clNearBlack;
		TextDigitRender3Right24(ListView.ColLeft[3], aTop, aClr, 1);
		x++;
	}	
}

void TIoPadDefine::RenderHandCtlChannelPrm(void)
{
	int aTop, i, aIdx, aCount, aType;
	unsigned int aClr;
	
	RenderContent3(0xFF000000);
	
	aTop = ListView.FixColTop + TextTopOffSet;
	TextRender_string24(ListView.ColLeft[5],aTop,TextColor, " 指派类型" );
	TextRender_string24(ListView.ColLeft[6],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[7],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[8],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[9],aTop,TextColor, "区域" );	
	
	aCount = Loaded1.Count;
	aIdx = Level1.TopIndex;
	aClr = clNearBlack;
	if(1){
		for(i=0; i<ListLineCnt; i++){
			if(aIdx >= aCount)return;
			if(1){
				if((IsEditing) && (PrmEdit.SelectedCol == i) ){
					aTop = ListView.ColTop[i] + TextTopOffSet;
					
					if(PrmEdit.SelectedRow == 0)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					aType = ValueList[aIdx][0].eType;
					if(aType >30)aType =30;
					TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_oType[aType]);
	
					if(PrmEdit.SelectedRow == 1)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,aClr, ValueList[aIdx][0].Group0 );
						
					if(PrmEdit.SelectedRow == 2)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,aClr,  ValueList[aIdx][0].Group1);
						
					if(PrmEdit.SelectedRow == 3)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,aClr,  ValueList[aIdx][0].Group2 );
					
					if(PrmEdit.SelectedRow == 4)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,aClr,  ValueList[aIdx][0].Group3 );

				}else{
					aTop = ListView.ColTop[i] + TextTopOffSet;
					aType = ValueList[aIdx][0].eType;
					if(aType >30)aType =30;
					TextRender_string24(ListView.ColLeft[5],aTop,clNearBlack, StrAssign_oType[aType]);
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,clNearBlack,  ValueList[aIdx][0].Group0 );
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,clNearBlack,  ValueList[aIdx][0].Group1 );
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,clNearBlack,  ValueList[aIdx][0].Group2);
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,clNearBlack,  ValueList[aIdx][0].Group3);
				}
			}
			aIdx++;
		}
	}
}

TGuiMsgReturn TIoPadDefine::HandCtlKeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	if(aKey == VK_LEFT){
		if(IsEditing){
			if(PrmEdit.SelectedRow>0){
				PrmEdit.SelectedRow--;
				RenderHandCtlChannelPrm();
			}else{
				IsEditing = 0;
				IsModifing = 0;
				RenderHandCtlChannel();
				RenderHandCtlChannelPrm();
			}
		}else{
			if(CtlLevel >0){
				if(CtlLevel == 1){
					CtlLevel = 0;
					RenderHandCtl();
					RenderHandCtlChannel();
					RenderHandCtlChannelPrm();
					IsWarningSave =1;
					RenderWarningSave();
				}else{
					CtlLevel--;
					if(CtlLevel == 0){

					}else
					if(CtlLevel == 1){
						LoadHandCtl();
						RenderHandCtlChannel();
						RenderHandCtlChannelPrm();
					}else{
						CtlLevel =0;
						LoadHandCtl();
						LoadHandCtlChannel();
						RenderHandCtl();
						RenderHandCtlChannel();
						RenderHandCtlChannelPrm();
					}
				}
			}
		}
	}else
	if(aKey == VK_RIGHT){
		if(IsEditing){
			if(PrmEdit.SelectedRow<4){
				PrmEdit.SelectedRow++;
				RenderHandCtlChannelPrm();
			}
		}else{
			if(CtlLevel == 0){
				CtlLevel++;
				LoadHandCtlChannel();
				RenderHandCtl();
				RenderHandCtlChannel();
				RenderHandCtlChannelPrm();
			}else
			if(CtlLevel == 1){
				IsEditing = 1;
				PrmEdit.SelectedCol = Level1.SelectedCol;
				PrmEdit.SelectedRow = 0;
				RenderHandCtlChannel();
				RenderHandCtlChannelPrm();
			}
		}			
	}else
	if(aKey == VK_UP){
		if(IsEditing){
			if(IsModifing){	
				aCh = 0;
				if(CtlLevel == 1){
					y = Level1.SelectedIndex;
				}
				x = PrmEdit.SelectedRow;
				if(x==0){
					if(ValueList[y][aCh].eType < 30)ValueList[y][aCh].eType++;
				}else
				if(x==1){
					ValueList[y][aCh].Group0++;
				}else
				if(x==2){
					ValueList[y][aCh].Group1++;
				}else
				if(x==3){
					ValueList[y][aCh].Group2++;
				}else
				if(x==4){
					ValueList[y][aCh].Group3++;
				}
				RenderHandCtlChannel();
				RenderHandCtlChannelPrm();				
			}else{
				if(CtlLevel ==1){
					if(Level1.SelectedCol >0){
						Level1.SelectedCol--;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						PrmEdit.TopIndex = Level1.TopIndex;
						PrmEdit.SelectedIndex = Level1.SelectedIndex;
						PrmEdit.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}else{
						if(Level1.TopIndex >0){
							Level1.TopIndex --;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}
					if(Updata){
						RenderHandCtl();
						RenderHandCtlChannel();
						RenderHandCtlChannelPrm();
					}
				}
			}
		}else{
			if(CtlLevel == 0){
				//ListLineCnt
				if(Level0.SelectedCol >0){
					Level0.SelectedCol--;
					Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
					Level1.SelectedCol =0;
					Level1.SelectedIndex = 0;
					Level1.SelectedIndex = 0;
					Level2.SelectedCol =0;
					Level2.SelectedIndex = 0;
					Level2.SelectedIndex = 0;
					LoadHandCtl();
					LoadHandCtlChannel();
					RenderHandCtl();
					RenderHandCtlChannel();
					RenderHandCtlChannelPrm();
					
				}else{
					if(Level0.TopIndex >0){
						Level0.TopIndex --;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadHandCtl();
						LoadHandCtlChannel();
						RenderHandCtl();
						RenderHandCtlChannel();
						RenderHandCtlChannelPrm();
					}
				}
			}else
			if(CtlLevel == 1){
				if(Level1.SelectedCol >0){
					Level1.SelectedCol--;
					Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
					Level2.TopIndex = Level1.TopIndex;
					Level2.SelectedIndex = Level1.SelectedIndex;
					Level2.SelectedCol = Level1.SelectedCol;
					
					aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
					aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
					Updata = 1;
				}else{
					if(Level1.TopIndex >0){
						Level1.TopIndex --;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}
				if(Updata){
					RenderHandCtlChannel();
					RenderHandCtlChannelPrm();
				}
			}
		}
	}else
	if(aKey == VK_DOWN){
		if(IsEditing){
			if(IsModifing){
				aCh = 0;
				y = Level1.SelectedIndex;
				x = PrmEdit.SelectedRow;
				if(x==0){
					if(ValueList[y][aCh].eType >0)ValueList[y][aCh].eType--;
				}else
				if(x==1){
					ValueList[y][aCh].Group0--;
				}else
				if(x==2){
					ValueList[y][aCh].Group1--;
				}else
				if(x==3){
					ValueList[y][aCh].Group2--;
				}else
				if(x==4){
					ValueList[y][aCh].Group3--;
				}
				RenderHandCtlChannel();
				RenderHandCtlChannelPrm();
			}else{
				if(CtlLevel == 1){
					if(Level1.SelectedCol < (ListLineCnt-1)){
						if( (Level1.TopIndex + Level1.SelectedCol) < (Loaded1.Count -1) ){
							Level1.SelectedCol++;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}else{
						if( (Level1.TopIndex + ListLineCnt) < Loaded1.Count){
							Level1.TopIndex ++;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}
					if(Updata){
						RenderHandCtlChannel();
						RenderHandCtlChannelPrm();
					}					
				}
			}
		}else{
			if(CtlLevel == 0){
				//ListLineCnt
				if(Level0.SelectedCol < (ListLineCnt-1)){
					if( (Level0.TopIndex + Level0.SelectedCol) < (Loaded0.Count -1) ){
						Level0.SelectedCol++;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadHandCtl();
						LoadHandCtlChannel();
						RenderHandCtl();
						RenderHandCtlChannel();
						RenderHandCtlChannelPrm();
					}
				}else{
					if( (Level0.TopIndex + ListLineCnt) < Loaded0.Count){
						Level0.TopIndex ++;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadHandCtl();
						LoadHandCtlChannel();
						RenderHandCtl();
						RenderHandCtlChannel();
						RenderHandCtlChannelPrm();
					}
				}
			}else
			if(CtlLevel == 1){
				if(Level1.SelectedCol < (ListLineCnt-1)){
					if( (Level1.TopIndex + Level1.SelectedCol) < (Loaded1.Count -1) ){
						Level1.SelectedCol++;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}else{
					if( (Level1.TopIndex + ListLineCnt) < Loaded1.Count){
						Level1.TopIndex ++;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}
				if(Updata){
					RenderHandCtlChannel();
					RenderHandCtlChannelPrm();
				}
			}
		}
	}else
	if(aKey == VK_F1){
		//Go Modify
		if(IsEditing){
			if(IsModifing)IsModifing =0; else IsModifing = 1;
			RenderHandCtlChannelPrm();
		}
	}else
	if(aKey == VK_TAB){
		if(IsWarningSave){
			//User Not Save Here
			IsWarningSave = 0;
			CtlLevel = 0;
			RenderHandCtl();
			RenderHandCtlChannel();
			RenderHandCtlChannelPrm();
		}
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(IsWarningSave){
			//Save Here
			if(IsReqReturn){
				//Save and Return
				
				aMsg = guiMsgReturn;
			}else{
				IsWarningSave = 0;
				RenderHandCtl();
				RenderHandCtlChannel();
				RenderHandCtlChannelPrm();
			}
		}else{
			//ask if Save Here
			IsWarningSave = 1;
			RenderWarningSave();
		}
	}else
	if(aKey == VK_RETURN){
		if(IsWarningSave ==0){
			IsWarningSave = 1;
			RenderWarningSave();
			IsReqReturn = 1;
		}else{
			IsWarningSave = 0;
			if(IsReqReturn){
				aMsg = guiMsgReturn;
			}else{
				RenderHandCtlChannel();
				RenderHandCtlChannelPrm();
			}
		}
	}
	return aMsg;		
}

//_________________________________________________________________________________________________________________________________________
void TIoPadDefine::SetDirectCtl(void)
{
	ParentType = 1;
}

void TIoPadDefine::LoadDirectCtl(void)
{
	int i;
	Loaded0.Count = 0;
	for(i=0; i<256;i++){
		Loaded0.ItemsPath[i] =0;
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTableDirectCtl[i] == 0xA5){
			Loaded0.Count++;
			Loaded0.Type[i] = dgTYPE_DIRECT_CTL;
			Loaded0.ItemsPath[i] = i+1;
		}
	}	
	
	if(Loaded0.Count == 0){
		TMyString::sFromStr("定义 多线盘数量 0", Caption.Text);
		Caption.Show();
		DrawCaption2();
	}
	
	Level1.SelectedCol = 0;
	Level1.SelectedIndex = 0;
	Level1.TopIndex = 0;	
}

void TIoPadDefine::LoadDirectCtlChannel(void)
{
	unsigned int i;
	unsigned char aP0;
	aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
	Loaded1.Count = 0;
	if(aP0 == 0)return;
	aP0--;
	if(aP0 < dDIRECTCTL_MAX_COUNT){
		for(i=0; i<DirectCtlBoard[aP0].fData.DirectCtl.ResistedCount; i++){
			Loaded1.Count++;
			Loaded1.Type[i] = dgTYPE_DIRECT_CTL_CHANNEL;
			Loaded1.ItemsPath[i] = i +1;
		}
	}
}

void TIoPadDefine::RenderDirectCtl(void)
{
	int i, aIndex, BGClr, TextClr, aTop;
	unsigned int aClr;

	RenderContent0(0xFF000000);		
	aTop = ListView.FixColTop + TextTopOffSet;

	TextRender_string24(ListView.ColLeft[0],aTop,TextColor, "盘号" );
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 按键" );

	if(Loaded0.Count < 1){
		aTop = ListView.ColTop[i] + TextTopOffSet;
		TextRender_string24(ListView.ColLeft[0], aTop, aClr, "未挂载多线盘");
		return;
	}

	for(i=0; i<LineCount; i++){
		if(i == Level0.SelectedCol)aClr = SelectedTextColor;
		else aClr = TextColor;
		aIndex = Level0.TopIndex + i;
		if(aIndex < 40){
			if(  Loaded0.ItemsPath[aIndex] !=0  ){
				aTop = ListView.ColTop[i] + TextTopOffSet;
				TextDigitRender2Right24(ListView.ColLeft[0], aTop, aClr, Loaded0.ItemsPath[aIndex]);
			}
		}
	}
	
	TMyString::sFromStr("定义  多线盘数量", Caption.Text);
	
	TMyString::sAddOn3Dg(Loaded0.Count, Caption.Text);
	Caption.Show();
	DrawCaption2();	
}

void TIoPadDefine::RenderDirectCtlChannel(void)
{
	unsigned char i,x,y, aType, aIndex;
	unsigned int aClr;
	int aTop;
	
	RenderContent1(0xFF000000);
	RenderContent2(0xFF000000);
	
	aTop = ListView.FixColTop + TextTopOffSet;
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 通道" );
	TextRender_string24(ListView.ColLeft[3],aTop,TextColor, "---" );
	
	x = Level1.TopIndex;
	for(i=0; i<LineCount; i++){
		if(x >= Loaded1.Count)break;
		if(i == Level1.SelectedCol)aClr = clNearWhite; else aClr = clNearBlack;
		aIndex = Loaded1.ItemsPath[x];
		if(aIndex < 1)return;
		
		aTop = ListView.ColTop[i] + TextTopOffSet;
		TextDigitRender3Right24(ListView.ColLeft[1], aTop, aClr, aIndex);
		
		aType = Loaded1.Type[x];
		if(aType > 30)aType = 30;
		ImageList.Render(24,    aType,      ListView.ColLeft[2], aTop);
		
		aClr = clNearBlack;
		TextDigitRender3Right24(ListView.ColLeft[3], aTop, aClr, 1);
		x++;
	}	
}

void TIoPadDefine::RenderDirectCtlChannelPrm(void)
{
	int aTop, i, aIdx, aCount, aType;
	unsigned int aClr;
	
	RenderContent3(0xFF000000);
	
	aTop = ListView.FixColTop + TextTopOffSet;
	TextRender_string24(ListView.ColLeft[5],aTop,TextColor, " 指派类型" );
	TextRender_string24(ListView.ColLeft[6],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[7],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[8],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[9],aTop,TextColor, "区域" );	
	
	aCount = Loaded1.Count;
	aIdx = Level1.TopIndex;
	aClr = clNearBlack;
	if(1){
		for(i=0; i<ListLineCnt; i++){
			if(aIdx >= aCount)return;
			if(1){
				if((IsEditing) && (PrmEdit.SelectedCol == i) ){
					aTop = ListView.ColTop[i] + TextTopOffSet;
					
					if(PrmEdit.SelectedRow == 0)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					aType = ValueList[aIdx][0].eType;
					if(aType >30)aType =30;
					TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_oType[aType]);
	
					if(PrmEdit.SelectedRow == 1)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,aClr, ValueList[aIdx][0].Group0 );
						
					if(PrmEdit.SelectedRow == 2)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,aClr,  ValueList[aIdx][0].Group1);
						
					if(PrmEdit.SelectedRow == 3)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,aClr,  ValueList[aIdx][0].Group2 );
					
					if(PrmEdit.SelectedRow == 4)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,aClr,  ValueList[aIdx][0].Group3 );

				}else{
					aTop = ListView.ColTop[i] + TextTopOffSet;
					aType = ValueList[aIdx][0].eType;
					if(aType >30)aType =30;
					TextRender_string24(ListView.ColLeft[5],aTop,clNearBlack, StrAssign_oType[aType]);
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,clNearBlack,  ValueList[aIdx][0].Group0 );
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,clNearBlack,  ValueList[aIdx][0].Group1 );
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,clNearBlack,  ValueList[aIdx][0].Group2);
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,clNearBlack,  ValueList[aIdx][0].Group3);
				}
			}
			aIdx++;
		}
	}
}

TGuiMsgReturn TIoPadDefine::DirectCtlKeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	if(aKey == VK_LEFT){
		if(IsEditing){
			if(PrmEdit.SelectedRow>0){
				PrmEdit.SelectedRow--;
				RenderDirectCtlChannelPrm();
			}else{
				IsEditing = 0;
				IsModifing = 0;
				RenderDirectCtlChannel();
				RenderDirectCtlChannelPrm();
			}
		}else{
			if(CtlLevel >0){
				if(CtlLevel == 1){
					CtlLevel = 0;
					RenderDirectCtl();
					RenderDirectCtlChannel();
					RenderDirectCtlChannelPrm();
					IsWarningSave =1;
					RenderWarningSave();
				}else{
					CtlLevel--;
					if(CtlLevel == 0){

					}else
					if(CtlLevel == 1){
						LoadDirectCtl();
						RenderDirectCtlChannel();
						RenderDirectCtlChannelPrm();
					}else{
						CtlLevel =0;
						LoadDirectCtl();
						LoadDirectCtlChannel();
						RenderDirectCtl();
						RenderDirectCtlChannel();
						RenderDirectCtlChannelPrm();
					}
				}
			}
		}
	}else
	if(aKey == VK_RIGHT){
		if(IsEditing){
			if(PrmEdit.SelectedRow<4){
				PrmEdit.SelectedRow++;
				RenderDirectCtlChannelPrm();
			}
		}else{
			if(CtlLevel == 0){
				CtlLevel++;
				LoadDirectCtlChannel();
				RenderDirectCtl();
				RenderDirectCtlChannel();
				RenderDirectCtlChannelPrm();
			}else
			if(CtlLevel == 1){
				IsEditing = 1;
				PrmEdit.SelectedCol = Level1.SelectedCol;
				PrmEdit.SelectedRow = 0;
				RenderDirectCtlChannel();
				RenderDirectCtlChannelPrm();
			}
		}			
	}else
	if(aKey == VK_UP){
		if(IsEditing){
			if(IsModifing){	
				aCh = 0;
				if(CtlLevel == 1){
					y = Level1.SelectedIndex;
				}
				x = PrmEdit.SelectedRow;
				if(x==0){
					if(ValueList[y][aCh].eType < 30)ValueList[y][aCh].eType++;
				}else
				if(x==1){
					ValueList[y][aCh].Group0++;
				}else
				if(x==2){
					ValueList[y][aCh].Group1++;
				}else
				if(x==3){
					ValueList[y][aCh].Group2++;
				}else
				if(x==4){
					ValueList[y][aCh].Group3++;
				}
				RenderDirectCtlChannel();
				RenderDirectCtlChannelPrm();				
			}else{
				if(CtlLevel ==1){
					if(Level1.SelectedCol >0){
						Level1.SelectedCol--;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						PrmEdit.TopIndex = Level1.TopIndex;
						PrmEdit.SelectedIndex = Level1.SelectedIndex;
						PrmEdit.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}else{
						if(Level1.TopIndex >0){
							Level1.TopIndex --;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}
					if(Updata){
						RenderDirectCtl();
						RenderDirectCtlChannel();
						RenderDirectCtlChannelPrm();
					}
				}
			}
		}else{
			if(CtlLevel == 0){
				//ListLineCnt
				if(Level0.SelectedCol >0){
					Level0.SelectedCol--;
					Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
					Level1.SelectedCol =0;
					Level1.SelectedIndex = 0;
					Level1.SelectedIndex = 0;
					Level2.SelectedCol =0;
					Level2.SelectedIndex = 0;
					Level2.SelectedIndex = 0;
					LoadDirectCtl();
					LoadDirectCtlChannel();
					RenderDirectCtl();
					RenderDirectCtlChannel();
					RenderDirectCtlChannelPrm();
					
				}else{
					if(Level0.TopIndex >0){
						Level0.TopIndex --;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadDirectCtl();
						LoadDirectCtlChannel();
						RenderDirectCtl();
						RenderDirectCtlChannel();
						RenderDirectCtlChannelPrm();
					}
				}
			}else
			if(CtlLevel == 1){
				if(Level1.SelectedCol >0){
					Level1.SelectedCol--;
					Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
					Level2.TopIndex = Level1.TopIndex;
					Level2.SelectedIndex = Level1.SelectedIndex;
					Level2.SelectedCol = Level1.SelectedCol;
					
					aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
					aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
					Updata = 1;
				}else{
					if(Level1.TopIndex >0){
						Level1.TopIndex --;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}
				if(Updata){
					RenderDirectCtlChannel();
					RenderDirectCtlChannelPrm();
				}
			}
		}
	}else
	if(aKey == VK_DOWN){
		if(IsEditing){
			if(IsModifing){
				aCh = 0;
				y = Level1.SelectedIndex;
				x = PrmEdit.SelectedRow;
				if(x==0){
					if(ValueList[y][aCh].eType >0)ValueList[y][aCh].eType--;
				}else
				if(x==1){
					ValueList[y][aCh].Group0--;
				}else
				if(x==2){
					ValueList[y][aCh].Group1--;
				}else
				if(x==3){
					ValueList[y][aCh].Group2--;
				}else
				if(x==4){
					ValueList[y][aCh].Group3--;
				}
				RenderDirectCtlChannel();
				RenderDirectCtlChannelPrm();
			}else{
				if(CtlLevel == 1){
					if(Level1.SelectedCol < (ListLineCnt-1)){
						if( (Level1.TopIndex + Level1.SelectedCol) < (Loaded1.Count -1) ){
							Level1.SelectedCol++;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}else{
						if( (Level1.TopIndex + ListLineCnt) < Loaded1.Count){
							Level1.TopIndex ++;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}
					if(Updata){
						RenderDirectCtlChannel();
						RenderDirectCtlChannelPrm();
					}					
				}
			}
		}else{
			if(CtlLevel == 0){
				//ListLineCnt
				if(Level0.SelectedCol < (ListLineCnt-1)){
					if( (Level0.TopIndex + Level0.SelectedCol) < (Loaded0.Count -1) ){
						Level0.SelectedCol++;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadDirectCtl();
						LoadDirectCtlChannel();
						RenderDirectCtl();
						RenderDirectCtlChannel();
						RenderDirectCtlChannelPrm();
					}
				}else{
					if( (Level0.TopIndex + ListLineCnt) < Loaded0.Count){
						Level0.TopIndex ++;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadDirectCtl();
						LoadDirectCtlChannel();
						RenderDirectCtl();
						RenderDirectCtlChannel();
						RenderDirectCtlChannelPrm();
					}
				}
			}else
			if(CtlLevel == 1){
				if(Level1.SelectedCol < (ListLineCnt-1)){
					if( (Level1.TopIndex + Level1.SelectedCol) < (Loaded1.Count -1) ){
						Level1.SelectedCol++;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}else{
					if( (Level1.TopIndex + ListLineCnt) < Loaded1.Count){
						Level1.TopIndex ++;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}
				if(Updata){
					RenderDirectCtlChannel();
					RenderDirectCtlChannelPrm();
				}
			}
		}
	}else
	if(aKey == VK_F1){
		//Go Modify
		if(IsEditing){
			if(IsModifing)IsModifing =0; else IsModifing = 1;
			RenderDirectCtlChannelPrm();
		}
	}else
	if(aKey == VK_TAB){
		if(IsWarningSave){
			//User Not Save Here
			IsWarningSave = 0;
			CtlLevel = 0;
			RenderDirectCtl();
			RenderDirectCtlChannel();
			RenderDirectCtlChannelPrm();
		}
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(IsWarningSave){
			//Save Here
			if(IsReqReturn){
				//Save and Return
				
				aMsg = guiMsgReturn;
			}else{
				IsWarningSave = 0;
				RenderDirectCtl();
				RenderDirectCtlChannel();
				RenderDirectCtlChannelPrm();
			}
		}else{
			//ask if Save Here
			IsWarningSave = 1;
			RenderWarningSave();
		}
	}else
	if(aKey == VK_RETURN){
		if(IsWarningSave ==0){
			IsWarningSave = 1;
			RenderWarningSave();
			IsReqReturn = 1;
		}else{
			IsWarningSave = 0;
			if(IsReqReturn){
				aMsg = guiMsgReturn;
			}else{
				RenderDirectCtlChannel();
				RenderDirectCtlChannelPrm();
			}
		}
	}
	return aMsg;		
}

//________________________________________________________________________________________________________________________________________
//________________________________________________________________________________________________________________________________________
void TIoPadDefine::SetPort(void)
{
	ParentType = 2;
}

void TIoPadDefine::LoadPort(void)
{
	int i;
	Loaded0.Count = 0;
	for(i=0; i<256;i++){
		Loaded0.ItemsPath[i] =0;
	}
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
			Loaded0.Count++;
			Loaded0.Type[i] = dgTYPE_PORT;
			Loaded0.ItemsPath[i] = i+1;
		}
	}	
	
	if(Loaded0.Count == 0){
		TMyString::sFromStr("定义 回路数量 0", Caption.Text);
		Caption.Show();
		DrawCaption2();
	}
	
	Level1.SelectedCol = 0;
	Level1.SelectedIndex = 0;
	Level1.TopIndex = 0;
	Level2.SelectedCol = 0;
	Level2.SelectedIndex = 0;
	Level2.TopIndex = 0;
}

void TIoPadDefine::LoadEp(void)
{
	unsigned int i,y;
	unsigned char aP0, aType;
	int aTop;
	if(Loaded0.Count ==0) return;
	aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
	if(aP0 == 0)return;
	aP0--;
	if(11){
		Loaded1.Count = 0;
		if(aP0 < dPORT_MAX_COUNT){
			for(i=0; i<dEP_MAX_COUNT_PER_PORT; i++){
				if(Port[aP0].ExistTable[i] == 0xA5){
					aType = EpDev[aP0][i].fData.Split.dType;
					Loaded1.Type[Loaded1.Count] = aType;
					Loaded1.ItemsPath[Loaded1.Count] = i +1;
					
					if(aType == dgTYPE_TEMPE_DETECTOR){
						Loaded1.ChannelCount[Loaded1.Count] = 2;
						Loaded1.ChannelInCount[Loaded1.Count] = 2;
					}else
					//if(aType == dgTYPE_SMOKE_TEMPE_DETECTOR_II){
					//	Loaded1.ChannelCount[Loaded1.Count] = 2;
					//	Loaded1.ChannelInCount[Loaded1.Count] = 2;
					//}else
					if(aType == dgTYPE_IN_MODULE){
						//find prm in EpDev[][]
						Loaded1.ChannelCount[Loaded1.Count] = 2;
						Loaded1.ChannelInCount[Loaded1.Count] = 2;
					}else
					if(aType == dgTYPE_OUT_MODULE){
						Loaded1.ChannelCount[Loaded1.Count] = 2;
						Loaded1.ChannelInCount[Loaded1.Count] = 0;
					}else
					if(  (aType == dgTYPE_INOUT_MODULE_NonSource) || 
					     (aType == dgTYPE_INOUT_MODULE_Source) ){
						Loaded1.ChannelCount[Loaded1.Count] = 2;
						Loaded1.ChannelInCount[Loaded1.Count] = 0;
					}else{
						Loaded1.ChannelCount[Loaded1.Count] = 1;
						Loaded1.ChannelInCount[Loaded1.Count] = 1;
					}
					Loaded1.Count++;
				}
			}
		}
	}
	
	TMyString::sFromStr("定义  回路-", Caption.Text);
	TMyString::sAddOn2Dg(aP0+1, Caption.Text);
	TMyString::sAddOnStr(" 挂载",Caption.Text);
	TMyString::sAddOn2Dg(Loaded1.Count, Caption.Text);
	Caption.Show();
	DrawCaption2();
}

void TIoPadDefine::LoadEpChannel(void)
{
	unsigned int i,y;
	unsigned char aP0, aP1, aType, aHalf, aIdx;
	aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
	aP1 = Loaded1.ItemsPath[Level1.SelectedIndex];
	if(aP0 == 0) return;
	if(aP1 == 0) return;
	aP0--;
	aP1--;
	aIdx = Level1.SelectedIndex;
	if(Port[aP0].ExistTable[aP1] == 0xA5){
		Loaded2.Count = 0;
		aType = Loaded1.Type[Level1.SelectedIndex];
		if(1){
			if(aType == dgTYPE_SMOKE_DETECTOR){
				Loaded2.Count+=2;
				Loaded2.IsOut[0] = 0;
				Loaded2.IsOut[1] = 0;
				Loaded2.ItemsPath[0] = 1;
				Loaded2.ItemsPath[1] = 2;
			}else
			if(aType == dgTYPE_TEMPE_DETECTOR){
				Loaded2.Count+=2;
				Loaded2.IsOut[0] = 0;
				Loaded2.IsOut[1] = 0;
				Loaded2.ItemsPath[0] = 1;
				Loaded2.ItemsPath[1] = 2;
			}else
			if(aType == dgTYPE_IN_MODULE){
				for(i=0; i<Loaded1.ChannelCount[aIdx];i++){
					Loaded2.Count++;
					Loaded2.IsOut[i] = 0;
					Loaded2.ItemsPath[i] = i +1;
				}
			}else
			if(aType == dgTYPE_OUT_MODULE){
				for(i=0; i<Loaded1.ChannelCount[aIdx];i++){
					Loaded2.Count++;
					Loaded2.IsOut[i] = 1;
					Loaded2.ItemsPath[i] = i +1;
				}
			}else
			if( (aType == dgTYPE_INOUT_MODULE_NonSource) || 
				(aType == dgTYPE_INOUT_MODULE_Source) ){
				if(Loaded1.ChannelCount[aIdx]==2){
					Loaded2.Count+=2;
					Loaded2.IsOut[0] = 0;
					Loaded2.ItemsPath[0] = 1;
					Loaded2.IsOut[1] = 1;
					Loaded2.ItemsPath[1] = 2;
				}
				if(Loaded1.ChannelCount[aIdx]==4){
					Loaded2.Count+=4;
					Loaded2.IsOut[0] = 0;
					Loaded2.ItemsPath[0] = 1;
					Loaded2.IsOut[1] = 0;
					Loaded2.ItemsPath[1] = 2;
					Loaded2.IsOut[2] = 1;
					Loaded2.ItemsPath[2] = 3;
					Loaded2.IsOut[3] = 1;
					Loaded2.ItemsPath[3] = 4;
				}
			}else{
				Loaded2.Count =1;
			}
		}
	}
}


void TIoPadDefine::RenderPort(void)
{
	int i, aIndex, BGClr, TextClr, aTop;
	unsigned int aClr;

	RenderContent0(0xFF000000);		
	aTop = ListView.FixColTop + TextTopOffSet;

	TextRender_string24(ListView.ColLeft[0],aTop,TextColor, "回路" );
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 地址" );
	TextRender_string24(ListView.ColLeft[3],aTop,TextColor, " 通道" );

	for(i=0; i<LineCount; i++){
		if(i == Level0.SelectedCol){
			if(CtlLevel == 0)aClr = SelectedTextColor;
			else aClr = SelectedTextColor;
		}else{
			aClr = TextColor;
		}			
		aIndex = Level0.TopIndex + i;
		if(aIndex < Loaded0.Count){
			if(  Loaded0.ItemsPath[aIndex] !=0  ){
				aTop = ListView.ColTop[i] + TextTopOffSet;
				TextDigitRender2Right24(ListView.ColLeft[0] + 8, aTop, aClr, Loaded0.ItemsPath[aIndex]);
			}
		}
	}	
}

void TIoPadDefine::RenderEp(void)
{
	int i, x, aIndex, BGClr, TextClr, aTop;
	unsigned int OutCount;
	unsigned int aClr;
	unsigned char aType;
	unsigned char aP0;

	RenderContent1(0xFF000000);	
	RenderContent2(0xFF000000);	
	aTop = ListView.FixColTop + TextTopOffSet;

	TextRender_string24(ListView.ColLeft[0],aTop,TextColor, "回路" );
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 地址" );
	TextRender_string24(ListView.ColLeft[3],aTop,TextColor, " 通道" );
	
	x = Level1.TopIndex;
	for(i=0; i<LineCount; i++){
		if(x >= Loaded1.Count)break;
		if(i == Level1.SelectedCol)aClr = clNearWhite; else aClr = clNearBlack;
		aIndex = Loaded1.ItemsPath[x];
		if(aIndex < 1)return;
		
		aTop = ListView.ColTop[i] + TextTopOffSet;
		TextDigitRender3Right24(ListView.ColLeft[1], aTop, aClr, aIndex);
		
		aType = Loaded1.Type[x];
		if(aType > 30)aType = 30;
		ImageList.Render(24,    aType,      ListView.ColLeft[2], aTop);
		
		aClr = clNearBlack;
		if(Loaded1.ChannelCount[x] > 1){
			TextDigitRender3Right24(ListView.ColLeft[3], aTop, aClr, Loaded1.ChannelInCount[x]);
		}else{
			TextDigitRender3Right24(ListView.ColLeft[3], aTop, aClr, Loaded1.ChannelCount[x]);
		}
		
		x++;
	}
}


void TIoPadDefine::RenderEpChannel(void)
{
	int i, x, aIndex, BGClr, TextClr, aTop;
	unsigned int aClr;
	unsigned char aType;
	unsigned char aP0;

	RenderContent2(0xFF000000);	
	aTop = ListView.FixColTop + TextTopOffSet;

	TextRender_string24(ListView.ColLeft[0],aTop,TextColor, "回路" );
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 地址" );
	TextRender_string24(ListView.ColLeft[3],aTop,TextColor, " 通道" );
	
	x = Level2.TopIndex;
	for(i=0; i<LineCount; i++){
		if(x >= Loaded2.Count)break;
		if(i == Level2.SelectedCol)aClr = clNearWhite; else aClr = clNearBlack;
		aIndex = Loaded2.ItemsPath[x];
		if(aIndex < 1)return;
		
		aTop = ListView.ColTop[i] + TextTopOffSet;
		TextDigitRender3Right24(ListView.ColLeft[3], aTop, aClr, aIndex);
		
		x++;
	}	
}

void TIoPadDefine::RenderEpPrm(void)
{
	int aTop, i, x, aIdx, aCount;
	unsigned int aClr;
	
	RenderContent3(clMoneyGreen);
	
	aTop = ListView.FixColTop + TextTopOffSet;
	TextRender_string24(ListView.ColLeft[5],aTop,TextColor, " 指派类型" );
	TextRender_string24(ListView.ColLeft[6],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[7],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[8],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[9],aTop,TextColor, "区域" );	
	
	aCount = Loaded1.Count;
	aIdx = Level1.TopIndex;
	aClr = clNearBlack;
	if(1){
		for(i=0; i<ListLineCnt; i++){
			if(aIdx >= aCount)return;
			if(Loaded1.ChannelCount[aIdx] == 1){
				if((IsEditing) && (PrmEdit.SelectedCol == i) ){
					aTop = ListView.ColTop[i] + TextTopOffSet;
					
					if(PrmEdit.SelectedRow == 0)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					if(Loaded1.ChannelInCount)
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_iType[ValueList[aIdx][0].eType]);
					else
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_oType[ValueList[aIdx][0].eType]);
	
					if(PrmEdit.SelectedRow == 1)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,aClr, ValueList[aIdx][0].Group0 );
						
					if(PrmEdit.SelectedRow == 2)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,aClr,  ValueList[aIdx][0].Group1);
						
					if(PrmEdit.SelectedRow == 3)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,aClr,  ValueList[aIdx][0].Group2 );
					
					if(PrmEdit.SelectedRow == 4)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,aClr,  ValueList[aIdx][0].Group3 );

				}else{
					aTop = ListView.ColTop[i] + TextTopOffSet;
					if(Loaded1.ChannelInCount)
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_iType[ValueList[aIdx][0].eType]);
					else
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_oType[ValueList[aIdx][0].eType]);
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,clNearBlack,  ValueList[aIdx][0].Group0 );
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,clNearBlack,  ValueList[aIdx][0].Group1 );
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,clNearBlack,  ValueList[aIdx][0].Group2);
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,clNearBlack,  ValueList[aIdx][0].Group3);
				}
			}
			aIdx++;
		}
	}
}

void TIoPadDefine::RenderEpChannelPrm()
{
	int aTop, i, x, aIdx, aCount, aType;
	unsigned int aClr;
	unsigned int sColClr, sRowClr;
	unsigned int sColTextClr, sRowTextClr;
	unsigned char aCh;
	
	RenderContent3(clMoneyGreen);
	
	aTop = ListView.FixColTop + TextTopOffSet;
	TextRender_string24(ListView.ColLeft[5],aTop,TextColor, " 指派类型" );
	TextRender_string24(ListView.ColLeft[6],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[7],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[8],aTop,TextColor, "区域" );
	TextRender_string24(ListView.ColLeft[9],aTop,TextColor, "区域" );	
	
	aCount = Loaded2.Count;
	aIdx = Level1.SelectedIndex;
	aCh = Level2.TopIndex;
	if(1){
		for(i=0; i<ListLineCnt; i++){
			if(aCh >= aCount)return;
			if(1){
				if((IsEditing) && (PrmEdit.SelectedCol == i) ){
					aTop = ListView.ColTop[i] + TextTopOffSet;
					
					if(PrmEdit.SelectedRow == 0)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					aType = ValueList[aIdx][aCh].eType;
					if(aType >30)aType =30;
					if(Loaded1.ChannelInCount)
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_iType[aType]);
					else
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_oType[aType]);
	
					if(PrmEdit.SelectedRow == 1)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,aClr, ValueList[aIdx][aCh].Group0 );
						
					if(PrmEdit.SelectedRow == 2)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,aClr,  ValueList[aIdx][aCh].Group1);
						
					if(PrmEdit.SelectedRow == 3)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,aClr,  ValueList[aIdx][aCh].Group2 );
					
					if(PrmEdit.SelectedRow == 4)	aClr = clNearWhite; 
					else aClr = clNearBlack;
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,aClr,  ValueList[aIdx][aCh].Group3 );

				}else{
					aTop = ListView.ColTop[i] + TextTopOffSet;
					if(Loaded1.ChannelInCount)
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_iType[ValueList[aIdx][aCh].eType]);
					else
						TextRender_string24(ListView.ColLeft[5],aTop,aClr, StrAssign_oType[ValueList[aIdx][aCh].eType]);
					TextDigitRender3Right24(ListView.ColLeft[6],aTop,clNearBlack,  ValueList[aIdx][aCh].Group0 );
					TextDigitRender3Right24(ListView.ColLeft[7],aTop,clNearBlack,  ValueList[aIdx][aCh].Group1 );
					TextDigitRender3Right24(ListView.ColLeft[8],aTop,clNearBlack,  ValueList[aIdx][aCh].Group2);
					TextDigitRender3Right24(ListView.ColLeft[9],aTop,clNearBlack,  ValueList[aIdx][aCh].Group3);
				}
			}
			aCh++;
		}
	}
}
//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TIoPadDefine::EpKeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	if(aKey == VK_LEFT){
		if(IsEditing){
			if(PrmEdit.SelectedRow>0){
				PrmEdit.SelectedRow--;
				if(CtlLevel == 1){
					RenderEpPrm();
				}else
				if(CtlLevel == 2){
					RenderEpChannelPrm();
				}
			}else{
				IsEditing = 0;
				IsModifing = 0;
				if(CtlLevel == 1){
					RenderEp();
					RenderEpPrm();
				}else
				if(CtlLevel == 2){
					RenderEpChannel();
					RenderEpChannelPrm();
				}
			}
		}else{
			if(CtlLevel >0){
				if(CtlLevel == 1){
					CtlLevel = 0;
					RenderPort();
					RenderEp();
					RenderEpChannel();
					RenderEpChannelPrm();
					IsWarningSave =1;
					RenderWarningSave();
				}else{
					CtlLevel--;
					if(CtlLevel == 0){

					}else
					if(CtlLevel == 1){
						LoadEp();
						RenderEp();
						RenderEpPrm();
					}else
					if(CtlLevel == 2){
						RenderEpChannel();
						RenderEpChannelPrm();
					}else{
						CtlLevel =0;
						LoadPort();
						RenderPort();
						RenderEp();
						RenderEpChannel();
						RenderEpChannelPrm();
					}
				}
			}
		}
	}else
	if(aKey == VK_RIGHT){
		if(IsEditing){
			if(PrmEdit.SelectedRow<4){
				PrmEdit.SelectedRow++;
				if(CtlLevel == 1){
					RenderEp();
					RenderEpPrm();
				}else
				if(CtlLevel == 2){
					RenderEpChannel();
					RenderEpChannelPrm();
				}
			}
		}else{
			if(CtlLevel == 0){
				CtlLevel++;
				LoadEp();
				LoadEpChannel();
				RenderPort();
				RenderEp();
				RenderEpPrm();
			}else
			if(CtlLevel == 1){
				aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
				aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
				if(aP0 && aP1 ){
					aP0--;
					aP1--;
					Level1.CurrentType = EpDev[aP0][aP1].fData.Split.dType;
					if( Loaded1.ChannelCount[Level1.SelectedIndex] >1 ){
						CtlLevel ++;
						LoadEpChannel();
						RenderEp();
						Level2.TopIndex = 0;
						Level2.SelectedIndex = 0;
						Level2.SelectedCol = 0;
						RenderEpChannel();
						RenderEpChannelPrm();
					}else{
						IsEditing = 1;
						PrmEdit.SelectedCol = Level1.SelectedCol;
						PrmEdit.SelectedRow = 0;
						RenderEp();
						RenderEpPrm();
					}
				}
			}else
			if(CtlLevel == 2){
				IsEditing = 1;
				PrmEdit.SelectedCol = Level2.SelectedCol;
				PrmEdit.SelectedRow = 0;
				RenderEp();
				RenderEpChannel();
				RenderEpChannelPrm();
			}
		}			
	}else
	if(aKey == VK_UP){
		if(IsEditing){
			if(IsModifing){
				if(CtlLevel == 1){
					y = Level1.SelectedIndex;
					aCh = 0;
				}else
				if(CtlLevel == 2){
					y = Level1.SelectedIndex;
					aCh = Level2.SelectedIndex;
				}
				x = PrmEdit.SelectedRow;
				if(x==0){
					if(ValueList[y][aCh].eType < 30)ValueList[y][aCh].eType++;
				}else
				if(x==1){
					ValueList[y][aCh].Group0++;
				}else
				if(x==2){
					ValueList[y][aCh].Group1++;
				}else
				if(x==3){
					ValueList[y][aCh].Group2++;
				}else
				if(x==4){
					ValueList[y][aCh].Group3++;
				}
				if(CtlLevel == 1){
					RenderEpPrm();
				}else
				if(CtlLevel == 2){
					RenderEpChannelPrm();
				}
			}else{
				if(CtlLevel ==1){
					if(Level1.SelectedCol >0){
						Level1.SelectedCol--;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						PrmEdit.TopIndex = Level1.TopIndex;
						PrmEdit.SelectedIndex = Level1.SelectedIndex;
						PrmEdit.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}else{
						if(Level1.TopIndex >0){
							Level1.TopIndex --;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}
					if(Updata){
						RenderEp();
						RenderEpPrm();
					}
				}else
				if(CtlLevel ==2){
					if(Level2.SelectedCol >0){
						Level2.SelectedCol--;
						Level2.SelectedIndex--;
						RenderEpChannel();
						PrmEdit.SelectedCol =  Level2.SelectedCol;
						PrmEdit.SelectedIndex =  Level2.SelectedIndex;
						RenderEpChannelPrm();
					}
				}
			}
		}else{
			if(CtlLevel == 0){
				//ListLineCnt
				if(Level0.SelectedCol >0){
					Level0.SelectedCol--;
					Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
					Level1.SelectedCol =0;
					Level1.SelectedIndex = 0;
					Level1.SelectedIndex = 0;	
					Level2.SelectedCol =0;
					Level2.SelectedIndex = 0;
					Level2.SelectedIndex = 0;
					LoadEp();
					LoadEpChannel();
					RenderPort();
					RenderEp();
					RenderEpPrm();
					
				}else{
					if(Level0.TopIndex >0){
						Level0.TopIndex --;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadEp();
						LoadEpChannel();
						RenderPort();
						RenderEp();
						RenderEpPrm();
					}
				}
			}else
			if(CtlLevel == 1){
				if(Level1.SelectedCol >0){
					Level1.SelectedCol--;
					Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
					Level2.TopIndex = Level1.TopIndex;
					Level2.SelectedIndex = Level1.SelectedIndex;
					Level2.SelectedCol = Level1.SelectedCol;
					
					aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
					aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
					Updata = 1;
				}else{
					if(Level1.TopIndex >0){
						Level1.TopIndex --;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}
				if(Updata){
					RenderEp();
					RenderEpPrm();
				}
			}else
			if(CtlLevel == 2){
				if(Level2.SelectedCol >0){
					Level2.SelectedCol--;
					Level2.SelectedIndex--;
					RenderEpChannel();
				}
			}
		}
	}else
	if(aKey == VK_DOWN){
		if(IsEditing){
			if(IsModifing){
				if(CtlLevel == 1){
					y = Level1.SelectedIndex;
					aCh = 0;
				}else
				if(CtlLevel == 2){
					y = Level1.SelectedIndex;
					aCh = Level2.SelectedIndex;
				}
				x = PrmEdit.SelectedRow;
				if(x==0){
					if(ValueList[y][aCh].eType >0)ValueList[y][aCh].eType--;
				}else
				if(x==1){
					ValueList[y][aCh].Group0--;
				}else
				if(x==2){
					ValueList[y][aCh].Group1--;
				}else
				if(x==3){
					ValueList[y][aCh].Group2--;
				}else
				if(x==4){
					ValueList[y][aCh].Group3--;
				}
				if(CtlLevel == 1){
					RenderEpPrm();
				}else
				if(CtlLevel == 2){
					RenderEpChannelPrm();
				}
			}else{
				if(CtlLevel == 1){
					if(Level1.SelectedCol < (ListLineCnt-1)){
						if( (Level1.TopIndex + Level1.SelectedCol) < (Loaded1.Count -1) ){
							Level1.SelectedCol++;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}else{
						if( (Level1.TopIndex + ListLineCnt) < Loaded1.Count){
							Level1.TopIndex ++;
							Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
							Level2.TopIndex = Level1.TopIndex;
							Level2.SelectedIndex = Level1.SelectedIndex;
							Level2.SelectedCol = Level1.SelectedCol;
							PrmEdit.TopIndex = Level1.TopIndex;
							PrmEdit.SelectedIndex = Level1.SelectedIndex;
							PrmEdit.SelectedCol = Level1.SelectedCol;
							aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
							aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
							Updata = 1;
						}
					}
					if(Updata){
						RenderEp();
						RenderEpPrm();
					}					
				}else
				if(CtlLevel == 2){
					if( (Level2.SelectedCol < (Loaded2.Count-1)) && (Level2.SelectedCol < 3) ){
						Level2.SelectedCol++;
						Level2.SelectedIndex++;
						RenderEpChannel();
						PrmEdit.SelectedCol =  Level2.SelectedCol;
						PrmEdit.SelectedIndex =  Level2.SelectedIndex;
						RenderEpChannelPrm();
					}
				}
			}
		}else{
			if(CtlLevel == 0){
				//ListLineCnt
				if(Level0.SelectedCol < (ListLineCnt-1)){
					if( (Level0.TopIndex + Level0.SelectedCol) < (Loaded0.Count -1) ){
						Level0.SelectedCol++;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadEp();
						RenderPort();
						RenderEp();
						RenderEpPrm();
					}
				}else{
					if( (Level0.TopIndex + ListLineCnt) < Loaded0.Count){
						Level0.TopIndex ++;
						Level0.SelectedIndex = Level0.TopIndex + Level0.SelectedCol;
						Level1.SelectedCol =0;
						Level1.SelectedIndex = 0;
						Level1.SelectedIndex = 0;
						Level2.SelectedCol =0;
						Level2.SelectedIndex = 0;
						Level2.SelectedIndex = 0;
						LoadEp();
						RenderPort();
						RenderEp();
						RenderEpPrm();
					}
				}
			}else
			if(CtlLevel == 1){
				if(Level1.SelectedCol < (ListLineCnt-1)){
					if( (Level1.TopIndex + Level1.SelectedCol) < (Loaded1.Count -1) ){
						Level1.SelectedCol++;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}else{
					if( (Level1.TopIndex + ListLineCnt) < Loaded1.Count){
						Level1.TopIndex ++;
						Level1.SelectedIndex = Level1.TopIndex + Level1.SelectedCol;
						Level2.TopIndex = Level1.TopIndex;
						Level2.SelectedIndex = Level1.SelectedIndex;
						Level2.SelectedCol = Level1.SelectedCol;
						aP0 = Loaded0.ItemsPath[Level0.SelectedIndex];
						aP1 =  Loaded1.ItemsPath[Level1.SelectedIndex];
						Updata = 1;
					}
				}
				if(Updata){
					RenderEp();
					RenderEpPrm();
				}
			}
			if(CtlLevel == 2){
				if( (Level2.SelectedCol < (Loaded2.Count-1)) && (Level2.SelectedCol < 3) ){
					Level2.SelectedCol++;
					Level2.SelectedIndex++;
					RenderEpChannel();
				}
			}
		}
	}else
	if(aKey == VK_F1){
		//Go Modify
		if(IsEditing){
			if(IsModifing)IsModifing =0; else IsModifing = 1;
			if(CtlLevel == 1){
				RenderEpPrm();
			}else
			if(CtlLevel == 2){
				RenderEpChannelPrm();
			}
		}
	}else
	if(aKey == VK_TAB){
		if(IsWarningSave){
			//User Not Save Here
			IsWarningSave = 0;
			CtlLevel = 0;
			RenderPort();
			RenderEp();
		}
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(IsWarningSave){
			//Save Here
			if(IsReqReturn){
				//Save and Return
				
				aMsg = guiMsgReturn;
			}else{
				IsWarningSave = 0;
				CtlLevel = 0;
				RenderPort();
				RenderEp();
				if(CtlLevel == 2){
					RenderEpChannel();
					RenderEpChannelPrm();
				}else{
					RenderEpPrm();
				}
			}
		}else{
			//Ask if Save Here
			IsWarningSave = 1;
			RenderWarningSave();
		}
	}else
	if(aKey == VK_RETURN){
		if(IsWarningSave == 0){
			IsWarningSave = 1;
			RenderWarningSave();
			IsReqReturn = 1;
		}else{
			IsWarningSave = 0;
			if(IsReqReturn){
				aMsg = guiMsgReturn;
			}else{
				if(CtlLevel == 2){
					RenderEpChannel();
					RenderEpChannelPrm();
				}else{
					RenderEp();
					RenderEpPrm();
				}
			}
		}
	}
	return aMsg;	
}

TGuiMsgReturn TIoPadDefine::KeyIn(unsigned char aKey)
{
	int ind,i,aIndex;
	TGuiMsgReturn aMsg = guiMsgNone;
	//Up or Down One Index___________________
	
	if(ParentType == 0){
		//dgTYPE_HAND_CTL
		aMsg = HandCtlKeyIn(aKey);			
	}else
	if(ParentType == 1){
		//dgTYPE_DIRECT_CTL
		aMsg = DirectCtlKeyIn(aKey);
	}else
	if(ParentType == 2){
		//EpDev 
		aMsg = EpKeyIn(aKey);
	}
	
	if(aKey == VK_EXECUTE){
		//Open path Device

	}else
	if(aKey == VK_F2){
		
	}else
	if(aKey == VK_RETURN){

	}	

	return aMsg;	
}















