#include "UserSet.h"

#include "Runtime.h"

#define TextTopOffSet	6
#define ListLineCnt	10

#define dAddrSdram_UserCodeP0	(dAddrSdRam_Commom)
#define dAddrSdram_UserCodeP1	(dAddrSdRam_Commom + 2048)
#define dAddrSdram_DescpBufP0	(dAddrSdRam_Commom + 4096)
#define dAddrSdram_DescpBufP1	(dAddrSdRam_Commom + 20480)

void TUserSet::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);
	Content.Set(Left, Top+CH, Right, Bottom);
	SText.Init	(519,		Top,		280,   CH, 0, 0, clWhite, clMaroon);
	VScrollBarBox.Set(Right-20 +1, Top+CH, Right, Bottom);
	Color = aColor;
	#undef CH

	ActiveLineNum=0;
	Load0.Count=0;
	Load1.SelectedIndex=0;
	
	Lv.ColHeight = 33;
	Lv.TopIndex=0;
	Lv.RowCount = 9;
	Lv.FixColTop = Content.Top;
	Lv.FixColBottom = Lv.FixColTop + Lv.ColHeight;
	Lv.ColTop[0] = Lv.FixColBottom + 1;
	Lv.ColBottom[0] = Lv.ColTop[0] + Lv.ColHeight -1;
	Lv.ColLeft[0] = 1;
	Lv.ColRight[0] = Lv.ColWidth[0] +1;
	for(i=1; i<12; i++){
		Lv.ColTop[i] = Lv.ColTop[i -1] + Lv.ColHeight;
		Lv.ColBottom[i] =  Lv.ColBottom[i-1] + Lv.ColHeight; 
	}
	for(i=1;i<Lv.RowCount;i++){
		Lv.ColLeft[i] = Lv.ColWidth[i-1] + Lv.ColLeft[i-1];
		Lv.ColRight[i] = Lv.ColWidth[i] + Lv.ColLeft[i];
	}

	ViewPath = 0;
	GridX =0;
	IsEditUsercode =0;
	IsEditDescp = 0;
	AskingSave = 0;
	
	Caption.Border.Color = clGray;
	VScrollBar.Color = clFrmFace;
	VScrollBar.RibbonColor =  clGray;
	
	FixColColor = clMaroon;
	FixColTextColor = clNearWhite;
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	Load0.SelectedIndex  = 0;
	Load1.SelectedIndex  = 0;
	Lv.TopIndex = 0;
	
	vEdit.Init(0,0,2,2,0,0,1,clFrmFace);
}

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

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

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

void TUserSet::RenderContent(void)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	unsigned int x,y, y2, clr;
	RectFillRender(Content.Left, Lv.FixColTop, Content.Right, Lv.FixColBottom, FixColColor);
	y = Lv.FixColBottom + 1;
	for(x=0; x<(ListLineCnt); x++){	
		y2 =  y + Lv.ColHeight - 1;
		if(y2 > Content.Bottom)y2 = Content.Bottom;		
		if(x == Lv.SelectedCol){
				RectFillRender(Content.Left, y, Content.Right, y2, SelectedColor);			
		}else{
			if(x & 1){
				RectFillRender(Content.Left, y, Content.Right, y2, OddColor);
			}else{
				RectFillRender(Content.Left, y, Content.Right, y2, Color);
			}							
		}
		y = y2 + 1;
		if(y>Content.Bottom)break;
	}	
}

void TUserSet::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);
	
	RenderContent();
}

void TUserSet::DrawCaption(void)
{	
	Caption.Show();
	if(ViewPath == 0){
		DrawCaption1();
	}else{
		DrawCaption2();
	}
}

void TUserSet::DrawCaption1(void)
{	
	if(IsProgram){
		TMyString::sFromStr("按查询键进入子部件设置", SText.Text);
	}else{
		TMyString::sFromStr("  按查询键进入子部件  ", SText.Text);
	}
	SText.Show();
}

void TUserSet::DrawCaption2(void)
{	
	if(IsProgram){
		TMyString::sFromStr("按确定键确认保存", SText.Text);
	}else{
		TMyString::sFromStr("                ", SText.Text);
	}
	SText.Show();
}

void TUserSet::LoadTextToTemp(unsigned int aAddr)
{
	int i;
	for(i=0; i<32; i++){
		CommomGuiTempText[i] = *(volatile unsigned char *)(aAddr + i);
	}
	CommomGuiTempText[32]='\0';
}

