#include "gType.h"
#include "GraphLow.h"
#include "GraphBase.h"
#include "GlobalString.h"
#include "RuntimeData.h"
#include "Runtime.h"
#include "Gui.h"
#include "ListViewDevice.h"

#define TextTopOffSet	6
#define ListLineCnt	10
#define IconVcnt	5
#define IconHcnt	10

#define HLineTextRight	780
#define HSpace	33

#define clOdd	clMoneyGreen
#define clEven	clWhiteBlue

static const char cType[8] = "类型";
static const char cIndex[8] = {"编号"};

static const char cActive[8] = {"搭载/"};
static const char cCarry[8] = {"已登记"};
static const char cVoltage[8] = {"电压"};
static const char cCurrent[8] = {"电流"};
static const char cDescp[8] = {"描述"};

static const char cUserCode[8] = {"用户码"};
static const char cAddr[8] = {"地址"};
static const char cAtr1[8] = {"特征1"};
static const char cAtr2[8] = {"特征2"};
static const char cAtr3[8] = {"特征3"};
static const char cAtr4[8] = {"特征4"};

static const int Lv0Left[5] ={
	10, 120, 280, 460, 610
};
static const int Lv1Left[6] ={
	4, 64, 164, 230, 340, 410
};

void TListViewDevice::Init()
{
	this->DevProperty = nullptr;
	Tag.Reset();
	Items[0].Reset();
	Items[1].Reset();
	Items[2].Reset();
	IsShow = 0;
}

void TListViewDevice::SetFontSize(int size)
{
	FontSize = size;
	FontHeight = GetFontHeight(FontSize);
	LineCount =Content.Height / FontHeight;	
}

void TListViewDevice::SetListColWidth(int w0, int w1, int w2, int w3, int w4,int w5,int w6, int w7, int w8)
{
	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.ColRight[0] = ListView.ColLeft[0] + w0;
	for(i=1; i<9; i++){
		ListView.ColLeft[i] = ListView.ColRight[i-1] +1;
		ListView.ColRight[i] = ListView.ColLeft[i] + ListView.ColWidth[i];	
	}
}

void TListViewDevice::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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

void TListViewDevice::RenderContent(void)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	unsigned int x,y, y2, clr;
	
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);	
}

void TListViewDevice::DrawSelfLevel0(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	if(LanguageEnCn == 0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->注册信息");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Loop->Registered Information");
	}
	
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(LanguageEnCn == 0){
		TextRender_string24(Lv0Left[0],Lv.Grid.Top[1] + 4, clNearWhite, "回路号");
		TextRender_string24(Lv0Left[1],Lv.Grid.Top[1] + 4, clNearWhite, "已注册点数");
		TextRender_string24(Lv0Left[2],Lv.Grid.Top[1] + 4, clNearWhite, "未注册点数");
		TextRender_string24(Lv0Left[3],Lv.Grid.Top[1] + 4, clNearWhite, "屏蔽地址数");
		TextRender_string24(Lv0Left[4],Lv.Grid.Top[1] + 4, clNearWhite, "已注册比例");
	}else{
		TextRender_string24(Lv0Left[0],Lv.Grid.Top[1] + 4, clNearWhite, "Loop");
		TextRender_string24(Lv0Left[1],Lv.Grid.Top[1] + 4, clNearWhite, "Registered");
		TextRender_string24(Lv0Left[2],Lv.Grid.Top[1] + 4, clNearWhite, "UnRegister");
		TextRender_string24(Lv0Left[3],Lv.Grid.Top[1] + 4, clNearWhite, "Blocking");
		TextRender_string24(Lv0Left[4],Lv.Grid.Top[1] + 4, clNearWhite, "Reg Ratio");
	}
	
	TMyString::sClear(Text);
	if(LanguageEnCn == 0){
		TMyString::sFromStr("机器号", Text);
	}else{
		TMyString::sFromStr("CtrlNum", Text);
	}
	TMyString::sAddOn2Dg(MainCtl.fData.Split.MyNum,Text);
	if(LanguageEnCn == 0){
		TMyString::sAddOnStr("本机", Text);
	}else{
		TMyString::sAddOnStr(" Local", Text);
	}
	TextRender_string24(Lv.Grid.Left[ 0]+ 10, Lv.Grid.Top[ 0]+ 4, clNearWhite, Text);
	
	TMyString::sClear(Text);
	if(LanguageEnCn==0)TMyString::sFromStr("总回路数", Text);
	else TMyString::sFromStr("Loop(s)", Text);
	TMyString::sAddOn2Dg(MainCtl.fData.Split.PortCount,Text);
	if(LanguageEnCn==0)TMyString::sAddOnStr("  已注册总点数",Text);
	else TMyString::sAddOnStr("  Mounted Device(s)",Text); 
	int aCn =0;
	for(int i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
			for(int x=0; x<dEP_MAX_COUNT_PER_PORT; x++){
				if( (Port[i].ExistTable[x] == 0xA5) && (Port[i].dTypeTable[x] != 0) ){
					aCn++;
				}
			}
		}
	}
	TMyString::sAddOn5Dg(aCn,Text);
	TextRender_string24(Lv.Grid.Left[ 0]+ 360, Lv.Grid.Top[ 0]+ 4, clNearWhite, ItemColor, Text);
	
}