void TUserSet::DrawList(void)
{
	int i,j, aTop, aImg;
	unsigned int idx, addr, aP0, aP1, aCode;
	unsigned int aClr;
	unsigned char aType;
	if(1){
		RenderContent();		
		aTop = Lv.FixColTop + TextTopOffSet;
		if(ViewPath == 0){
			if(Load0.Count <1)return;
			SetListColWidth(30,80,60,80,500,1,1,1,1);
			TextRender_string24(Lv.ColLeft[1],aTop,TextColor, "类型" );	
			TextRender_string24(Lv.ColLeft[2],aTop,TextColor, "序号" );
			TextRender_string24(Lv.ColLeft[3],aTop,TextColor, " 用户码");			
			TextRender_string24(Lv.ColLeft[4],aTop,TextColor, "   描述");
			idx = Lv.TopIndex ;
			for(i=0; i<ListLineCnt; i++){
				if(idx < Load0.Count){
					aTop = Lv.ColTop[i] + TextTopOffSet;
					aP0 = Load0.addr[idx];
					if(i == Lv.SelectedCol){
						aClr = SelectedTextColor;
					}else{	
						aClr = TextColor;							
					}
					if(aP0){
						addr = aP0;
						if(aP0<41){
							aP0 &= 0x000000FF;
							aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP0 + (aP0-1) * 4);
							LoadTextToTemp(dAddrSdram_DescpBufP0 + (aP0-1) * 32);
							aP0 = aP0-1;
							ImageList.Render(24,    dgTYPE_ROOT_PORT, Lv.ColLeft[0], aTop);
							TextRender_string24	(Lv.ColLeft[1],  	aTop,  	aClr, StrRootType[dgTYPE_ROOT_PORT]);
							TextDigitRender2Right24	(Lv.ColLeft[2],  	aTop,  	aClr, aP0+1);
							
							TextDigitRender6Right24	(Lv.ColLeft[3],  	aTop,  	aClr, aCode);
							TextRender_string24	(Lv.ColLeft[4],  	aTop,  	aClr, CommomGuiTempText);
						}else
						if(aP0<49){
							aP0 &= 0x000000FF;
							aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP0 + (aP0-1) * 4);
							LoadTextToTemp(dAddrSdram_DescpBufP0 + (aP0-1) * 32);
							aP0 = aP0 -40-1;
							ImageList.Render(24,    dgTYPE_ROOT_HAND_CTL,      Lv.ColLeft[0], aTop);
							TextRender_string24	(Lv.ColLeft[1],  	aTop,  aClr, StrRootType[dgTYPE_ROOT_HAND_CTL]);
							TextDigitRender2Right24	(Lv.ColLeft[2],  	aTop,  aClr, aP0+1);
							TextDigitRender6Right24	(Lv.ColLeft[3],  	aTop,  aClr, aCode);
							TextRender_string24	(Lv.ColLeft[4],  	aTop,  aClr, CommomGuiTempText);
						}else
						if(aP0<57){
							aP0 &= 0x000000FF;
							aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP0 + (aP0-1) * 4);
							LoadTextToTemp(dAddrSdram_DescpBufP0 + (aP0-1) * 32);
							aP0 = aP0 -48-1;
							ImageList.Render(24,    dgTYPE_ROOT_DIRECT_CTL,  Lv.ColLeft[0], aTop);
							TextRender_string24	(Lv.ColLeft[1],  	aTop,  aClr, StrRootType[dgTYPE_ROOT_DIRECT_CTL]);
							TextDigitRender2Right24	(Lv.ColLeft[2], 	aTop,  aClr, aP0+1);
							TextDigitRender6Right24	(Lv.ColLeft[3],  	aTop,  aClr, aCode);
							TextRender_string24	(Lv.ColLeft[4],  	aTop,  aClr, CommomGuiTempText);
						}
					}else{
						/*addr = aP0;
						ImageList.Render(24,    0,      Lv.ColLeft[0], aTop);
						TextRender_string24	(Lv.ColLeft[1],  aTop,  aClr, "本机");
						TextRender_string24	(Lv.ColLeft[2],  aTop,  aClr, "  ");
						TextDigitRender6Right24	(Lv.ColLeft[3],  aTop,  aClr, UserCodeBuf.P0[addr]);
						TextRender_string24	(Lv.ColLeft[4],  aTop,  aClr, DescpBuf.P0[addr]);*/
					}
				}
				idx++;
			}
		}else
		if(ViewPath == 1){
			if(Load1.Count <1)return;
			if(Load1.P0 ==0) return;
			SetListColWidth(30,160,50,80,474,1,1,1,1);
			TextRender_string24(Lv.ColLeft[1],aTop,TextColor, " 类型" );
			
			TextRender_string24(Lv.ColLeft[3],aTop,TextColor, " 用户码");
			TextRender_string24(Lv.ColLeft[4],aTop,TextColor, "   描述");
			if(Load1.P0 < 41){
				TextRender_string24(Lv.ColLeft[2],aTop,TextColor, "地址");
				aP0 = Load1.P0 -1;
				idx = Lv.TopIndex;
				for(i=0; i<ListLineCnt; i++){
					aP1 = Load1.addr[idx];
					if(aP1 ==0)break;
					aP1--;
					if(idx <= Load1.Count){
						aTop = Lv.ColTop[i] + TextTopOffSet;
						aType = EpDev[aP0][aP1].fData.Split.dType;
						ImageList.Render(24,    aType,      Lv.ColLeft[0], aTop);
						if(i == Lv.SelectedCol){
							aClr = SelectedTextColor;
						}else{	
							aClr = TextColor;							
						}
						TextRender_string24(Lv.ColLeft[1], 		aTop,  aClr, StrType[aType]);
						TextDigitRender3Right24(Lv.ColLeft[2],  	aTop,  aClr, aP1+1);
						aP1 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP1 + aP1 * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  	aTop,  aClr, aCode);
						LoadTextToTemp(dAddrSdram_DescpBufP1 + aP1 * 32);
						TextRender_string24(Lv.ColLeft[4],  	aTop,  aClr, CommomGuiTempText);
					}else{
						break;
					}
					idx++;
				}
			}else
			if(Load1.P0 < 49){
				TextRender_string24(Lv.ColLeft[2],aTop,TextColor, "键号");
				aP0 = Load1.P0 -40 -1;
				idx = Lv.TopIndex;
				for(i=0; i<ListLineCnt; i++){
					aP1 = Load1.addr[idx];
					if(aP1 ==0)break;
					aP1--;
					if(idx <= Load1.Count){
						aTop = Lv.ColTop[i] + TextTopOffSet;
						aType = dgTYPE_HAND_CTL_CHANNEL;
						ImageList.Render(24,    aType,      Lv.ColLeft[0], aTop);
						if(i == Lv.SelectedCol){
							aClr = SelectedTextColor;
						}else{	
							aClr = TextColor;							
						}
						TextRender_string24(Lv.ColLeft[1], 		aTop,  aClr, "按键");
						TextDigitRender3Right24(Lv.ColLeft[2],  	aTop,  aClr, aP1+1);
						aP1 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP1 + aP1 * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  	aTop,  aClr, aCode);
						LoadTextToTemp(dAddrSdram_DescpBufP1 + aP1 * 32);
						TextRender_string24(Lv.ColLeft[4],  	aTop,  aClr, CommomGuiTempText);
					}else{
						break;
					}
					idx++;
				}
			}else
			if(Load1.P0 <57){
				TextRender_string24(Lv.ColLeft[2],aTop,TextColor, "键号");
				aP0 = Load1.P0 -48 -1;
				idx = Lv.TopIndex;
				for(i=0; i<ListLineCnt; i++){
					aP1 = Load1.addr[idx];
					if(aP1 ==0)break;
					aP1--;
					if(idx <= Load1.Count){
						aTop = Lv.ColTop[i] + TextTopOffSet;
						aType = dgTYPE_DIRECT_CTL_CHANNEL;
						ImageList.Render(24,    aType,      Lv.ColLeft[0], aTop);
						if(i == Lv.SelectedCol){
							aClr = SelectedTextColor;
						}else{	
							aClr = TextColor;							
						}
						TextRender_string24(Lv.ColLeft[1], 		aTop,  aClr, "按键");
						TextDigitRender3Right24(Lv.ColLeft[2],  	aTop,  aClr, aP1+1);
						aP1 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP1 + aP1 * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  	aTop,  aClr, aCode);
						LoadTextToTemp(dAddrSdram_DescpBufP1 + aP1 * 32);
						TextRender_string24(Lv.ColLeft[4],  	aTop,  aClr, CommomGuiTempText);
					}else{
						break;
					}
					idx++;
				}	
			}
		}
	}
}

void TUserSet::DrawValueP0(void)
{
	int i,j, aTop, aImg;
	unsigned int idx, aP0, aP1, aCode;
	unsigned int aClr;
	unsigned char aType;
	RenderContent();		
	aTop = Lv.FixColTop + TextTopOffSet;
	
	if(ViewPath == 0){
		if(Load0.Count <1)return;
		SetListColWidth(30,80,40,400,1,1,1,1,1);
		
		for(i=0; i<ListLineCnt; i++){
			if(i == Lv.SelectedCol){
				RectFillRender(Lv.ColLeft[3], Lv.ColTop[i], Lv.ColRight[3], Lv.ColBottom[i], SelectedColor);
			}else{	
				if(i & 1){
					RectFillRender(Lv.ColLeft[3], Lv.ColTop[i], Lv.ColRight[3], Lv.ColBottom[i],  OddColor);
				}else{
					RectFillRender(Lv.ColLeft[3], Lv.ColTop[i], Lv.ColRight[3], Lv.ColBottom[i],  Color);
				}						
			}
		}
		
		idx = Lv.TopIndex ;
		for(i=0; i<ListLineCnt; i++){
			if(idx < Load0.Count){
				aTop = Lv.ColTop[i] + TextTopOffSet;
				aP0 = Load0.addr[idx];
				if(i == Lv.SelectedCol){
					aClr = SelectedTextColor;
				}else{	
					aClr = TextColor;							
				}
				if(aP0 < 65){
					if(aP0<41){
						aP0 = aP0-1;
						LoadTextToTemp(dAddrSdram_DescpBufP0 + aP0 * 32);
						aP0 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP0 + aP0 * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  aTop,  TextColor, aCode);
						TextRender_string24(Lv.ColLeft[4],  aTop,  aClr, CommomGuiTempText);
					}else
					if(aP0<49){
						aP0 = aP0-1;
						LoadTextToTemp(dAddrSdram_DescpBufP0 + aP0 * 32);
						aP0 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP0 + aP0 * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  aTop,  TextColor, aCode);
						TextRender_string24(Lv.ColLeft[4],  aTop,  aClr, CommomGuiTempText);
					}else
					if(aP0<57){
						aP0 = aP0-1;
						LoadTextToTemp(dAddrSdram_DescpBufP0 + aP0 * 32);
						aP0 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP0 + aP0 * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  aTop,  TextColor, aCode);
						TextRender_string24(Lv.ColLeft[4],  aTop,  aClr, CommomGuiTempText);
					}
				}else{
					break;
				}
			}
			idx++;
		}
	}
}