void TListViewDevice::DrawSelfLevel1(void)
{
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	if(LanguageEnCn == 0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->注册信息->单回路查询          ");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Circuit->Registered Information");
	}
	
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	
	DrawLevel1Detail();
	if(LanguageEnCn == 0){
		TextRender_string24(Lv1Left[0], Lv.Grid.Top[ 1]+ 4, clNearWhite, "地址");
		TextRender_string24(Lv1Left[1], Lv.Grid.Top[ 1]+ 4, clNearWhite, "类型");
		TextRender_string24(Lv1Left[2], Lv.Grid.Top[ 1]+ 4, clNearWhite, "信息");
		TextRender_string24(Lv1Left[3], Lv.Grid.Top[ 1]+ 4, clNearWhite, " 二次码");
		TextRender_string24(Lv1Left[4], Lv.Grid.Top[ 1]+ 4, clNearWhite, "分区");
		TextRender_string24(Lv1Left[5], Lv.Grid.Top[ 1]+ 4, clNearWhite, "  注释");
	}else{
		TextRender_string24(Lv1Left[0], Lv.Grid.Top[ 1]+ 4, clNearWhite, "Addr");
		TextRender_string24(Lv1Left[1], Lv.Grid.Top[ 1]+ 4, clNearWhite, "Type");
		TextRender_string24(Lv1Left[2], Lv.Grid.Top[ 1]+ 4, clNearWhite, "Info");
		TextRender_string24(Lv1Left[3], Lv.Grid.Top[ 1]+ 4, clNearWhite, " AuxID");
		TextRender_string24(Lv1Left[4], Lv.Grid.Top[ 1]+ 4, clNearWhite, "Zone");
		TextRender_string24(Lv1Left[5], Lv.Grid.Top[ 1]+ 4, clNearWhite, "  Note");
	}
}

void TListViewDevice::DrawSelfLevel2(void)
{
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	if(LanguageEnCn == 0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->注册信息->单回路查询->部件数据");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Loop->Registered Information->Single Loop->Device");
	}
	
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
}

void TListViewDevice::DrawLevel1Detail(void)
{
	RectFillRender(Lv.Grid.Left[0], Lv.Grid.Top[0], Lv.Grid.Right[0], Lv.Grid.Bottom[0], ItemColor);
	
	TMyString::sClear(Text);
	if(LanguageEnCn==0)TMyString::sFromStr("回路号", Text);
	else TMyString::sFromStr("Loop:", Text);
	TMyString::sAddOn2Dg(Items[0].Path+1,Text);
	TextRender_string24(Lv1Left[0], Lv.Grid.Top[ 0]+ 4, clNearWhite, Text);
	
	TMyString::sClear(Text);
	if(LanguageEnCn==0)TMyString::sFromStr("机器号", Text);
	else TMyString::sFromStr("CTRL:", Text);
	TMyString::sAddOn2Dg(MainCtl.fData.Split.MyNum,Text);
	if(LanguageEnCn==0)TMyString::sAddOnStr("本机", Text);
	else TMyString::sAddOnStr("Local", Text);
	TextRender_string24(Lv.Grid.Left[ 0]+ 200, Lv.Grid.Top[ 0]+ 4, clNearWhite, Text);
	
	TMyString::sClear(Text);
	if(LanguageEnCn==0)TMyString::sFromStr("已注册总点数", Text);
	else TMyString::sFromStr("Registered:", Text);
	int Cnt =0;
	for(int i=0; i<250; i++){
		if( (Port[Items[0].Path].ExistTable[i] == 0xA5) && (Port[Items[0].Path].dTypeTable[i] != 0) ){
			Cnt++;
		}
	}
	TMyString::sAddOn3Dg(Cnt,Text);
	if(LanguageEnCn==0)TMyString::sAddOnStr("  未注册总点数",Text);
	else TMyString::sAddOnStr("   UnReg:",Text);
	TMyString::sAddOn3Dg(250-Cnt,Text);
	TextRender_string24(Lv.Grid.Left[ 0]+ 410, Lv.Grid.Top[ 0]+ 4, clNearWhite, ItemColor, Text);
}

void TListViewDevice::DrawCaption(void)
{	
	Caption.Show();
}

void TListViewDevice::DrawCaption2(void)
{	
	//SText.Show();
}

void TListViewDevice::GetTypeAndCount()
{
	int i;
	unsigned char aP0,aT;
	if(ItemEnter.Path0 == 0)return;
	for(i=0; i<256; i++){
		TypeAndCount[i] = 0;
	} 
	if(ItemEnter.Path0 < 41){
		aP0 = ItemEnter.Path0 - 1;
		for(i=0; i<256; i++){
			//if(EpDev[aP0][i].fData.Split.Id){
				//aT = EpDev[aP0][i].fData.Split.dType;
				//TypeAndCount[aT]++;
			//}
		}
		TypeCount = 0;
		for(i=0; i<256; i++){
			if(TypeAndCount[i] != 0)TypeCount++;
		}
		TypeCountPageCount = TypeCount / 30 +1;
		TypeCountIndex = 0;
	}
}

int TListViewDevice::FindStartPoint()
{
	int i;
	TypeCountIndex = TypeCountPageIndex *30;
	for(i=0;i<256;i++){
		if(TypeAndCount[i] != 0){
			return i;
		}
	}
	return -1;
}

void TListViewDevice::CheckNewLine(int aLen)
{
	if( (DrawX + aLen) > HLineTextRight ){
		DrawY = DrawY + HSpace;
		DrawX = Content.Left +6;
	}
}

void TListViewDevice::ShowTypeList(unsigned char aP0, unsigned char Type1, unsigned char Type2, int Is2Type, char *p, int tLen)
{

	int i,ToDraw, Addr, DrawText ,Path,ThisLen, aFrom, Found, NewLine;
	
	if(aP0 == 0)return;
	if(aP0 >40) return;

	LastDrawX = -1;
	LastDrawY = -1;
	Path = aP0 -1;
	ToDraw = 0;
	DrawText =0;
	Found = 0;
	for(i=0; i<251; i++){
		if(Is2Type){
			if( (Port[Path].ExistTable[i] == 0xA5) && (( Port[Path].dTypeTable[i] == Type1) || (Port[Path].dTypeTable[i] == Type2) )){
				Addr = i + 1;
				if(Found == 0){
					aFrom = i + 1;
					Found = 1;
				}
			}else{
				if(Found){
					if(aFrom == Addr){
						//Show One Addr
						ToDraw = 1;
					}else{
						//Show Multi Addr
						ToDraw = 2;
					}
				}
				Found = 0;
			}
		}else{
			if( (Port[Path].ExistTable[i] == 0xA5) && ( Port[Path].dTypeTable[i] == Type1 )){
				Addr = i + 1;
				if(Found == 0){
					aFrom = i + 1;
					Found = 1;
				}
			}else{
				if(Found){
					if(aFrom == Addr){
						//Show One Addr
						ToDraw = 1;
					}else{
						//Show Multi Addr
						ToDraw = 2;
					}
				}
				Found = 0;
			}
		}
		
		
		if(i == 250){
			if(Found){
				if(aFrom == Addr){
					//Show One Addr
					ToDraw = 1;
				}else{
					//Show Multi Addr
					ToDraw = 2;
				}
			}
			Found = 0;
		}
		
		if(ToDraw){
			if(DrawText == 0){
				CheckNewLine(tLen);
				TextRender_string24(DrawX,DrawY,clNearBlack, p);
				DrawX = DrawX + tLen;
				DrawText = 1;
			}
			
			if(ToDraw == 1){
				if(Addr > 99){
					ThisLen = 3 * 12;
				}else
				if(Addr >9){
					ThisLen = 2 * 12;
				}else{
					ThisLen = 1 * 12;
				}
				CheckNewLine(ThisLen);
				TextDigitRenderLeft24_1t3(DrawX,DrawY,clNearBlack,Addr);
				DrawX = DrawX + ThisLen;
				
				ThisLen = 1 *12;
				CheckNewLine(ThisLen);
				TextRender_string24(DrawX,DrawY,clNearBlack, ",");
				LastDrawX = DrawX;
				LastDrawY = DrawY;
				DrawX = DrawX + ThisLen;
			}else
			if(ToDraw == 2){
				if(aFrom > 99){
					ThisLen = 3 * 12;
				}else
				if(aFrom >9){
					ThisLen = 2 * 12;
				}else{
					ThisLen = 1 * 12;
				}
				CheckNewLine(ThisLen);
				TextDigitRenderLeft24_1t3(DrawX,DrawY,clNearBlack,aFrom);
				DrawX = DrawX + ThisLen;
				//_________________________
				ThisLen = 1 *12;
				
				CheckNewLine(ThisLen);
				TextRender_string24(DrawX,DrawY,clNearBlack, "~");
				DrawX = DrawX + ThisLen;
				//_________________________
				
				if(Addr > 99){
					ThisLen = 3 * 12;
				}else
				if(Addr >9){
					ThisLen = 2 * 12;
				}else{
					ThisLen = 1 * 12;
				}
				CheckNewLine(ThisLen);
				TextDigitRenderLeft24_1t3(DrawX,DrawY,clNearBlack,Addr);
				DrawX = DrawX + ThisLen;
				
				ThisLen = 1 *12;
				CheckNewLine(ThisLen);
				TextRender_string24(DrawX,DrawY,clNearBlack, ",");
				LastDrawX = DrawX;
				LastDrawY = DrawY;
				DrawX = DrawX + ThisLen;
			}
			ToDraw = 0;
		}
	}
	if(DrawText){
		if(LastDrawX >0){
			TextRender_string24(LastDrawX,LastDrawY,clNearBlack, Color, " ");
			if( (LastDrawX + 36) < HLineTextRight){
				TextRender_string24(LastDrawX,LastDrawY,clNearBlack, Color, "   ");
				DrawX = DrawX + 32;
			}else{
				DrawY = DrawY + HSpace;
				DrawX = Content.Left +6;
			}
		}
		TypeCharHasShow = 1;
	}
}