void TUserSet::DrawValueP1(void)
{
	int i,j, aTop, aImg;
	unsigned int idx, aP0, aP1, aCode;
	unsigned int aClr;
	unsigned char aType;
	unsigned char aLoadInx;
	if(1){
		RenderContent();		
		aTop = Lv.FixColTop + TextTopOffSet;
		
		if(ViewPath == 1){
			if(Load1.Count <1)return;
			if(Load1.P0 ==0) return;
			
			for(i=0; i<ListLineCnt; i++){
				if(i == Lv.SelectedCol){
					RectFillRender(Lv.ColLeft[3], Lv.ColTop[i], Lv.ColRight[4], Lv.ColBottom[i], SelectedColor);
					aClr = SelectedTextColor;
				}else{	
					if(i & 1){
						RectFillRender(Lv.ColLeft[3], Lv.ColTop[i], Lv.ColRight[4], Lv.ColBottom[i],  OddColor);
					}else{
						RectFillRender(Lv.ColLeft[3], Lv.ColTop[i], Lv.ColRight[4], Lv.ColBottom[i],  Color);
					}
					aClr = TextColor;							
				}
			}
			if(Load1.P0 < 1){
				
			}else
			if(Load1.P0 < 41){
				aP0 = Load1.P0 -1;
				idx = Lv.TopIndex;
				for(i=0; i<ListLineCnt; i++){
					aP1 = Load1.addr[idx];
					if(aP1 ==0)break;
					aLoadInx = aP1 -1;
					aP1--;
					if(idx <= Load1.Count){
						aTop = Lv.ColTop[i] + TextTopOffSet;
						aType = EpDev[aP0][aP1].fData.Split.dType;
						ImageList.Render(24,    aType,      Lv.ColLeft[0], aTop);
						if(i == Lv.SelectedCol){
							aClr = SelectedTextColor;
						}else{	
							aClr = TextColor;							
						}
						TextRender_string24(Lv.ColLeft[1], aTop, SelectedTextColor, StrType[aType]);
						TextDigitRender2Right24(Lv.ColLeft[2],  aTop,  TextColor, aP1+1);
						aP1 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP1 + aLoadInx * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  aTop,  TextColor, aCode);
						LoadTextToTemp(dAddrSdram_DescpBufP1 + aLoadInx * 32);
						TextRender_string24(Lv.ColLeft[4],  aTop,  TextColor, CommomGuiTempText);
					}else{
						break;
					}
					idx++;
				}
			}else
			if(Load1.P0 < 49){
				aP0 = Load1.P0 -40 -1;
				idx = Lv.TopIndex;
				for(i=0; i<ListLineCnt; i++){
					aP1 = Load1.addr[idx];
					if(aP1 ==0)break;
					aLoadInx = aP1 -1;
					aP1--;
					if(idx <= Load1.Count){
						aTop = Lv.ColTop[i] + TextTopOffSet;
						aType = dgTYPE_HAND_CTL_CHANNEL;
						ImageList.Render(24,    aType,      Lv.ColLeft[0], aTop);
						if(i == Lv.SelectedCol){
							aClr = SelectedTextColor;
						}else{	
							aClr = TextColor;							
						}
						TextRender_string24(Lv.ColLeft[1], aTop, SelectedTextColor, StrType[aType]);
						TextDigitRender2Right24(Lv.ColLeft[2],  aTop,  TextColor, aP1+1);
						aP1 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP1 + aLoadInx * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  aTop,  TextColor, aCode);
						LoadTextToTemp(dAddrSdram_DescpBufP1 + aLoadInx * 32);
						TextRender_string24(Lv.ColLeft[4],  aTop,  TextColor, CommomGuiTempText);
					}else{
						break;
					}
					idx++;
				}
			}else
			if(Load1.P0 <57){
				aP0 = Load1.P0 -48 -1;
				idx = Lv.TopIndex;
				for(i=0; i<ListLineCnt; i++){
					aP1 = Load1.addr[idx];
					if(aP1 ==0)break;
					aLoadInx = aP1 -1;
					aP1--;
					if(idx <= Load1.Count){
						aTop = Lv.ColTop[i] + TextTopOffSet;
						aType = dgTYPE_DIRECT_CTL_CHANNEL;
						ImageList.Render(24,    aType,      Lv.ColLeft[0], aTop);
						if(i == Lv.SelectedCol){
							aClr = SelectedTextColor;
						}else{	
							aClr = TextColor;							
						}
						TextRender_string24(Lv.ColLeft[1], aTop, SelectedTextColor, StrType[aType]);
						TextDigitRender2Right24(Lv.ColLeft[2],  aTop,  TextColor, aP1+1);
						aP1 &= 0x000000FF;
						aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP1 + aLoadInx * 4);
						TextDigitRender6Right24(Lv.ColLeft[3],  aTop,  TextColor, aCode);
						LoadTextToTemp(dAddrSdram_DescpBufP1 + aLoadInx * 32);
						TextRender_string24(Lv.ColLeft[4],  aTop,  TextColor, CommomGuiTempText);
					}else{
						break;
					}
					idx++;
				}	
			}
		}
	}
}