void TListViewDevice::ClearSpliteCount()
{
	Count.Smoke	=0;
	Count.Tempe	=0;
	Count.HandReport	=0;
	Count.HydKp		=0;
	Count.ModuleInput	 =0;
	Count.ModuleInOut	 =0;
	Count.ModuleOutput =0;
	Count.Va		=0;
	Count.FDisplay 	=0;
	Count.cOther   	=0;
	Count.SmokeTempe = 0;
	Count.Tt = 0;
}


void TListViewDevice::GetSpliteCount(unsigned char aP0)
{
	unsigned int sPath0,sPath1,aType0,aType1;
	int i, j;
	unsigned int aIndex, aCount;
	unsigned char aType;
	

	ClearSpliteCount();

	if(aP0 == 0) return;
	if(aP0 > dPORT_MAX_COUNT)return;
	i = aP0-1;
	if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
		for(j=0; j<250; j++){
			if(Port[i].ExistTable[j] == 0xA5){
				aType = Port[i].dTypeTable[j];
				//if(  (Port[i].StateTable[j] & (dEpStateBitFault + dEpStateBitMask)) == 0) {
				if(1){
					if(aType == 0){
						//Count.cOther++;
						//Count.Tt++;
					}else
					if(  (aType == dgTYPE_SMOKE_DETECTOR)  ){
						     Count.Smoke++;
							Count.Tt++;
					}else
					if(  aType ==  dgTYPE_TEMPE_DETECTOR ){
						     Count.Tempe++;
							Count.Tt++;
					}else
					if(  (aType == dgTYPE_HAND_REPORT ) ){
						     Count.HandReport++;
							Count.Tt++;
					}else
					if(  (aType == dgTYPE_FIRE_HydrantKp ) ){
						     Count.HydKp++;
							Count.Tt++;
					}else
					if(  (aType == dgTYPE_IN_MODULE ) ){
						     Count.ModuleInput++;
							Count.Tt++;
					}else
					if(   (aType == dgTYPE_INOUT_MODULE_NonSource) ||
						(aType == dgTYPE_INOUT_MODULE_Source) ){
						     Count.ModuleInOut++;
							Count.Tt++;
					}else
					if(  (aType == dgTYPE_OUT_MODULE) ){
						     Count.ModuleOutput++;
							Count.Tt++;
					}else
					if(  (aType == dgType_VAALRAM )  ){
						     Count.Va++;
							Count.Tt++;
					}else
					if(  aType == dgTYPE_DISPLAY_PAD ){
						     Count.FDisplay++;
							Count.Tt++;
					}else
					if(aType == dgTYPE_SMOKE_TEMPE_DETECTOR ){
							Count.SmokeTempe++;
							Count.Tt++;
					}else{
							Count.cOther++;
							Count.Tt++;
					}
				}
			}
		}
	}
}

void TListViewDevice::ShowFig(unsigned char aP0)
{
	
	RectFillRender(Content.Left, Content.Top+42, Content.Right, Content.Bottom, Color);
	
	TextRender_string24(Content.Left+20, Content.Top+42 + 0*32 , clNearBlack, "烟感:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 1*32 , clNearBlack, "温感:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 2*32 , clNearBlack, "输入模块:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 3*32 , clNearBlack, "输出模块:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 4*32 , clNearBlack, "手报:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 5*32 , clNearBlack, "消钮:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 6*32 , clNearBlack, "输入输出模块:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 7*32 , clNearBlack, "层显:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 8*32 , clNearBlack, "声光警报器:" );
	TextRender_string24(Content.Left+20, Content.Top+42 + 9*32 , clNearBlack, "其他:" );
	
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 0*32 ,clNearBlack, Count.Smoke);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 1*32 ,clNearBlack, Count.Tempe);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 2*32 ,clNearBlack, Count.ModuleInput);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 3*32 ,clNearBlack, Count.ModuleOutput);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 4*32 ,clNearBlack, Count.HandReport);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 5*32 ,clNearBlack, Count.HydKp);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 6*32 ,clNearBlack, Count.ModuleInOut);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 7*32 ,clNearBlack, Count.FDisplay);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 8*32 ,clNearBlack, Count.Va);
	TextDigitRender3Right24(Content.Left+200, Content.Top+42 + 9*32 ,clNearBlack, Count.cOther);
	
	TextRender_string24(Content.Left+420, Content.Top+42 + 0*32 , clNearBlack, "烟温一体:" );
	TextDigitRender3Right24(Content.Left+600, Content.Top+42 + 0*32 ,clNearBlack, Count.SmokeTempe);
}

void TListViewDevice::ShowCount(unsigned char aP0)
{
	DrawX = Content.Left +6;
	DrawY = Content.Top + 42;
	
	//dgTYPE_SMOKE_DETECTOR,				
	//dgTYPE_TEMPE_DETECTOR,				
	//dgTYPE_HAND_REPORT,				
	//dgTYPE_FIRE_hydrant_Kp,		
	//dgTYPE_IN_MODULE,			
	//dgTYPE_OUT_MODULE,			
	//dgTYPE_INOUT_MODULE_NonSource,	
	//dgTYPE_INOUT_MODULE_Source,		
	//dgType_VAALRAM,				
	//dgTYPE_DISPLAY_PAD
	RectFillRender(Content.Left, Content.Top+42, Content.Right, Content.Bottom, Color);
	
	TypeCharHasShow = 0;
	if(MainCtl.fData.Split.ExistTablePort[aP0-1] == 0xA5){
		ShowTypeList(aP0, dgTYPE_SMOKE_DETECTOR, 0, 0, "烟感:", 5*12);
		ShowTypeList(aP0, dgTYPE_TEMPE_DETECTOR, 0, 0, "温感:", 5*12);
		ShowTypeList(aP0, dgTYPE_IN_MODULE, 0, 0, "输入模块:", 9*12);
		ShowTypeList(aP0, dgTYPE_OUT_MODULE, 0, 0, "输出模块:", 9*12);
		ShowTypeList(aP0, dgTYPE_HAND_REPORT, 0, 0, "手报:", 5*12);
		ShowTypeList(aP0, dgTYPE_FIRE_HydrantKp, 0, 0, "消钮:", 5*12);
		ShowTypeList(aP0, dgTYPE_INOUT_MODULE_NonSource, dgTYPE_INOUT_MODULE_Source, 1, "输入输出模块:", 13*12);
		ShowTypeList(aP0, dgTYPE_DISPLAY_PAD, 0, 0, "层显:", 5*12);
		ShowTypeList(aP0, dgType_VAALRAM, 0, 0, "声光警报器:", 11*12);
		ShowTypeList(aP0, dgTYPE_SMOKE_TEMPE_DETECTOR, 0, 0, "烟温一体:", 5*12);
	}
	if(TypeCharHasShow == 0){
		TextRender_string24(Content.Left, Content.Top+42, clNearBlack, "本回路未挂载终端设备" );
	}
}

void TListViewDevice::DrawList(void)
{
	int i,t,x,y;
	unsigned int tAddr;
	unsigned int ExistCnt, cnt, aP0, aP1, dType;
	if(Tag.ViewingLevel == 0){
		for(i=0, t=Items[0].TopInx; (i<8) && (t<Items[0].Count); i++, t++){
			aP0 = Items[0].Item[t];
			//Draw Port Num
			TextDigitRender2Right24(Lv0Left[0], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, aP0+1);
			//Draw Registed 
			ExistCnt = 0;
			for(x=0; x<250; x++){
				if( (Port[aP0].ExistTable[x] == 0xA5) && Port[aP0].dTypeTable[x])ExistCnt++;
			}
			TextDigitRender3Right24(Lv0Left[1], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, ExistCnt);
			//Draw UnRegisted
			TextDigitRender3Right24(Lv0Left[2], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, 250 - ExistCnt);
			//Draw Mask
			cnt =0;
			for(x=0; x<250; x++){
				if(Port[aP0].StateTable[x] & dEpStateBitMask)cnt++;
			}
			TextDigitRender3Right24(Lv0Left[3], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, cnt);
			//Draw Percent
			cnt = ExistCnt * 1000 / 250;
			TextDigitRender3Right24(Lv0Left[4], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, cnt/10);
			TextRender_string24(Lv0Left[4]+36, Lv.Grid.Top[i+2] + 4, clNearWhite, "." );
			TextDigitRender1Right24(Lv0Left[4]+48, Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, cnt%10);
			TextRender_string24(Lv0Left[4]+62, Lv.Grid.Top[i+2] + 4, clNearWhite, "%" );
		}
	}else
	if(Tag.ViewingLevel == 1){
		aP0 = Items[0].Path;
		for(i=0, t=Items[1].TopInx; i<8; i++, t++){
			if( t<dEP_MAX_COUNT_PER_PORT){
				aP1 = t+1;
				//Draw Addr
				TextDigitRender3Right24(Lv1Left[0], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, aP1);
				//Draw Type 
				dType = Port[aP0].dTypeTable[t];
				if( (Port[aP0].ExistTable[t] == 0xA5) && dType){
					
					if(LanguageEnCn == 0)TMyString::sFromStr(StrHwType[dType],Text);
					else TMyString::sFromStr(StrHwTypeEn[dType],Text);
					TMyString::sFillFixLen(Text,8);
					TMyString::sBrokeFixLen(Text,8);
					TextRender_string24(Lv1Left[1]-16, Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, Text);
					//Draw State
					if(LanguageEnCn == 0){
						if(Port[aP0].StateTable[t] & dEpStateBitFire)TMyString::sFromStr("报警",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitStart)TMyString::sFromStr("启动",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitFeedBack)TMyString::sFromStr("反馈",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitFault)TMyString::sFromStr("故障",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitSv)TMyString::sFromStr("监管",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitMask)TMyString::sFromStr("屏蔽",Text);
						else TMyString::sFromStr("正常",Text);
					}else{
						if(Port[aP0].StateTable[t] & dEpStateBitFire)TMyString::sFromStr("Fire",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitStart)TMyString::sFromStr("Act",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitFeedBack)TMyString::sFromStr("FBCK",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitFault)TMyString::sFromStr("Fail",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitSv)TMyString::sFromStr("SV",Text);
						else if(Port[aP0].StateTable[t] & dEpStateBitMask)TMyString::sFromStr("Mask",Text);
						else TMyString::sFromStr("Idle",Text);
					}
					TextRender_string24(Lv1Left[2], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, Text);
				}else{
					TextRender_string24(Lv1Left[1]-16, Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, "  ----  ");
					TextRender_string24(Lv1Left[2], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, " -- ");
				}
				//Draw UserCode
				TMyString::sClear(Text);
				TMyString::sAddOn8Dg(Port[aP0].UcList[t].Full, Text);
				TextRender_string24(Lv1Left[3], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, Text);
				//Draw Area 
				cnt = Port[aP0].Area[t];
				TextRender_string24	(Lv1Left[4], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, "    ");
				TextDigitRender4Right24	(Lv1Left[4], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, cnt);
				//Draw Descp
				TMyString::sClear(Text);
				if( (aP0 <= dPORT_MAX_COUNT) && (t < 250) ){
					tAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (t * 32);
					for(int g=0; g<32; g++){
						Text[g] = *(volatile unsigned char *)(tAddr + g);
						if(  (Text[g] == '\0') || (Text[g] == 0) ){
							break;
						}
					}
					Text[32] = '\0';
					Text[33] = '\0';
					TMyString::sFillFixLen(Text, 32);
				}else{
					TMyString::sFillFixLen(Text, 32);
				}
				TextRender_string24(Lv1Left[5], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, Text);
			}else{
				TextRender_string24(Lv1Left[0], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, "   ");
				TextRender_string24(Lv1Left[1]-16, Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, "        ");
				TextRender_string24(Lv1Left[2], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, "    ");
				TextRender_string24(Lv1Left[3], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, "        ");
				TextRender_string24(Lv1Left[4], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, "    ");
				TMyString::sClear(Text);
				TMyString::sFillFixLen(Text, 32);
				TextRender_string24(Lv1Left[5], Lv.Grid.Top[i+2] + 4, clNearWhite, ItemColor, Text);
			}
		}
	}
}