void TUserSet::Show(void)
{
	DrawSelf();
	Caption.Enable = 1;
	DrawCaption();
	DrawList();	
}

void TUserSet::ReDraw(void)
{
	Caption.Enable = 1;
	DrawCaption();
	DrawList();	
}

void TUserSet::FullRedraw(int Prm)
{
	if(Prm == 0){
		DrawSelf();
		Caption.Enable = 1;
		DrawCaption();
		DrawList();	
		
		if(OnEditing){
			if(ViewPath == 0){
				vEdit.Show();
				vEdit.ShowCursor(clNearWhite);
			}else
			if(ViewPath == 1){
				vEdit.Show();
				vEdit.ShowCursor(clNearWhite);
			}
		}
		
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		Caption.Enable = 1;
		DrawCaption();
		DrawList();
		
		if(OnEditing){
			if(ViewPath == 0){
				vEdit.Show();
				vEdit.ShowCursor(clNearWhite);
			}else
			if(ViewPath == 1){
				vEdit.Show();
				vEdit.ShowCursor(clNearWhite);
			}
		}
	}
}

void TUserSet::DataInit()
{
	unsigned int aAddr,i,m;
	aAddr = dAddrSdram_UserCodeP0;
	for(i=0; i<256; i++){
		*(volatile unsigned int *)(aAddr + i * 4) = 0;
	}
	aAddr = dAddrSdram_UserCodeP1;
	for(i=0; i<256; i++){
		*(volatile unsigned int *)(aAddr + i * 4) = 0;
	}
	aAddr = dAddrSdram_DescpBufP0;
	for(i=0; i<256; i++){
		for(m=0;m<32;m++){
			*(volatile unsigned char *)(aAddr + i * 32 + m) = 0;
		}
						*(volatile unsigned char *)(aAddr + i * 32 + 0) = (i / 10) + '0';
						*(volatile unsigned char *)(aAddr + i * 32 + 1) = (i % 10) + '0';
	}
	aAddr = dAddrSdram_DescpBufP1;
	for(i=0; i<256; i++){
		for(m=0;m<32;m++){
			*(volatile unsigned char *)(aAddr + i * 32 + m) = 0;
		}
						*(volatile unsigned char *)(aAddr + i * 32 + 0) = (i / 10) + '0';
						*(volatile unsigned char *)(aAddr + i * 32 + 1) = (i % 10) + '0';
	}
}

void TUserSet::DataLoad()
{
	
}

void TUserSet::SetEditStart(void)
{
	int x,y,w,h;
	unsigned int aCode;
	if(ViewPath == 0){
		if(IsEditUsercode){
			OnEditing = 1;
			EditUsercodeIndex = Load0.addr[Load0.SelectedIndex] -1;
			x = Lv.ColLeft[3];
			y = Lv.ColTop[Lv.SelectedCol];
			w = Lv.ColWidth[3];
			h = Lv.ColHeight;
			vEdit.Init(x,y,w,h,0,0,1,clTeal);
			vEdit.Color = clTeal;
			TMyString::sClear(vEdit.Str.Text);
			vEdit.SetMaxLen(6);
			aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP0 + EditUsercodeIndex * 4);
			vEdit.Str.AddOn6Dg(aCode);
			vEdit.SetPstTail();
			vEdit.Show();
			vEdit.ShowCursor(clNearWhite);			
		}else
		if(IsEditDescp){
			OnEditing = 1;
			EditDescpIndex = Load0.addr[Load0.SelectedIndex] -1;
			x = Lv.ColLeft[4];
			y = Lv.ColTop[Lv.SelectedCol];
			w = Lv.ColWidth[4];
			h = Lv.ColHeight;
			vEdit.Init(x,y,w,h,0,0,1,clTeal);
			vEdit.Color = clTeal;
			vEdit.SetMaxLen(40);
			//vEdit.Str.FromStr(DescpBuf.P0[EditDescpIndex]);
			vEdit.SetPstTail();
			vEdit.Show();
			vEdit.ShowCursor(clNearWhite);
		}
	}else
	if(ViewPath == 1){
		if(IsEditUsercode){
			OnEditing = 1;
			EditUsercodeIndex = Load1.addr[Load1.SelectedIndex] -1;
			x = Lv.ColLeft[3];
			y = Lv.ColTop[Lv.SelectedCol];
			w = Lv.ColWidth[3];
			h = Lv.ColHeight;
			vEdit.Init(x,y,w,h,0,0,1,clTeal);
			vEdit.Color = clTeal;
			TMyString::sClear(vEdit.Str.Text);
			vEdit.SetMaxLen(6);
			aCode = *(volatile unsigned int *)(dAddrSdram_UserCodeP1 + EditUsercodeIndex * 4);
			vEdit.Str.AddOn6Dg(aCode);
			vEdit.SetPstTail();
			vEdit.Show();
			vEdit.ShowCursor(clNearWhite);
		}else
		if(IsEditDescp){
			OnEditing = 1;
			EditDescpIndex = Load1.addr[Load1.SelectedIndex] -1;
			x = Lv.ColLeft[4];
			y = Lv.ColTop[Lv.SelectedCol];
			w = Lv.ColWidth[4];
			h = Lv.ColHeight;
			vEdit.Init(x,y,w,h,0,0,1,clTeal);
			vEdit.Color = clTeal;
			vEdit.SetMaxLen(40);
			//vEdit.Str.FromStr(DescpBuf.P1[EditDescpIndex]);
			vEdit.SetPstTail();
			vEdit.Show();
			vEdit.ShowCursor(clNearWhite);		
		}
	}
}

void TUserSet::RestoreFromEdit(void)
{
	unsigned int aCode;
	if(ViewPath == 0) {
		if(IsEditDescp){
			//TMyString::sFromStr(vEdit.Str.Text, DescpBuf.P0[EditDescpIndex]);
		}
		if(IsEditUsercode){
			*(volatile unsigned int *)(dAddrSdram_UserCodeP0 + EditUsercodeIndex * 4) = TMyString::sToInteger(vEdit.Str.Text);
		}
	}else
	if(ViewPath == 1){
		if(IsEditDescp){
			//TMyString::sFromStr(vEdit.Str.Text, DescpBuf.P1[EditDescpIndex]);
		}
		if(IsEditUsercode){
			*(volatile unsigned int *)(dAddrSdram_UserCodeP1 + EditUsercodeIndex * 4) = TMyString::sToInteger(vEdit.Str.Text);
		}			
	}
}

void TUserSet::ShowIfSave()
{
	if(OnEditing){
		RestoreFromEdit();
		OnEditing = 0;
	}
	MessageBox.Init(0, 36, 800, 406, 0, 0, clMoneyGreen) ;
	MessageBox.SetBtnOkCancel();
	MessageBox.SetReturnValue(guiMsgReturn, guiMsgOkAndReturn, guiMsgReturn, guiMsgReturn);
	MessageBox.Show();
	MessageBox.DoWait4SaveCurrent();
	AskingSave = 1;
}