void TListViewDevice::Show(void)
{
	DrawSelfLevel0();
	DrawList();	
	Check4SelectedLv0();
	IsShow = 1;
}

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

void TListViewDevice::FullRedraw(int Prm)
{
	if(Prm == 0){
		RenderBackGround();
		if(Tag.ViewingLevel == 0){
			DrawSelfLevel0();
			DrawList();
			Check4SelectedLv0();
		}else
		if(Tag.ViewingLevel == 1){
			DrawSelfLevel1();
			DrawList();
			Check4SelectedLv1();
		}else
		if(Tag.ViewingLevel == 2){
			if(DevProperty != nullptr){
				DevProperty->FullRedraw(Prm);
			}
		}			
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		Caption.Enable = 1;
		if(Tag.ViewingLevel == 0){
			DrawSelfLevel0();
			DrawList();
			Check4SelectedLv0();
		}else
		if(Tag.ViewingLevel == 1){
			DrawSelfLevel1();
			DrawList();
			Check4SelectedLv1();
		}else
		if(Tag.ViewingLevel == 2){
			if(DevProperty != nullptr){
				DevProperty->FullRedraw(Prm);
			}
		}
	}
}

void TListViewDevice::ShowCircuitDet()
{
	if(IsPath0Found){
		TextRender_string24(Content.Left + 200,Content.Top + 4,clNearBlack, Color, "      " );
		TextRender_string24(Content.Left + 260,Content.Top + 4,clNearBlack, Color, "设备数量:     " );
		TextDigitRender3Right24(Content.Left + 368,Content.Top + 4,clNearBlack, Count.Tt);
		TextRender_string24(Content.Left + 540,Content.Top + 4,clNearBlack, "左右键切换显示内容" );
	}else{
		TextRender_string24(Content.Left + 200,Content.Top + 4,clNearBlack, Color, "此回路未登记       " );
		TextRender_string24(Content.Left + 540,Content.Top + 4,clNearBlack, Color, "                  " );
	}
}

void TListViewDevice::CalcIconDrawPrm(void)
{

}

TGuiMsgReturn TListViewDevice::QueryKeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg;
	aMsg = DevProperty->KeyIn(aKey);
	
	if(aMsg == guiMsgReturn){
		return guiMsgReturn;
	}
	return guiMsgNone;
}

TGuiMsgReturn TListViewDevice::EditKeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg;
	//editting
	return guiMsgNone;
}

void TListViewDevice::ClearSelectedLv0(void)
{
	int iT;
	iT = Tag.Level[0].Inx + 2;
	if(iT > 9)return;
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + Lv.Grid.Right[iT],  Lv.Grid.Top[iT] + 3,  ItemColor);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + 3,  Lv.Grid.Bottom[iT],  ItemColor);   //draw rectangle
	RectFillRender(Lv.Grid.Right[iT] -3,  Lv.Grid.Top[iT],  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  ItemColor);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Bottom[iT] -3,  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  ItemColor);   //draw rectangle
}