TGuiMsgReturn TUserSet::KeyIn(unsigned char aKey)
{
	int ind,i,aIndex, x,y,w,h;
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn MBoxMsg = guiMsgNone;
	//Up or Down One Index___________________
	
	if(AskingSave){
		switch(aKey){
			case VK_LEFT:
				MBoxMsg = MessageBox.KeyIn(aKey);
				break;
			case VK_RIGHT:
				MBoxMsg = MessageBox.KeyIn(aKey);
				break;
			case VK_EXECUTE:
				MBoxMsg = MessageBox.KeyIn(aKey);
				if(MBoxMsg == guiMsgOkAndReturn){
					AskingSave = 0;
					ReDraw();
				}else
				if(MBoxMsg == guiMsgReturn){
					AskingSave = 0;
					ReDraw();
				}
				break;
			case VK_RETURN:
				AskingSave = 0;
				ReDraw();
				break;
		}
		return aMsg;
	}
	
	if(OnEditing){
		if(ViewPath == 0){
			switch(aKey){
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case '*':
				case '#':
				case VK_LEFT:
				case VK_RIGHT:
				case VK_DELETE:
					vEdit.KeyIn(aKey);
					vEdit.Show();
					vEdit.ShowCursor(clNearWhite);
					break;
				case VK_UP:
					aIndex = Load0.SelectedIndex -1;
					if(aIndex > -1){
						RestoreFromEdit();
						if(aIndex < Lv.TopIndex){
							Lv.TopIndex = aIndex;
						}else{
							Lv.SelectedCol--;
						}
						Load0.SelectedIndex = aIndex;
						ReDraw();
						SetEditStart();
					}
					break;
				case VK_DOWN:
					aIndex = Load0.SelectedIndex +1;
					if(aIndex < Load0.Count){
						RestoreFromEdit();
						if(aIndex > (Lv.TopIndex + ListLineCnt -1) ){
							Lv.TopIndex++;
						}else{
							Lv.SelectedCol++;
						}
						Load0.SelectedIndex = aIndex;
						ReDraw();
						SetEditStart();
					}
					break;
				case VK_TAB:
					/*if(IsEditUsercode){
						RestoreFromEdit();
						IsEditUsercode = 0;
						IsEditDescp = 1;
						ReDraw();
						SetEditStart();
					}else{
						RestoreFromEdit();
						IsEditUsercode = 1;
						IsEditDescp = 0;
						ReDraw();
						SetEditStart();
					}*/
					break;
				case VK_EXECUTE:
					ShowIfSave();
					break;
			}
		}else
		if(ViewPath == 1){
			switch(aKey){
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case '*':
				case '#':
				case VK_LEFT:
				case VK_RIGHT:
				case VK_DELETE:
					vEdit.KeyIn(aKey);
					vEdit.Show();
					vEdit.ShowCursor(clNearWhite);
					break;
				case VK_UP:
					aIndex = Load1.SelectedIndex -1;
					if(aIndex > -1){
						RestoreFromEdit();
						if(aIndex < Lv.TopIndex){
							Lv.TopIndex = aIndex;
						}else{
							Lv.SelectedCol--;
						}
						Load1.SelectedIndex = aIndex;
						ReDraw();
						SetEditStart();
					}
					break;
				case VK_DOWN:
					aIndex = Load1.SelectedIndex +1;
					if(aIndex < Load1.Count){
						RestoreFromEdit();
						if(aIndex > (Lv.TopIndex + ListLineCnt -1) ){
							Lv.TopIndex++;
						}else{
							Lv.SelectedCol++;
						}
						Load1.SelectedIndex = aIndex;
						ReDraw();
						SetEditStart();
					}
					break;
				case VK_TAB:
					/*if(IsEditUsercode){
						RestoreFromEdit();
						IsEditUsercode = 0;
						IsEditDescp = 1;
						ReDraw();
						SetEditStart();
					}else{
						RestoreFromEdit();
						IsEditUsercode = 1;
						IsEditDescp = 0;
						ReDraw();
						SetEditStart();
					}*/
					break;
				case VK_EXECUTE:
					ShowIfSave();
					break;
			}	
		}
		if(aKey == VK_RETURN){
			RestoreFromEdit();
			OnEditing = 0;
			ReDraw();
		}
	}else{
			if(aKey == VK_TAB){

			}
			if(VK_UP == aKey){
				if(ViewPath ==0){
					aIndex = Load0.SelectedIndex -1;
					if(aIndex > -1){
						if(aIndex < Lv.TopIndex){
							Lv.TopIndex = aIndex;
						}else{
							Lv.SelectedCol--;
						}
						Load0.SelectedIndex = aIndex;
						ReDraw();
					}
				}else
				if(ViewPath ==1){
					aIndex = Load1.SelectedIndex -1;
					if(aIndex > -1){
						if(aIndex < Lv.TopIndex){
							Lv.TopIndex = aIndex;
						}else{
							Lv.SelectedCol--;
						}
						Load1.SelectedIndex = aIndex;
						ReDraw();
					}
				}
			}else
			if(VK_DOWN == aKey){
				if(ViewPath ==0){
					aIndex = Load0.SelectedIndex +1;
					if(aIndex < Load0.Count){
						if(aIndex > (Lv.TopIndex + ListLineCnt -1) ){
							Lv.TopIndex++;
						}else{
							Lv.SelectedCol++;
						}
						Load0.SelectedIndex = aIndex;
						ReDraw();
					}
				}else
				if(ViewPath ==1){
					aIndex = Load1.SelectedIndex +1;
					if(aIndex < Load1.Count){
						if(aIndex > (Lv.TopIndex + ListLineCnt -1) ){
							Lv.TopIndex++;
						}else{
							Lv.SelectedCol++;
						}
						Load1.SelectedIndex = aIndex;
						ReDraw();
					}
				}
			}else
			//Page up or Down____________________________
			
			if(aKey == VK_LEFT){
				if(ViewPath == 0){
					GridX = 0;
				}else
				if(ViewPath == 1){
					GridX = 0;
				}
			}else
			if(aKey == VK_RIGHT){
				if(ViewPath == 0){
					GridX = 0;
				}else
				if(ViewPath == 1){
					GridX = 1;
				}
			}else
			if(aKey == VK_EXECUTE){
				//Open path Device
				if(IsProgram){
					ShowIfSave();
				}
			}else
			if(aKey == VK_F1 ){
				//Setup Device Property
				if(IsProgram){
					if(ViewPath == 0){
						SetEditStart();
					}else
					if(ViewPath == 1){
						SetEditStart();
					}
				}
			}else
			if(aKey == VK_F2){
				if(ViewPath == 0){
					if(Load0.addr[Load0.SelectedIndex] != 0){
						BkP0TopIndex = Lv.TopIndex;
						ClearPath1();
						LoadPath1();
						ViewPath = 1;
						ReDraw();
					}
				}
			}else
			if(aKey == VK_RETURN){
				if(ViewPath == 0){
					aMsg = guiMsgReturn;
				}else
				if(ViewPath == 1){
					ClearPath0();
					LoadPath0();
					Lv.TopIndex = BkP0TopIndex;
					Lv.SelectedCol = Load0.SelectedIndex - Lv.TopIndex;
					ViewPath = 0;
					ReDraw();
				}else
				if(ViewPath > 1){
					ClearPath1();
					LoadPath1();
					ViewPath = 1;
					ReDraw();
				}
			}	
	}
	//____________________________________________________________________<- List View

	return aMsg;

}

void TUserSet::ClearPath0(void)
{
	unsigned int i;
	Load0.Count =0;
	for(i=0;i<64;i++){
		Load0.addr[i] =0;
	}
	Load0.SelectedIndex = 0;
	Lv.TopIndex = 0;
}

void TUserSet::ClearPath1(void)
{
	unsigned int i;
	for(i=0;i<256;i++){
		Load1.addr[i] = 0;
	}
	Load1.Count = 0;
	Load1.SelectedIndex = 0;
	Lv.TopIndex = 0;
}

void TUserSet::LoadPath0Item(unsigned char aP) 
{
	if(Load0.Count < 65){
		Load0.addr[Load0.Count] = aP;
		Load0.Count++;
	}
}
		
void TUserSet::LoadPath1Item(unsigned char aP)
{
	if(Load1.Count < 256){
		Load1.addr[Load1.Count]= aP;
		Load1.Count++;
	}
}

void TUserSet::LoadPath0(void) 
{
	int i;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5)
			LoadPath0Item(i+1);
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTableHandCtl[i] == 0xA5)
			LoadPath0Item(i+41);
	}
	for(i=0; i<MainCtl.fData.Split.DirectCtlCount; i++){
		if(MainCtl.fData.Split.ExistTableDirectCtl[i] == 0xA5)
			LoadPath0Item(i+49);
	}

	Lv.SelectedCol = 0;
	Lv.TopIndex = 0;
	
	TMyString::sFromStr("系统设置->用户码->根设备", Caption.Text);
	TMyString::sAddOnStr("         按设置键开始设置", Caption.Text);

	Caption.FullRedraw();
}
		
void TUserSet::LoadPath1(void)
{
	unsigned int i;
	unsigned char aP0, aLim;
	
	aP0 = Load0.addr[Load0.SelectedIndex];
	if(aP0 == 0)return;
	if(aP0 > 64) return;
	Load1.Count = 0;
	if(aP0 < 41){
		ClearPath1();
		Load1.P0 = aP0;
		aP0 = aP0-1;

		for(i=0; i< dEP_MAX_COUNT_PER_PORT; i++){
			if(Port[aP0].ExistTable[i] == 0xA5)
				LoadPath1Item(i+1);
		}
	}else
	if(aP0 < 49){
		ClearPath1();
		Load1.P0 = aP0;
		aP0 = aP0 -40 -1;
		aLim = HandCtlBoard[aP0].fData.HandCtl.ResistedCount;
		for(i=0; i< aLim; i++){
			LoadPath1Item(i+1);
		}
	}else
	if(aP0 < 57){
		ClearPath1();
		Load1.P0 = aP0;
		aP0 = aP0 -48 -1;
		aLim = DirectCtlBoard[aP0].fData.HandCtl.ResistedCount;
		for(i=0; i< aLim; i++){
			LoadPath1Item(i+1);
		}
	}
	
	TMyString::sFromStr("系统设置->用户码->终端设备", Caption.Text);
	TMyString::sAddOnStr("         按设置键开始设置", Caption.Text);
	
	Caption.FullRedraw();
	
	Lv.SelectedCol = 0;
	Lv.TopIndex = 0;
}