void TListViewDevice::Check4SelectedLv0(void)
{
	int iT;
	iT = Tag.Level[0].Inx + 2;
	if(iT > 9)return;
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + Lv.Grid.Right[iT],  Lv.Grid.Top[iT] + 3,  clBlue);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + 3,  Lv.Grid.Bottom[iT],  clBlue);   //draw rectangle
	RectFillRender(Lv.Grid.Right[iT] -3,  Lv.Grid.Top[iT],  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  clBlue);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Bottom[iT] -3,  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  clBlue);   //draw rectangle
}

void TListViewDevice::ClearSelectedLv1(void)
{
	int iT;
	iT = Tag.Level[1].Inx + 2;
	if(iT > 9)return;
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + Lv.Grid.Right[iT],  Lv.Grid.Top[iT] + 3,  ItemColor);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + 3,  Lv.Grid.Bottom[iT],  ItemColor);   //draw rectangle
	RectFillRender(Lv.Grid.Right[iT] -3,  Lv.Grid.Top[iT],  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  ItemColor);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Bottom[iT] -3,  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  ItemColor);   //draw rectangle
}

void TListViewDevice::Check4SelectedLv1(void)
{
	int iT;
	iT = Tag.Level[1].Inx + 2;
	if(iT > 9)return;
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + Lv.Grid.Right[iT],  Lv.Grid.Top[iT] + 3,  clBlue);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Top[iT],  Lv.Grid.Left[iT] + 3,  Lv.Grid.Bottom[iT],  clBlue);   //draw rectangle
	RectFillRender(Lv.Grid.Right[iT] -3,  Lv.Grid.Top[iT],  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  clBlue);   //draw rectangle
	RectFillRender(Lv.Grid.Left[iT],  Lv.Grid.Bottom[iT] -3,  Lv.Grid.Right[iT],  Lv.Grid.Bottom[iT],  clBlue);   //draw rectangle
}

TGuiMsgReturn TListViewDevice::KeyIn(unsigned char aKey)
{
	int ind,i,aIndex, Found;
	unsigned char aP0;
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn aMsg2Rtn = guiMsgNone;
	//Up or Down One Index___________________
	
	if(Tag.ViewingLevel == 0){
		if(VK_LEFT ==aKey){
						
		}else
		if(VK_RIGHT == aKey){

		}else
		if(VK_UP == aKey){
			if(Tag.Level[0].Inx >0){
				ClearSelectedLv0();
				Tag.Level[0].Inx--;
				Items[0].Inx--; 
				Items[0].Path = Items[0].Item[Items[0].Inx];
				Check4SelectedLv0();
			}else{
				if(Items[0].Inx >0){
					Items[0].Inx--;
					Items[0].Path = Items[0].Item[Items[0].Inx];
					Items[0].TopInx = Items[0].Inx; 
					DrawList();
				}
			}
		}else
		if(VK_DOWN == aKey){
			if(Tag.Level[0].Inx < 7){
				if(Items[0].Inx < (Items[0].Count-1)){
					ClearSelectedLv0();
					Tag.Level[0].Inx++;
					Items[0].Inx++; 
					Items[0].Path = Items[0].Item[Items[0].Inx];
					Check4SelectedLv0();
				}
			}else{
				Tag.Level[0].Inx = 7;
				if(Items[0].Inx < (Items[0].Count-1)){
					Items[0].Inx++; 
					Items[0].Path = Items[0].Item[Items[0].Inx];
					Items[0].TopInx++; 
					if(Items[0].TopInx <0)Items[0].TopInx =0;
					DrawList();
				}
			}
		}else
		if(aKey == VK_TAB){

		}else
		if(aKey == VK_EXECUTE){
				//Open path Device
			if(Items[0].Count > 0){
				if(Items[0].Inx < Items[0].Count){
					if(Items[0].Path < dPORT_MAX_COUNT){
						Tag.ViewingLevel = 1;
						Tag.Level[1].Inx = 0;
						Items[1].TopInx = 0;
						Items[1].Inx = 0;
						Items[1].Path = Items[1].Inx;
						RenderBackGround();
						DrawSelfLevel1();
						DrawList();
						Check4SelectedLv1();
					}
				}
			}
		}else
		if(aKey == VK_RETURN){
			aMsg = guiMsgReturn;
		}else{

		}
	}else
	if(Tag.ViewingLevel == 1){
		if(VK_LEFT ==aKey){
			int GoInx;
			if(Items[1].TopInx <1){
				GoInx = 248;
				Items[1].Inx = GoInx;
				Items[1].TopInx = GoInx;
			}else{
				GoInx = (Items[1].Inx / 8 -1) * 8;
				if(GoInx <0)GoInx = 0;
				Items[1].Inx = GoInx;
				Items[1].TopInx = GoInx;
			}
			Items[1].Path = Items[1].Inx;
			DrawList();
			ClearSelectedLv1();
			Tag.Level[1].Inx = 0;
			Check4SelectedLv1();
		}else
		if(VK_RIGHT == aKey){
			int GoInx;
			GoInx = (Items[1].Inx / 8 +1) * 8;
			if(GoInx > 248)GoInx = 0;
			Items[1].Inx = GoInx;
			Items[1].TopInx = GoInx;
			Items[1].Path = Items[1].Inx;
			DrawList();
			ClearSelectedLv1();
			Tag.Level[1].Inx = 0;
			Check4SelectedLv1();
		}else
		if(VK_UP == aKey){
			if(Tag.Level[1].Inx >0){
				ClearSelectedLv1();
				Tag.Level[1].Inx--;
				Items[1].Inx--; 
				Items[1].Path = Items[1].Inx;
				Check4SelectedLv1();
			}else{
				if(Items[1].Inx >0){
					Items[1].Inx--;
					Items[1].TopInx = Items[1].Inx;
					Items[1].Path = Items[1].Inx;
					DrawList();
				}
			}
		}else
		if(VK_DOWN == aKey){
			if(Tag.Level[1].Inx < 7){
				if(Items[1].Inx < (dEP_MAX_COUNT_PER_PORT-1)){
					ClearSelectedLv1();
					Tag.Level[1].Inx++;
					Items[1].Inx++; 
					Items[1].Path = Items[1].Inx;
					Check4SelectedLv1();
				}
			}else{
				Tag.Level[1].Inx = 7;
				if(Items[1].Inx < (dEP_MAX_COUNT_PER_PORT-1)){
					Items[1].TopInx++;
					Items[1].Inx++; 
					Items[1].Path = Items[1].Inx;
					DrawList();
				}
			}
		}else
		if(aKey == VK_TAB){

		}else
		if(aKey == VK_EXECUTE){
			this->DevProperty->Init(TDevProperty::htNoType, 1);
			this->DevProperty->aPanel = this->aPanel;
			this->DevProperty->IList = this->IList;
			this->DevProperty->IList->RealtimeData.Reset();
			this->DevProperty->IList->TextOutClear();
			this->DevProperty->SetPath(MainCtl.fData.Split.MyNum, Items[0].Path+1, Items[1].Path+1);
			this->DevProperty->Show();
			this->DevProperty->Init4DType();
			this->DevProperty->DrawCommonInfTitle();
			this->DevProperty->RenewCommonInf();
			Tag.ViewingLevel = 2;
		}else
		if(aKey == VK_RETURN){
			Tag.ViewingLevel = 0;
			DrawSelfLevel0();
			DrawList();	
			Check4SelectedLv0();
		}else{

		}
	}else
	if(Tag.ViewingLevel == 2){
		aMsg2Rtn = this->DevProperty->KeyIn(aKey);
		if(aMsg2Rtn == guiMsgReturn){
			Tag.ViewingLevel = 1;
			RenderBackGround();
			DrawSelfLevel1();
			DrawList();
			Check4SelectedLv1();
		}
	}
	//____________________________________________________________________<- List View

	return aMsg;

}

void TListViewDevice::ClearPath(void)
{
	ListView.TopIndex = 0;

}

void TListViewDevice::ClearPath1(void)
{
	ListView.TopIndex = 0;
}

void TListViewDevice::LoadPath0Item() 
{
	for(int i=0; i<dPORT_MAX_COUNT; i++){
		Items[0].Item[i] = 0;
	}
	Items[0].Count = 0;
	for(int i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
			Items[0].Item[Items[0].Count] = i;
			Items[0].Count++;
		}
	}
	ListView.SelectedCol = 0;
	ListView.TopIndex = 0;
	Items[0].Inx = 0;
	Items[0].Path = Items[0].Item[Items[0].Inx];
}
		
void TListViewDevice::LoadPath1Item(TEpDevice *pItem )
{

}

void TListViewDevice::LoadPath0(void) 
{
	int i;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5);
	}
	ListView.SelectedCol = 0;
	ListView.TopIndex = 0;
	Items[0].Inx = 0;
}
		
void TListViewDevice::LoadPath1(unsigned char aP0)
{
	TRootDevice *theRDev;
	TPort *thePort;
	unsigned int i, px;
	
	ItemEnter.Path0 = 0;
	if(aP0 == 0){
		return;
	}
	if(aP0 >40){
		return;
	}
	aP0--;
	
	theRDev = &Port[aP0];
	if(theRDev->fData.Common.dType == dgTYPE_PORT){
		px = theRDev->fData.Port.Id;
		if(px){
			ItemEnter.Path0 = px;
			px--;
			thePort = (TPort *)theRDev;
			ClearPath1();
			for(i=0; i< dEP_MAX_COUNT_PER_PORT; i++){
				if(Port[px].ExistTable[i] == 0xA5)
					LoadPath1Item(&EpDev[px][i]);
			}
		}
	}

	ListView.SelectedCol = 0;
	ListView.TopIndex = 0;
}

void TListViewDevice::SetEditMode(unsigned int aMode)
{
	EditMode = aMode;
}







