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

#define TextTopOffSet	6
#define ListLineCnt	4
#define ColHeight 	33

#define FixTop	69
#define MsgX0	6
#define DescpX0	270	//90

#define dLineMaxLen	64

const char sCharFire[8] = "火警";
const char sCharLinkage[8] = "联动";
const char sCharSv[8] = "监管";
const char sCharFault[8] = "故障";
const char sCharOpa[8] = "操作";

const char sCharFireEn[8] = "Fire";
const char sCharLinkageEn[8] = "Link";
const char sCharSvEn[8] = "SUPV";
const char sCharFaultEn[8] = "Fail";
const char sCharOpaEn[8] = "OPR ";

const static unsigned short stAllTop[10] = {
	80, 120, 160, 200, 240, 
	280, 320, 360, 400, 440
};

static const short qmFixCol[8] = {
	20,100,160,240,   320,400,520,600
};

void TQueryMsg::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);
	BoxBoundTime.Set(Left+170, Top+80, Right-170, Bottom-30);
	GoPrint.Set(Left+170, Top+150, Right-170, Bottom-130);
	Color = aColor;
	#undef CH
	

	Caption.Border.Color = clGray;
	
	FixColColor = clMaroon;
	FixColTextColor = clNearWhite;
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	Count=0;
	SelectedIndex=0;
	SelectedIndex  = 0;
	TopIndex = 0;
	
	IsInputTime = 0;
	IsGoPrint = 0;
	IsAllTime = 1;
	TimeEditFrom = -1;
	TimeIndex =0;
	IsEditing = 0;
	PrintWhat = 0;
	ReStoreP0 = 0;
	ReStoreP1 = 0;
}

void TQueryMsg::InitBackEnd()
{
	Count=0;
	SelectedIndex=0;
	SelectedIndex  = 0;
	TopIndex = 0;
	
	IsInputTime = 0;
	IsGoPrint = 0;
	IsAllTime = 1;
	TimeEditFrom = -1;
	TimeIndex =0;
	IsEditing = 0;
	PrintWhat = 0;
	ReStoreP0 = 0;
	ReStoreP1 = 0;
}

void TQueryMsg::SetListColWidth(int w0, int w1, int w2, int w3, int w4,int w5,int w6, int w7, int w8)
{

}

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

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

void TQueryMsg::RenderContent(void)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);	
}

void TQueryMsg::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 TQueryMsg::DrawCaption(void)
{	
	if(LanguageEnCn==0)TMyString::sFromStr("历史记录->查询全部信息", Caption.Text); 
	else TMyString::sFromStr("Recorded Info->Query All", Caption.Text); 
		
	Caption.Show();
	DrawCaption2();
}

void TQueryMsg::DrawCaption2(void)
{	

}

void TQueryMsg::RenderWarningWait(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);
	if(LanguageEnCn==0){
		TextRender_string24(x+8, y+6, clNearWhite, "提示信息");
		TextRender_string24(x+66, y+36, clNearBlack, "正在读取,请等待");
	}else{
		TextRender_string24(x+8, y+6, clNearWhite, "Message");
		TextRender_string24(x+66, y+36, clNearBlack, "Reading,Please Wait");
	}
}

void TQueryMsg::DrawItemCount(int aCount)
{
	
	TextDigitRender6Left24(500,Caption.Bound.Top +3, clNearWhite, Caption.Color, CountTT);
	TextDigitRender6Left24(720,Caption.Bound.Top +3, clNearWhite, Caption.Color, Count);
	if(LanguageEnCn==0){
		TextRender_string24(400, Caption.Bound.Top +3, clNearWhite, "总数量:");
		TextRender_string24(600, Caption.Bound.Top +3, clNearWhite, "查看数量:");
	}else{
		TextRender_string24(400, Caption.Bound.Top +3, clNearWhite, "Total:");
		TextRender_string24(600, Caption.Bound.Top +3, clNearWhite, "Display:");
	}
}

void TQueryMsg::DrawBoxTimeOutLine()
{
	RectFillRender(BoxBoundTime.Left, 	BoxBoundTime.Top, BoxBoundTime.Right, BoxBoundTime.Bottom, clFrmFace);
	
	VertLineRender(BoxBoundTime.Left,		BoxBoundTime.Top, 		BoxBoundTime.Height -1,			0xFFFFFFFF);
	VertLineRender(BoxBoundTime.Right -1,	BoxBoundTime.Top +1,		BoxBoundTime.Height -2,			0xFF808080);
	VertLineRender(BoxBoundTime.Right,		BoxBoundTime.Top,			BoxBoundTime.Height,			0xFF404040);
	
	HorizLineRender(BoxBoundTime.Left, 		BoxBoundTime.Top, 		BoxBoundTime.Width - 1, 		0xFFFFFFFF);
	HorizLineRender(BoxBoundTime.Left +1, 	BoxBoundTime.Bottom -1,  	BoxBoundTime.Width - 2, 		0xFF808080);
	HorizLineRender(BoxBoundTime.Left,		BoxBoundTime.Bottom,		BoxBoundTime.Width,			0xFF404040);
	
	if(LanguageEnCn==0)TStaticText::sShow(BoxBoundTime.Left+2, BoxBoundTime.Top+2, BoxBoundTime.Right-2, BoxBoundTime.Top+32, clNearWhite, clBlue,  "设置日期时间段,按确认键开始查询");	
	else TStaticText::sShow(BoxBoundTime.Left+2, BoxBoundTime.Top+2, BoxBoundTime.Right-2, BoxBoundTime.Top+32, clNearWhite, clBlue,  "Set Date And Time,OK Key:Start The Query");
}

void TQueryMsg::DrawTimeEntry(void)
{
	int x,y,w,h;
	int i;
	unsigned char aHex0, aHex1;
	
	unsigned int aClr;
	if(IsAllTimeTemp){
		aClr = clSilver;
	}else{
		aClr = clNearBlack;
	}
	
	w = 36;
	h = 32;
	
	vtValueFrom[0] = DateTime.Date.Split.Year;
	vtValueFrom[1] = DateTime.Date.Split.Month;
	vtValueFrom[2] = DateTime.Date.Split.Day;
	
	vtValueFrom[3] = DateTime.Time.Split.Hour;
	vtValueFrom[4] = DateTime.Time.Split.Minute;
	vtValueFrom[5] = DateTime.Time.Split.Second;
	
	vtValueTo[0] =vtValueFrom[0];
	vtValueTo[1] =vtValueFrom[1];
	vtValueTo[2] =vtValueFrom[2];
	vtValueTo[3] =vtValueFrom[3];
	vtValueTo[4] =vtValueFrom[4];
	vtValueTo[5] =vtValueFrom[5];
	
	
	for(i=0; i<6; i++){
		x = BoxBoundTime.Left + 80 + (i * 48);
		y = BoxBoundTime.Top + 140 + (0 * 112);
		
		aHex0 = vtValueFrom[i] / 16 % 10;
		aHex1 = vtValueFrom[i] % 16 % 10;
		vtTextFrom[i][0] = aHex0 + '0';
		vtTextFrom[i][1] = aHex1 + '0';
		vtTextFrom[i][2] = 0;
		RectFillRender(x, y, x+w-1, y+h-1, clFrmFace);
		TextRender_string24(x+3, y+3, aClr, vtTextFrom[i]);
	}
	
	for(i=0; i<6; i++){
		x = BoxBoundTime.Left + 80 + (i * 48);
		y = BoxBoundTime.Top + 140 + (1 * 112);
		
		aHex0 = vtValueTo[i] / 16 % 10;
		aHex1 = vtValueTo[i] % 16 % 10;
		vtTextTo[i][0] = aHex0 + '0';
		vtTextTo[i][1] = aHex1 + '0';
		vtTextTo[i][2] = 0;
		RectFillRender(x, y, x+w-1, y+h-1, clFrmFace);
		TextRender_string24(x+3, y+3, aClr, vtTextTo[i]);
	}
}


void TQueryMsg::DrawTimeEntryNoUpdate(void)
{
	int x,y,w,h;
	int i;
	unsigned char aHex0, aHex1;
	
	unsigned int aClr;
	if(IsAllTimeTemp){
		aClr = clSilver;
	}else{
		aClr = clNearBlack;
	}
	
	w = 36;
	h = 32;
	
	for(i=0; i<6; i++){
		x = BoxBoundTime.Left + 80 + (i * 48);
		y = BoxBoundTime.Top + 140 + (0 * 112);
		
		aHex0 = vtValueFrom[i] / 16 % 10;
		aHex1 = vtValueFrom[i] % 16 % 10;
		vtTextFrom[i][0] = aHex0 + '0';
		vtTextFrom[i][1] = aHex1 + '0';
		vtTextFrom[i][2] = 0;
		RectFillRender(x, y, x+w-1, y+h-1, clFrmFace);
		TextRender_string24(x+3, y+3, aClr, vtTextFrom[i]);
	}
	
	for(i=0; i<6; i++){
		x = BoxBoundTime.Left + 80 + (i * 48);
		y = BoxBoundTime.Top + 140 + (1 * 112);
		
		aHex0 = vtValueTo[i] / 16 % 10;
		aHex1 = vtValueTo[i] % 16 % 10;
		vtTextTo[i][0] = aHex0 + '0';
		vtTextTo[i][1] = aHex1 + '0';
		vtTextTo[i][2] = 0;
		RectFillRender(x, y, x+w-1, y+h-1, clFrmFace);
		TextRender_string24(x+3, y+3, aClr, vtTextTo[i]);
	}
}

void TQueryMsg::DrawIfAllTime(void)
{
	unsigned int aClr, bClr;
	if(TimeEditFrom == -1){
		aClr = clNearWhite;
		bClr = clBlue;
	}else{
		aClr = clNearBlack;
		bClr = clFrmFace;
	}
	if(LanguageEnCn==0){
		if(IsAllTimeTemp){
			TextRender_string24(BoxBoundTime.Left + 150, BoxBoundTime.Top + 42, aClr, bClr, "全部条目");
		}else{
			TextRender_string24(BoxBoundTime.Left + 150, BoxBoundTime.Top + 42, aClr, bClr, "按时间段");
		}
	}else{
		if(IsAllTimeTemp){
			TextRender_string24(BoxBoundTime.Left + 150, BoxBoundTime.Top + 42, aClr, bClr, "   All  ");
		}else{
			TextRender_string24(BoxBoundTime.Left + 150, BoxBoundTime.Top + 42, aClr, bClr, "Segments");
		}
	}
}

void TQueryMsg::DrawBoxTimeString(void)
{
	unsigned int aClr;
	if(IsAllTimeTemp){
		aClr = clSilver;
	}else{
		aClr = clNearBlack;
	}
	if(LanguageEnCn==0){
		TextRender_string24(BoxBoundTime.Left + 52, BoxBoundTime.Top + 74, aClr,  "开始时间");
		TextRender_string24(BoxBoundTime.Left + 82, BoxBoundTime.Top + 108, aClr, "年  月  日  时  分  秒");
		TextRender_string24(BoxBoundTime.Left + 52, BoxBoundTime.Top + 187, aClr, "结束时间");
		TextRender_string24(BoxBoundTime.Left + 82, BoxBoundTime.Top + 219, aClr, "年  月  日  时  分  秒");
	}else{
		TextRender_string24(BoxBoundTime.Left + 52, BoxBoundTime.Top + 74, aClr, "Begin");
		TextRender_string24(BoxBoundTime.Left + 82, BoxBoundTime.Top + 108, aClr, " Y   M   D   H   M   S");
		TextRender_string24(BoxBoundTime.Left + 52, BoxBoundTime.Top + 187, aClr, "End");
		TextRender_string24(BoxBoundTime.Left + 82, BoxBoundTime.Top + 219, aClr, " Y   M   D   H   M   S");
	}
}

void TQueryMsg::DrawBoxTime(void)
{
	DrawBoxTimeOutLine();
	DrawBoxTimeString();
	DrawIfAllTime();
	DrawTimeEntry();
}

void TQueryMsg::DrawGoPrintOutLine()
{
	RectFillRender(GoPrint.Left, 	GoPrint.Top, GoPrint.Right, GoPrint.Bottom, clFrmFace);
	
	VertLineRender(GoPrint.Left,		GoPrint.Top, 		GoPrint.Height -1,		0xFFFFFFFF);
	VertLineRender(GoPrint.Right -1,	GoPrint.Top +1,		GoPrint.Height -2,		0xFF808080);
	VertLineRender(GoPrint.Right,		GoPrint.Top,		GoPrint.Height,			0xFF404040);
	
	HorizLineRender(GoPrint.Left, 	GoPrint.Top, 		GoPrint.Width - 1, 		0xFFFFFFFF);
	HorizLineRender(GoPrint.Left +1, 	GoPrint.Bottom -1,  	GoPrint.Width - 2, 		0xFF808080);
	HorizLineRender(GoPrint.Left,		GoPrint.Bottom,		GoPrint.Width,			0xFF404040);
	
	if(LanguageEnCn==0)TStaticText::sShow(GoPrint.Left+2, GoPrint.Top+2, GoPrint.Right-2, GoPrint.Top+32, clNearWhite, clBlue,  "按确认键开始打印并返回");	
	else TStaticText::sShow(GoPrint.Left+2, GoPrint.Top+2, GoPrint.Right-2, GoPrint.Top+32, clNearWhite, clBlue,  "Confirm Print Or Return");
}

void TQueryMsg::DrawPrintWhat()
{
	if(LanguageEnCn==0)TextRender_string24(GoPrint.Left + 150, GoPrint.Top + 66, clNearBlack, clFrmFace, "打印当前选定条目");
	else TextRender_string24(GoPrint.Left + 150, GoPrint.Top + 66, clNearBlack, clFrmFace, "Print Current Item");
	return;
	if(PrintWhat == 0){
		TextRender_string24(GoPrint.Left + 150, GoPrint.Top + 66, clNearBlack, clFrmFace, "当前选定条目");
	}else
	if(PrintWhat == 1){
		TextRender_string24(GoPrint.Left + 150, GoPrint.Top + 66, clNearBlack, clFrmFace, "当前选定条目");
		//TextRender_string24(GoPrint.Left + 150, GoPrint.Top + 66, clNearBlack, clTeal, "  当前页面  ");
	}else{
		TextRender_string24(GoPrint.Left + 150, GoPrint.Top + 66, clNearBlack, clFrmFace, "当前选定条目");
		//TextRender_string24(GoPrint.Left + 150, GoPrint.Top + 66, clNearBlack, clTeal, "  全部信息  ");
	}
}

void TQueryMsg::Show(void)
{
	DrawSelf();
	Caption.Enable = 1;
	DrawCaption();
	if(IsInputTime){
		DrawBoxTime();
	}else{
		DrawListAll();
	}
	DrawItemCount(Count);
	ShowTip();
}

void TQueryMsg::ReDraw(void)
{
	Caption.Enable = 1;
	//DrawCaption();
	if(IsInputTime){
		DrawBoxTime();
	}else{
		DrawListAll();
	}	
}

void TQueryMsg::FullRedraw(int Prm)
{
	if(Prm == 0){
		DrawSelf();
		Caption.Enable = 1;
		DrawCaption();
		if(IsAllTime){
			DrawListAll();
		}else{
			DrawListInTime();
		}
		if(IsInputTime){
			DrawBoxTimeOutLine();
			DrawBoxTimeString();
			DrawIfAllTime();
			DrawTimeEntryNoUpdate();
			if(TimeEditFrom > -1){
				SetEditStart();
			}else{
				DrawBoxTimeString();
				DrawIfAllTime();
				DrawTimeEntry();
			}
		}else
		if(IsGoPrint){
			DrawGoPrintOutLine();
			DrawPrintWhat();
		}
		DrawItemCount(Count);
		ShowTip();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		if(IsAllTime){
			DrawListAll();
		}else{
			DrawListInTime();
		}
		if(IsInputTime){
			DrawBoxTimeOutLine();
			DrawBoxTimeString();
			DrawIfAllTime();
			DrawTimeEntryNoUpdate();
			if(TimeEditFrom > -1){
				SetEditStart();
			}else{
				DrawBoxTimeString();
				DrawIfAllTime();
				DrawTimeEntry();
			}
		}else
		if(IsGoPrint){
			DrawGoPrintOutLine();
			DrawPrintWhat();
		}
		ShowTip();
	}
}

void TQueryMsg::ShowTip()
{
	if(LanguageEnCn==0){
		if(  (IsInputTime == 0) && (IsGoPrint == 0) ){
			//TextRender_string24(40, 410, clNearBlack, Color, "左右键翻页, 上下键加减一条目, 设置键设定时间段, TAB键去打印");
			TextRender_string24(40, 410, clNearBlack, Color, "左右键翻页, 上下键加减一条目, TAB键去打印");
		}
		if(IsInputTime){
			TextRender_string24(40, 410, clNearBlack, Color, "上下左右键切换或编辑条目, 数字键删除键编辑                 ");
		}else
		if(IsGoPrint){
			TextRender_string24(40, 410, clNearBlack, Color, "按确定键打印当前条目                                       ");
		}
	}else{
		if(  (IsInputTime == 0) && (IsGoPrint == 0) ){
			//TextRender_string24(40, 410, clNearBlack, Color, "左右键翻页, 上下键加减一条目, 设置键设定时间段, TAB键去打印");
			TextRender_string24(40, 410, clNearBlack, Color, "Left,Right,Up,Down:Switch item; TAB To Print              ");
		}
		if(IsInputTime){
			TextRender_string24(40, 410, clNearBlack, Color, "Left,Right,Up,Down To Switch Item To Edit; Num Key To Edit");
		}else
		if(IsGoPrint){
			TextRender_string24(40, 410, clNearBlack, Color, "Press The OK Key To Print The Current Item                ");
		}
	}
}

void TQueryMsg::SetEditStart(void)
{
	int x,y,w,h;
	
	IsEditing =1;
	
	ReStoreP0 = TimeEditFrom;
	ReStoreP1 = TimeIndex;
	
	x = BoxBoundTime.Left + 80 + (ReStoreP1 * 48);
	y = BoxBoundTime.Top + 140 + (ReStoreP0 * 112);
	w = 36;
	h = 32;
	
	vEdit.Init(x,y,w,h,0,0,1,clTeal);
	vEdit.Color = clTeal;
	vEdit.SetMaxLen(2);
	if(ReStoreP0 ==0){
		vEdit.Str.Text[0] = vtTextFrom[ReStoreP1][0];
		vEdit.Str.Text[1] = vtTextFrom[ReStoreP1][1];
		vEdit.Str.Text[2] = 0;
	}else
	if(ReStoreP0 ==1){
		vEdit.Str.Text[0] = vtTextTo[ReStoreP1][0];
		vEdit.Str.Text[1] = vtTextTo[ReStoreP1][1];
		vEdit.Str.Text[2] = 0;
	}
	vEdit.Pst = -1;
	vEdit.Show();
	vEdit.ShowCursor(clNearWhite);
}

void TQueryMsg::RestoreFromEdit(void)
{
	int x,y,w,h;
	
	if(IsEditing == 0)return;
	
	x = BoxBoundTime.Left + 80 + (ReStoreP1 * 48);
	y = BoxBoundTime.Top + 140 + (ReStoreP0 * 112);
	w = 36;
	h = 32;
	
	RectFillRender(x, y, x+w-1, y+h-1, clFrmFace);
	if(ReStoreP0 == 0){
		vtTextFrom[ReStoreP1][0] = vEdit.Str.Text[0];
		vtTextFrom[ReStoreP1][1] = vEdit.Str.Text[1];
		vtTextFrom[ReStoreP1][2] = 0;
		TextRender_string24(x+3, y+3, clNearBlack, vtTextFrom[ReStoreP1]);
	}else
	if(ReStoreP0 == 1){
		vtTextTo[ReStoreP1][0] = vEdit.Str.Text[0];
		vtTextTo[ReStoreP1][1] = vEdit.Str.Text[1];
		vtTextTo[ReStoreP1][2] = 0;
		TextRender_string24(x+3, y+3, clNearBlack, vtTextTo[ReStoreP1]);
	}
	IsEditing = 0;
}

void TQueryMsg::FillDescpText(int aInx)
{
	volatile unsigned int SdAddr, Addr;
	unsigned int d,e;
	unsigned int data;
	unsigned char aP0;
	unsigned char aP1;
	unsigned char aType;
	
	//Fire History
	Text.D32[0] = 0x20202020;
	Text.D32[1] = 0x20202020;
	Text.D32[2] = 0x20202020;
	Text.D32[3] = 0x20202020;
	Text.D32[4] = 0x20202020;
	Text.D32[5] = 0x20202020;
	
	Addr = Record.Buf4Fire.SdramAddrStart + aInx * 64;
	if(Addr >= (dSdramFireHistoryContentBaseAddr + dSdramHistoryBytesLength) )
		SdAddr = Addr - dSdramHistoryBytesLength;
	else
		SdAddr = Addr;
	for(d=7; d<15; d++){
		Text.D32[d-1] = *(volatile unsigned int *)(SdAddr + d * 4); 
	}
	Text.D32[14] =0;
	TMyString::sFillFixLen(Text.Text, dLineMaxLen);
}

void TQueryMsg::FillDescpTextEmpty()
{
	Text.Text[0] = ' ';
	Text.Text[1] = '\0';
	TMyString::sFillFixLen(Text.Text, dLineMaxLen);
}

void TQueryMsg::DrawListAll(void)
{
	volatile unsigned int i, aLen;
	int aInx, aTopInx;
	volatile unsigned int TextClr, bClr, Wrote;
	char *p, *fp;
	const char *tText;
	
	aInx = TopIndex;
	Wrote = 0;
	for(i=0;i<ListLineCnt;i++){
		aTopInx = i*2;
		if(aInx > (Count-1))break;
		if(SelectedIndex != aInx){
			bClr = Color;
			TextClr = clNearBlack;
		}else{
			bClr = clBlue;
			TextClr = clNearWhite;
		}
		if(List.Inx[i] > -1){
			if(List.What[i] == 0){
				MsgFire->FillText(List.Inx[i], aInx);
				p = MsgFire->Text.Text;
			}else
			if(List.What[i] == 1){
				MsgLinkage->FillText(List.Inx[i], aInx);
				p = MsgLinkage->Text.Text;
			}else
			if(List.What[i] == 2){
				MsgSv->FillText(List.Inx[i], aInx);
				p = MsgSv->Text.Text;
			}else
			if(List.What[i] == 3){
				MsgFault->FillText(List.Inx[i], aInx);
				p = MsgFault->Text.Text;
			}else
			if(List.What[i] == 4){
				MsgOperate->FillText(List.Inx[i], aInx);
				p = MsgOperate->Text.Text;
			}
			
			TMyString::sFillFixLen(p, dLineMaxLen);
			TMyString::sBrokeFixLen(p, dLineMaxLen);
			RectFillRender(MsgX0, stAllTop[aTopInx] + 24, MsgX0 +  767, stAllTop[aTopInx +1]-6-1, bClr);
			TextRender_string24(MsgX0, 	stAllTop[aTopInx +0], TextClr, bClr, p);
			
			if(List.What[i] == 0){
				MsgFire->FillDescpText(List.Inx[i]);
				p = MsgFire->Text.Text;
				fp =p;
				fp[4] = fp[0];
				fp[5] = fp[1];
				fp[6] = fp[2];
				fp[7] = fp[3];
				if(LanguageEnCn==0)tText = sCharFire; else tText= sCharFireEn;
				fp[0] = tText[0];
				fp[1] = tText[1];
				fp[2] = tText[2];
				fp[3] = tText[3];
			}else
			if(List.What[i] == 1){
				MsgLinkage->FillDescpText(List.Inx[i]);
				p = MsgLinkage->Text.Text;
				fp =p;
				fp[4] = fp[0];
				fp[5] = fp[1];
				fp[6] = fp[2];
				fp[7] = fp[3];
				if(LanguageEnCn==0)tText = sCharLinkage; else tText= sCharLinkageEn;
				fp[0] = tText[0];
				fp[1] = tText[1];
				fp[2] = tText[2];
				fp[3] = tText[3];
			}else
			if(List.What[i] == 2){
				MsgSv->FillDescpText(List.Inx[i]);
				p = MsgSv->Text.Text;
				fp =p;
				fp[4] = fp[0];
				fp[5] = fp[1];
				fp[6] = fp[2];
				fp[7] = fp[3];
				if(LanguageEnCn==0)tText = sCharSv; else tText= sCharSvEn;
				fp[0] = tText[0];
				fp[1] = tText[1];
				fp[2] = tText[2];
				fp[3] = tText[3];
			}else
			if(List.What[i] == 3){
				MsgFault->FillDescpText(List.Inx[i]);
				p = MsgFault->DescpText.Text;
				fp =p;
				//fp[0] = sCharFault[0];
				//fp[1] = sCharFault[1];
				//fp[2] = sCharFault[2];
				//fp[3] = sCharFault[3];
			}else
			if(List.What[i] == 4){
				MsgOperate->FillDescpText(List.Inx[i]);
				p = MsgOperate->Text.Text;
				fp =p;
				if(fp[0] == 0x20){
					if(LanguageEnCn==0)tText = sCharOpa; else tText= sCharOpaEn;
					fp[0] = tText[0];
					fp[1] = tText[1];
					fp[2] = tText[2];
					fp[3] = tText[3];
				}
			}		
			
			TextRender_string24(MsgX0, 	stAllTop[aTopInx +1]-6, TextClr, bClr, p);
		}else{
			Text.Text[0] = ' ';
			Text.Text[1] = 0;
			TMyString::sFillFixLen(Text.Text, dLineMaxLen);
			RectFillRender(MsgX0, stAllTop[aTopInx] + 24, MsgX0 +  767, stAllTop[aTopInx +1]-6-1, Color);
			TextRender_string24(MsgX0, 	stAllTop[aTopInx +0], 	clWhite, Color, Text.Text);
			FillDescpTextEmpty();
			TextRender_string24(MsgX0, 	stAllTop[aTopInx +1]-6, clNearBlack, Color, Text.Text);
			FillDescpTextEmpty();
		}
		aInx++;
		Wrote++;
	}
	for(i=Wrote; i<ListLineCnt; i++){
		Text.Text[0] = ' ';
		Text.Text[1] = 0;
		TMyString::sFillFixLen(Text.Text, dLineMaxLen);
		aTopInx = i*2;
		RectFillRender(MsgX0, stAllTop[aTopInx] + 24, MsgX0 +  767, stAllTop[aTopInx +1]-6-1, Color);
		TextRender_string24(MsgX0, 	stAllTop[aTopInx +0], 	clWhite, Color, Text.Text);
		FillDescpTextEmpty();
		TextRender_string24(MsgX0, 	stAllTop[aTopInx +1]-6, clNearBlack, Color, Text.Text);
		FillDescpTextEmpty();
	}
	aInx = TopIndex;
	if(aInx <0)aInx = 0;
	TVScrollBar::sDrawByPoint(777, stAllTop[0], 795, stAllTop[7] + 24, clBlue , clGray , Count, 8, TopIndex);
}

void TQueryMsg::DrawListAllUp(void)
{
	/*volatile unsigned int i, aLen;
	int aInx, aTopInx;
	volatile unsigned int bClr, Wrote;
	char *p, *fp;
	
	aInx = TopIndex;
	Wrote = 0;
	for(i=ListLineCnt-1;i>-1;i--){
		aTopInx = i*2;
		if(aInx > (Count-1))break;
		if(SelectedIndex != aInx)bClr = Color;
		else bClr = clBlue;
		if(List.Inx[i] < 0){
			break;
		}
		if(List.What[i] == 0){
			MsgFire->FillText(List.Inx[i], aInx);
			p = MsgFire->Text.Text;
		}else
		if(List.What[i] == 1){
			MsgLinkage->FillText(List.Inx[i], aInx);
			p = MsgLinkage->Text.Text;
		}else
		if(List.What[i] == 2){
			MsgSv->FillText(List.Inx[i], aInx);
			p = MsgSv->Text.Text;
		}else
		if(List.What[i] == 3){
			MsgFault->FillText(List.Inx[i], aInx);
			p = MsgFault->Text.Text;
		}else
		if(List.What[i] == 4){
			MsgOperate->FillText(List.Inx[i], aInx);
			p = MsgOperate->Text.Text;
		}
		
		TMyString::sFillFixLen(p, dLineMaxLen);
		TMyString::sBrokeFixLen(p, dLineMaxLen);
		RectFillRender(MsgX0, stAllTop[aTopInx] + 24, MsgX0 +  767, stAllTop[aTopInx +1]-6-1, bClr);
		TextRender_string24(MsgX0, 	stAllTop[aTopInx +0], clNearBlack, bClr, p);
		
		if(List.What[i] == 0){
			MsgFire->FillDescpText(List.Inx[i]);
			p = MsgFire->Text.Text;
			fp =p;
			fp[0] = sCharFire[0];
			fp[1] = sCharFire[1];
			fp[2] = sCharFire[2];
			fp[3] = sCharFire[3];
		}else
		if(List.What[i] == 1){
			MsgLinkage->FillDescpText(List.Inx[i]);
			p = MsgLinkage->Text.Text;
			fp =p;
			fp[0] = sCharLinkage[0];
			fp[1] = sCharLinkage[1];
			fp[2] = sCharLinkage[2];
			fp[3] = sCharLinkage[3];
		}else
		if(List.What[i] == 2){
			MsgSv->FillDescpText(List.Inx[i]);
			p = MsgSv->Text.Text;
			fp =p;
			fp[0] = sCharSv[0];
			fp[1] = sCharSv[1];
			fp[2] = sCharSv[2];
			fp[3] = sCharSv[3];
		}else
		if(List.What[i] == 3){
			MsgFault->FillDescpText(List.Inx[i]);
			p = MsgFault->DescpText.Text;
			fp =p;
			//fp[0] = sCharFault[0];
			//fp[1] = sCharFault[1];
			//fp[2] = sCharFault[2];
			//fp[3] = sCharFault[3];
		}else
		if(List.What[i] == 4){
			MsgOperate->FillDescpText(List.Inx[i]);
			p = MsgOperate->Text.Text;
			fp =p;
			fp[0] = sCharOpa[0];
			fp[1] = sCharOpa[1];
			fp[2] = sCharOpa[2];
			fp[3] = sCharOpa[3];
		}		
		
		TextRender_string24(MsgX0, 	stAllTop[aTopInx +1]-6, clNearBlack, bClr, p);
		aInx++;
		Wrote++;
	}
	for(i=ListLineCnt - 1 - Wrote; i>-1; i--){
		Text.Text[0] = ' ';
		Text.Text[1] = 0;
		TMyString::sFillFixLen(Text.Text, dLineMaxLen);
		aTopInx = i*2;
		RectFillRender(MsgX0, stAllTop[aTopInx] + 24, MsgX0 +  767, stAllTop[aTopInx +1]-6-1, Color);
		TextRender_string24(MsgX0, 	stAllTop[aTopInx +0], 	clWhite, Color, Text.Text);
		FillDescpTextEmpty();
		TextRender_string24(MsgX0, 	stAllTop[aTopInx +1]-6, clNearBlack, Color, Text.Text);
		FillDescpTextEmpty();
	}
	aInx = TopIndex;
	if(aInx <0)aInx = 0;
	TVScrollBar::sDrawByPoint(777, stAllTop[0], 795, stAllTop[7] + 24, clBlue , clGray , Count, 8, TopIndex);*/
}

void TQueryMsg::DrawListInTime(void)
{
	volatile unsigned int i, aLen;
	int aInx, aTopInx;
	volatile unsigned int bClr, Wrote;
	
	Wrote = 0;
	if(InTimeCount >0){
		i = InTimeTopIndex;
		for(;i<(InTimeStopIndex+1);i++){
			if(InTimeSelectedIndex != (InTimeTopIndex + Wrote))bClr = Color; 
			else bClr = clBlue;
			aInx = InTimeTopIndex - InTimeStartIndex + Wrote;
			//FillText(i, aInx);
			TMyString::sFillFixLen(Text.Text, dLineMaxLen);
			TMyString::sBrokeFixLen(Text.Text, dLineMaxLen);
			aTopInx = Wrote *2;
			RectFillRender(MsgX0, stAllTop[aTopInx] + 24, MsgX0 +  767, stAllTop[aTopInx +1]-6-1, bClr);
			TextRender_string24(MsgX0, 	stAllTop[aTopInx +0], clNearBlack, bClr, Text.Text); 
			FillDescpText(i);
			TextRender_string24(DescpX0, 	stAllTop[aTopInx +1]-6, clNearBlack, bClr, Text.Text); 

			Wrote++;
			if(Wrote >= ListLineCnt){
				break;
			}
		}
	}
	for(i=Wrote; i<ListLineCnt; i++){
		Text.Text[0] = ' ';
		Text.Text[1] = 0;
		TMyString::sFillFixLen(Text.Text, dLineMaxLen);
		aTopInx = i *2;
		RectFillRender(MsgX0, stAllTop[aTopInx] + 24, MsgX0 +  767, stAllTop[aTopInx +1]-6-1, Color);
		TextRender_string24(MsgX0, 	stAllTop[aTopInx +0], clWhite, Color, Text.Text);
		FillDescpTextEmpty();
		TextRender_string24(DescpX0, 	stAllTop[aTopInx +1]-6, clWhite, Color, Text.Text);
	}
	aInx = InTimeTopIndex - InTimeStartIndex;
	if(aInx <0)aInx = 0;
	TVScrollBar::sDrawByPoint(777, stAllTop[0], 795, stAllTop[7] + 24, clBlue , clGray , InTimeCount, 8, aInx);
}

void TQueryMsg::LoadData()
{
	MsgFire->LoadData();
	MsgLinkage->LoadData();
	MsgSv->LoadData();
	MsgFault->LoadData();
	MsgOperate->LoadData();
	
	CountFire 		=MsgFire->Count;
	CountLinkage 	=MsgLinkage->Count;
	CountSv		=MsgSv->Count;
	CountFault		=MsgFault->Count;
	CountOperate	=MsgOperate->Count;
	
	CountTTFire 	=MsgFire->Count;
	CountTTLinkage 	=MsgLinkage->Count;
	CountTTSv		=MsgSv->Count;
	CountTTFault	=MsgFault->Count;
	CountTTOperate	=MsgOperate->Count;
	
	InxScope.End.Fire = CountFire -1;
	InxScope.End.Lnk = CountLinkage -1;
	InxScope.End.Sv = CountSv -1;
	InxScope.End.Fault = CountFault -1;
	InxScope.End.Opa = CountOperate -1;
	
	InxScope.Begin.Fire = 0;
	InxScope.Begin.Lnk = 0;
	InxScope.Begin.Sv = 0;
	InxScope.Begin.Fault = 0;
	InxScope.Begin.Opa = 0;
	
	Inx[0] = InxScope.End.Fire;
	Inx[1] = InxScope.End.Lnk;
	Inx[2] = InxScope.End.Sv;
	Inx[3] = InxScope.End.Fault;
	Inx[4] = InxScope.End.Opa;
	
	Count = CountFire + CountLinkage + CountSv + CountFault + CountOperate;
	CountTT = Count;
	
	TopIndex = Count - ListLineCnt;
	if(TopIndex < 0)TopIndex = 0;
	if(Count >0){
		SelectedIndex = Count -1;
	}else{
		SelectedIndex = TopIndex;
	}
	
	FindMsgPrv4Entries(0xFFFF'FFFF'FFFF'0000);
}



unsigned long long TQueryMsg::GetLongLongInx(int What, int aInx)
{
	unsigned int Addr, SdAddr, d;
	TDMix64 aLLInx;
	if(What == 0){
		Addr = Record.Buf4Fire.SdramAddrStart + aInx * 64;
		if(Addr >= (dSdramFireHistoryContentBaseAddr + dSdramHistoryBytesLength) )
			SdAddr = Addr - dSdramHistoryBytesLength;
		else
			SdAddr = Addr;
		aLLInx.D32[0] = *(volatile unsigned int *)(SdAddr + 0);
		aLLInx.D32[1] = *(volatile unsigned int *)(SdAddr + 4);
		return aLLInx.D64;
	}else
	if(What == 1){
		Addr = Record.Buf4Linkage.SdramAddrStart + aInx * 64;
		if(Addr >= (dSdramLinkageHistoryContentBaseAddr + dSdramHistoryBytesLength) )
			SdAddr = Addr - dSdramHistoryBytesLength;
		else
			SdAddr = Addr;
		aLLInx.D32[0] = *(volatile unsigned int *)(SdAddr + 0);
		aLLInx.D32[1] = *(volatile unsigned int *)(SdAddr + 4);
		return aLLInx.D64;
	}else
	if(What == 2){
		Addr = Record.Buf4Sv.SdramAddrStart + aInx * 64;
		if(Addr >= (dSdramSvHistoryContentBaseAddr + dSdramHistoryBytesLength) )
			SdAddr = Addr - dSdramHistoryBytesLength;
		else
			SdAddr = Addr;
		aLLInx.D32[0] = *(volatile unsigned int *)(SdAddr + 0);
		aLLInx.D32[1] = *(volatile unsigned int *)(SdAddr + 4);
		return aLLInx.D64;
	}else
	if(What == 3){
		Addr = Record.Buf4Fault.SdramAddrStart + aInx * 64;
		if(Addr >= (dSdramFaultHistoryContentBaseAddr + dSdramHistoryBytesLength) )
			SdAddr = Addr - dSdramHistoryBytesLength;
		else
			SdAddr = Addr;
		aLLInx.D32[0] = *(volatile unsigned int *)(SdAddr + 0);
		aLLInx.D32[1] = *(volatile unsigned int *)(SdAddr + 4);
		return aLLInx.D64;
	}else
	if(What == 3){
		Addr = Record.Buf4Operate.SdramAddrStart + aInx * 64;
		if(Addr >= (dSdramOperateHistoryContentBaseAddr + dSdramHistoryBytesLength) )
			SdAddr = Addr - dSdramHistoryBytesLength;
		else
			SdAddr = Addr;
		aLLInx.D32[0] = *(volatile unsigned int *)(SdAddr + 0);
		aLLInx.D32[1] = *(volatile unsigned int *)(SdAddr + 4);
		return aLLInx.D64;
	}
	return 1;
}


void TQueryMsg::LoadMsgPrv16(int What)
{
	unsigned int Addr, SdAddr;
	int aInx, d;
	if(What == 0){
		for(d=15; d>-1; d--){
			ListPreLoad[0].llIndex[d].D32[0] = 0;
			ListPreLoad[0].llIndex[d].D32[1] = 0;
			ListPreLoad[0].Inx[d] = -1;
		}
		if(CountFire <1)return;
		aInx = Inx[0];
		for(d=15; d>-1; d--){
			if(aInx <InxScope.Begin.Fire)break;
			Addr = Record.Buf4Fire.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramFireHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[0].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[0].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[0].Inx[d] = aInx;
			aInx--;
		}
	}else
	if(What == 1){
		for(d=15; d>-1; d--){
			ListPreLoad[1].llIndex[d].D32[0] = 0;
			ListPreLoad[1].llIndex[d].D32[1] = 0;
			ListPreLoad[1].Inx[d] = -1;
		}
		if(CountLinkage <1)return;
		aInx = Inx[1];
		for(d=15; d>-1; d--){
			if(aInx <InxScope.Begin.Lnk)return;
			Addr = Record.Buf4Linkage.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramLinkageHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[1].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[1].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[1].Inx[d] = aInx;
			aInx--;
		}
	}else
	if(What == 2){
		for(d=15; d>-1; d--){
			ListPreLoad[2].llIndex[d].D32[0] = 0;
			ListPreLoad[2].llIndex[d].D32[1] = 0;
			ListPreLoad[2].Inx[d] = -1;
		}
		if(CountSv <1)return;
		aInx = Inx[2];
		for(d=15; d>-1; d--){
			if(aInx <InxScope.Begin.Sv)return;
			Addr = Record.Buf4Sv.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramSvHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[2].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[2].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[2].Inx[d] = aInx;
			aInx--;
		}
	}else
	if(What == 3){
		for(d=15; d>-1; d--){
			ListPreLoad[3].llIndex[d].D32[0] = 0;
			ListPreLoad[3].llIndex[d].D32[1] = 0;
			ListPreLoad[3].Inx[d] = -1;
		}
		if(CountFault <1)return;
		aInx = Inx[3];
		for(d=15; d>-1; d--){
			if(aInx <InxScope.Begin.Fault)return;
			Addr = Record.Buf4Fault.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramFaultHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[3].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[3].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[3].Inx[d] = aInx;
			aInx--;
		}
	}else
	if(What == 4){
		for(d=15; d>-1; d--){
			ListPreLoad[4].llIndex[d].D32[0] = 0;
			ListPreLoad[4].llIndex[d].D32[1] = 0;
			ListPreLoad[4].Inx[d] = -1;
		}
		if(CountOperate <1)return;
		aInx = Inx[4];
		for(d=15; d>-1; d--){
			if(aInx <InxScope.Begin.Opa)return;
			Addr = Record.Buf4Operate.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramOperateHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[4].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[4].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[4].Inx[d] = aInx;
			aInx--;
		}
	}
}


void TQueryMsg::LoadMsgFwd16(int What)
{
	unsigned int Addr, SdAddr;
	int aInx, d;
	if(What == 0){
		for(d=0; d<16; d++){
			ListPreLoad[0].llIndex[d].D32[0] = 0;
			ListPreLoad[0].llIndex[d].D32[1] = 0;
			ListPreLoad[0].Inx[d] = -1;
		}
		if(CountFire <1)return;
		aInx = Inx[0];
		for(d=0; d<16; d++){
			if(aInx > InxScope.End.Fire)break;
			Addr = Record.Buf4Fire.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramFireHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[0].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[0].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[0].Inx[d] = aInx;
			aInx++;
		}
	}else
	if(What == 1){
		for(d=0; d<16; d++){
			ListPreLoad[1].llIndex[d].D32[0] = 0;
			ListPreLoad[1].llIndex[d].D32[1] = 0;
			ListPreLoad[1].Inx[d] = -1;
		}
		if(CountLinkage <1)return;
		aInx = Inx[1];
		for(d=0; d<16; d++){
			if(aInx > InxScope.End.Lnk)return;
			Addr = Record.Buf4Linkage.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramLinkageHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[1].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[1].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[1].Inx[d] = aInx;
			aInx++;
		}
	}else
	if(What == 2){
		for(d=0; d<16; d++){
			ListPreLoad[2].llIndex[d].D32[0] = 0;
			ListPreLoad[2].llIndex[d].D32[1] = 0;
			ListPreLoad[2].Inx[d] = -1;
		}
		if(CountSv <1)return;
		aInx = Inx[2];
		for(d=0; d<16; d++){
			if(aInx > InxScope.End.Fault)return;
			Addr = Record.Buf4Sv.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramSvHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[2].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[2].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[2].Inx[d] = aInx;
			aInx++;
		}
	}else
	if(What == 3){
		for(d=0; d<16; d++){
			ListPreLoad[3].llIndex[d].D32[0] = 0;
			ListPreLoad[3].llIndex[d].D32[1] = 0;
			ListPreLoad[3].Inx[d] = -1;
		}
		if(CountFault <1)return;
		aInx = Inx[3];
		for(d=0; d<16; d++){
			if(aInx > InxScope.End.Fault)return;
			Addr = Record.Buf4Fault.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramFaultHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[3].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[3].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[3].Inx[d] = aInx;
			aInx++;
		}
	}else
	if(What == 4){
		for(d=0; d<16; d++){
			ListPreLoad[4].llIndex[d].D32[0] = 0;
			ListPreLoad[4].llIndex[d].D32[1] = 0;
			ListPreLoad[4].Inx[d] = -1;
		}
		if(CountOperate <1)return;
		aInx = Inx[4];
		for(d=0; d<16; d++){
			if(aInx > InxScope.End.Opa)return;
			Addr = Record.Buf4Operate.SdramAddrStart + aInx * 64;
			if(Addr >= (dSdramOperateHistoryContentBaseAddr + dSdramHistoryBytesLength) )
				SdAddr = Addr - dSdramHistoryBytesLength;
			else
				SdAddr = Addr;
			ListPreLoad[4].llIndex[d].D32[0] = *(volatile unsigned int *)(SdAddr + 0);
			ListPreLoad[4].llIndex[d].D32[1] = *(volatile unsigned int *)(SdAddr + 4);
			ListPreLoad[4].Inx[d] = aInx;
			aInx++;
		}
	}
}


void TQueryMsg::FindBigMsgPrv(unsigned long long InxLimite)
{
	int InxInt;
	int i, x, z;
	
	MsgFound.What = 1000;
	MsgFound.sInx.D64 = 0;
	for(i=0; i<5; i++){
		for(x=0; x<16; x++){
			if( (ListPreLoad[i].llIndex[x].D64 > MsgFound.sInx.D64) && (ListPreLoad[i].llIndex[x].D64 < InxLimite) && (ListPreLoad[i].Inx[x] > -1) ){
					MsgFound.What = i;
					MsgFound.sInx.D64 = ListPreLoad[i].llIndex[x].D64;
					MsgFound.Inx = ListPreLoad[i].Inx[x];
			}
		}
	}
	if(MsgFound.What < 5){
		Inx[MsgFound.What] = MsgFound.Inx;
	}
}

void TQueryMsg::FindMinMsgFwd(unsigned long long InxLimite)
{
	int InxInt;
	int i, x, z;
	
	MsgFound.What = 1000;
	MsgFound.sInx.D64 = 0xFFFF'FFFF'FFFF'0000;
	for(i=0; i<5; i++){
		for(x=0; x<16; x++){
			if( (ListPreLoad[i].llIndex[x].D64 < MsgFound.sInx.D64) && (ListPreLoad[i].llIndex[x].D64 > InxLimite) && (ListPreLoad[i].Inx[x] > -1)){
					MsgFound.What = i;
					MsgFound.sInx.D64 = ListPreLoad[i].llIndex[x].D64;
					MsgFound.Inx = ListPreLoad[i].Inx[x];
			}
		}
	}
	if(MsgFound.What < 5){
		Inx[MsgFound.What] = MsgFound.Inx;
	}
}

void TQueryMsg::FindMsgPrvEntry(unsigned long long aInxLimite)
{
	int i, x, z;
	int WhatMax;
	
	LoadMsgPrv16(0);
	LoadMsgPrv16(1);
	LoadMsgPrv16(2);
	LoadMsgPrv16(3);
	LoadMsgPrv16(4);

	FindBigMsgPrv(aInxLimite);
	
	if(MsgFound.What < 5){
		for(i=3; i>0; i--){
			List.What[i] = List.What[i-1];
			List.Inx[i] = List.Inx[i-1];
			List.sInx[i].D64 = List.sInx[i-1].D64;
		}
		List.What[0] = MsgFound.What;
		List.Inx[0] = MsgFound.Inx;
		List.sInx[0].D64 = MsgFound.sInx.D64;
	}
	CheckFixTopIndex();
}

void TQueryMsg::FindMsgFwdEntry(unsigned long long aInxLimite)
{
	int i, x, z;
	int WhatMax;
	int Found[4][2];
	
	LoadMsgFwd16(0);
	LoadMsgFwd16(1);
	LoadMsgFwd16(2);
	LoadMsgFwd16(3);
	LoadMsgFwd16(4);

	FindMinMsgFwd(aInxLimite);
	
	if(MsgFound.What < 5){
		for(i=0; i<3; i++){
			List.What[i] = List.What[i+1];
			List.Inx[i] = List.Inx[i+1];
			List.sInx[i].D64 = List.sInx[i+1].D64;
		}
		List.What[3] = MsgFound.What;
		List.Inx[3] = MsgFound.Inx;
		List.sInx[3].D64 = MsgFound.sInx.D64;
	}
	CheckFixTopIndex();
}

void TQueryMsg::FindMsgPrv4Entries(unsigned long long InxLimite)
{
	unsigned long long NewLimite;
	int i;
	
	LoadMsgPrv16(0);
	LoadMsgPrv16(1);
	LoadMsgPrv16(2);
	LoadMsgPrv16(3);
	LoadMsgPrv16(4);
	
	NewLimite = InxLimite;
	for(i=3; i>-1; i--){
		FindBigMsgPrv(NewLimite);
		if(MsgFound.What <5){
			NewLimite = MsgFound.sInx.D64;
			List.What[i] = MsgFound.What;
			List.Inx[i] = MsgFound.Inx;
			List.sInx[i].D64 = MsgFound.sInx.D64;
		}
	}
	CheckFixTopIndex();
}

void TQueryMsg::FindMsgFwd4Entries(unsigned long long InxLimite)
{
	unsigned long long NewLimite;
	int i;
	
	LoadMsgFwd16(0);
	LoadMsgFwd16(1);
	LoadMsgFwd16(2);
	LoadMsgFwd16(3);
	LoadMsgFwd16(4);
	
	NewLimite = InxLimite;
	for(i=0; i<4; i++){
		FindMinMsgFwd(NewLimite);
		if(MsgFound.What <5){
			NewLimite = MsgFound.sInx.D64;
			List.What[i] = MsgFound.What;
			List.Inx[i] = MsgFound.Inx;
			List.sInx[i].D64 = MsgFound.sInx.D64;
		}
	}
	CheckFixTopIndex();
}

void TQueryMsg::FindMsgAtStart()
{
	Inx[0] = InxScope.Begin.Fire;
	Inx[1] = InxScope.Begin.Lnk;
	Inx[2] = InxScope.Begin.Sv;
	Inx[3] = InxScope.Begin.Fault;
	Inx[4] = InxScope.Begin.Opa;
	
	FindMsgFwd4Entries(1);
}

void TQueryMsg::FindMsgAtEnd()
{
	Inx[0] = InxScope.End.Fire;
	Inx[1] = InxScope.End.Lnk;
	Inx[2] = InxScope.End.Sv;
	Inx[3] = InxScope.End.Fault;
	Inx[4] = InxScope.End.Opa;
	TopIndex = Count - ListLineCnt;
	if(TopIndex < 0)TopIndex = 0;
	SelectedIndex =TopIndex;
	FindMsgPrv4Entries(0xFFFF'FFFF'FFFF'0000);	
}

void TQueryMsg::CheckFixTopIndex()
{
	int LineCnt;
	if(  (List.Inx[0] < 0) && (List.Inx[3] < 0)  ){
		FindMsgAtEnd();
	}else
	if(List.Inx[0] < 0){
		TopIndex = 0;
		SelectedIndex = TopIndex;
		FindMsgAtStart();
	}else
	if(List.Inx[3] < 0){
		LineCnt = 3;
		if(List.Inx[2] < 0)LineCnt = 2;
		if(List.Inx[1] < 0)LineCnt = 1;
		if(TopIndex != LineCnt){
			FindMsgAtEnd();
		}
	}
}

unsigned long long TQueryMsg::aTimeGet(int aInx)
{
	volatile unsigned int SdAddr, Addr;

	//Fire History
	Addr = Record.Buf4Fire.SdramAddrStart + aInx * 64;
	if(Addr >= (dSdramFireHistoryContentBaseAddr + dSdramHistoryBytesLength) )
		SdAddr = Addr - dSdramHistoryBytesLength;
	else
		SdAddr = Addr;
	alt.D32.Low4 =  *(volatile unsigned int *)(SdAddr + 12); 
	alt.D32.Hi2 =  *(volatile unsigned int *)(SdAddr + 16);
	alt.D8[6] = 0;
	alt.D8[7] = 0;
	return alt.D64;
}

void TQueryMsg::GetStartStopTime()
{
	int i;

	RestoreFromEdit();
	
	for(i=0; i<6;i++){
		alt.D8[5-i] = (vtTextFrom[i][0] - '0') * 16;
		alt.D8[5-i] += (vtTextFrom[i][1] - '0');
		
	}
	alt.D8[6] = 0;
	alt.D8[7] = 0;
	StartTime = alt.D64;
	
	for(i=0; i<6;i++){
		alt.D8[5-i] = (vtTextTo[i][0] - '0') * 16;
		alt.D8[5-i] += (vtTextTo[i][1] - '0');
		
	}
	alt.D8[6] = 0;
	alt.D8[7] = 0;
	StopTime = alt.D64;
}

void TQueryMsg::FindInTimeEntry()
{
	int aCnt;
	
	MsgFire->LoadData();
	MsgLinkage->LoadData();
	MsgSv->LoadData();
	MsgFault->LoadData();
	MsgOperate->LoadData();
	
	if(CountTTFire){
		MsgFire->StartTime = StartTime;
		MsgFire->StopTime = StopTime;
		MsgFire->FindInTimeEntry();
		
		CountFire = MsgFire->InTimeCount;
		InxScope.Begin.Fire = MsgFire->InTimeStartIndex;
		InxScope.End.Fire = MsgFire->InTimeStopIndex;
	}else{
		CountFire =0;
	}
	
	if(CountTTLinkage){
		MsgLinkage->StartTime = StartTime;
		MsgLinkage->StopTime = StopTime;
		MsgLinkage->FindInTimeEntry();
		
		CountLinkage = MsgLinkage->InTimeCount;
		InxScope.Begin.Lnk = MsgLinkage->InTimeStartIndex;
		InxScope.End.Lnk = MsgLinkage->InTimeStopIndex;
	}else{
		CountLinkage =0;
	}
	
	if(CountTTSv){
		MsgSv->StartTime = StartTime;
		MsgSv->StopTime = StopTime;
		MsgSv->FindInTimeEntry();
		
		CountSv = MsgSv->InTimeCount;
		InxScope.Begin.Sv = MsgSv->InTimeStartIndex;
		InxScope.End.Sv = MsgSv->InTimeStopIndex;
	}else{
		CountSv =0;
	}
	
	if(CountTTFault){
		MsgFault->StartTime = StartTime;
		MsgFault->StopTime = StopTime;
		MsgFault->FindInTimeEntry();
		
		CountFault = MsgFault->InTimeCount;
		InxScope.Begin.Fault = MsgFault->InTimeStartIndex;
		InxScope.End.Fault = MsgFault->InTimeStopIndex;
	}else{
		CountFault =0;
	}
	
	if(CountTTOperate){
		MsgOperate->StartTime = StartTime;
		MsgOperate->StopTime = StopTime;
		MsgOperate->FindInTimeEntry();
		
		CountOperate = MsgOperate->InTimeCount;
		InxScope.Begin.Opa = MsgOperate->InTimeStartIndex;
		InxScope.End.Opa = MsgOperate->InTimeStopIndex;
	}else{
		CountOperate =0;
	}
	
	Count = CountFire + CountLinkage + CountSv + CountFault + CountOperate;
	
	Inx[0] = InxScope.End.Fire;
	Inx[1] = InxScope.End.Lnk;
	Inx[2] = InxScope.End.Sv;
	Inx[3] = InxScope.End.Fault;
	Inx[4] = InxScope.End.Opa;
	
	TopIndex = Count - ListLineCnt;
	if(TopIndex < 0)TopIndex = 0;
	SelectedIndex =TopIndex;
	
	FindMsgPrv4Entries(0xFFFF'FFFF'FFFF'0000);
}


void TQueryMsg::Go4Print(void)
{
	volatile unsigned int i, aLen;
	int aInx, aTopInx;
	volatile unsigned int TextClr, bClr, Wrote;
	char *p, *fp;
	
	aInx = TopIndex;
	for(i=0;i<ListLineCnt;i++){
		if(aInx > (Count-1))break;
		if(SelectedIndex == aInx){
			if(List.Inx[i] > -1){
				if(List.What[i] == 0){
					MsgFire->GetEntry4Print(List.Inx[i]);
					RecordMsg.D32[0] = MsgFire->EntryTmp.fData.Body.D32[0];
					RecordMsg.D32[1] = MsgFire->EntryTmp.fData.Body.D32[1];
					RecordMsg.D32[2] = MsgFire->EntryTmp.fData.Body.D32[2];
					RecordMsg.D32[3] = MsgFire->EntryTmp.fData.Body.D32[3];
					RecordMsg.Splite.UserCode = MsgFire->EntryTmp.fData.UserCode;
					DoPrint.PrintHistory(0, RecordMsg, &MsgFire->Text.Text[24]);
				}else
				if(List.What[i] == 1){
					MsgLinkage->GetEntry4Print(List.Inx[i]);
					RecordMsg.D32[0] = MsgLinkage->EntryTmp.fData.Body.D32[0];
					RecordMsg.D32[1] = MsgLinkage->EntryTmp.fData.Body.D32[1];
					RecordMsg.D32[2] = MsgLinkage->EntryTmp.fData.Body.D32[2];
					RecordMsg.D32[3] = MsgLinkage->EntryTmp.fData.Body.D32[3];
					RecordMsg.Splite.UserCode = MsgLinkage->EntryTmp.fData.UserCode;
					DoPrint.PrintHistory(1, RecordMsg, &MsgLinkage->Text.Text[24]);
				}else
				if(List.What[i] == 2){
					MsgSv->GetEntry4Print(List.Inx[i]);
					RecordMsg.D32[0] = MsgSv->EntryTmp.fData.Body.D32[0];
					RecordMsg.D32[1] = MsgSv->EntryTmp.fData.Body.D32[1];
					RecordMsg.D32[2] = MsgSv->EntryTmp.fData.Body.D32[2];
					RecordMsg.D32[3] = MsgSv->EntryTmp.fData.Body.D32[3];
					RecordMsg.Splite.UserCode = MsgSv->EntryTmp.fData.UserCode;
					DoPrint.PrintHistory(2, RecordMsg, &MsgSv->Text.Text[24]);
				}else
				if(List.What[i] == 3){
					MsgFault->GetEntry4Print(List.Inx[i]);
					RecordMsg.D32[0] = MsgFault->EntryTmp.fData.Body.D32[0];
					RecordMsg.D32[1] = MsgFault->EntryTmp.fData.Body.D32[1];
					RecordMsg.D32[2] = MsgFault->EntryTmp.fData.Body.D32[2];
					RecordMsg.D32[3] = MsgFault->EntryTmp.fData.Body.D32[3];
					RecordMsg.Splite.UserCode = MsgFault->EntryTmp.fData.UserCode;
					DoPrint.PrintHistory(3, RecordMsg, &MsgFault->DescpText.Text[28]);
				}else
				if(List.What[i] == 4){
					MsgOperate->GetEntry4Print(List.Inx[i]);
					RecordMsg.D32[0] = MsgOperate->EntryTmp.fData.Body.D32[0];
					RecordMsg.D32[1] = MsgOperate->EntryTmp.fData.Body.D32[1];
					RecordMsg.D32[2] = MsgOperate->EntryTmp.fData.Body.D32[2];
					RecordMsg.D32[3] = MsgOperate->EntryTmp.fData.Body.D32[3];
					RecordMsg.Splite.UserCode = MsgOperate->EntryTmp.fData.UserCode;
					DoPrint.PrintHistory(4, RecordMsg, &MsgOperate->Text.Text[28]);
				}
			}
		}
		aInx++;
	}
}

TGuiMsgReturn TQueryMsg::KeyIn(unsigned char aKey)
{
	int ind,i,aIndex;
	TGuiMsgReturn aMsg = guiMsgNone;
	//Up or Down One Index___________________
	
	if(  (IsInputTime == 0) && (IsGoPrint == 0) ){
		if(aKey == VK_TAB){
			//Items.SelectedIndex = 0;
			//Lv.SelectedCol = 0;
			//Lv.TopIndex = 0;
			IsGoPrint = 1;
			DrawGoPrintOutLine();
			DrawPrintWhat();
			ShowTip();
		}else
		if(VK_LEFT == aKey){
			aIndex = TopIndex -ListLineCnt;
			if(aIndex > -1){
				TopIndex = aIndex;
				SelectedIndex -= ListLineCnt;
				FindMsgPrv4Entries(List.sInx[0].D64);
			}else{
				if(TopIndex > 0){
					TopIndex = 0;
					SelectedIndex = TopIndex;
					FindMsgAtStart();
				}else{
					FindMsgAtEnd();
				}
			}
			ReDraw();
		}else
		if(VK_RIGHT == aKey){
			aIndex = TopIndex +ListLineCnt;
			if(aIndex < Count){
				TopIndex = aIndex;
				SelectedIndex += ListLineCnt;
				if(SelectedIndex >= Count){
					SelectedIndex = Count-1;
				}
				FindMsgFwd4Entries(List.sInx[3].D64);
			}else{
				TopIndex = 0;
				SelectedIndex = TopIndex;
				FindMsgAtStart();
			}
			ReDraw();
		}else
		if(VK_UP == aKey){
			aIndex = SelectedIndex -1;
			if(aIndex > -1){
				SelectedIndex = aIndex;
				if(SelectedIndex < TopIndex){
					TopIndex = SelectedIndex;
					FindMsgPrvEntry(List.sInx[0].D64);
				}
			}else{
				FindMsgAtEnd();
			}
			ReDraw();
		}else
		if(VK_DOWN == aKey){
			aIndex = SelectedIndex +1;
			if(aIndex < Count){
				SelectedIndex = aIndex;
				if(SelectedIndex > (TopIndex + ListLineCnt -1) ){
					TopIndex++;
					FindMsgFwdEntry(List.sInx[3].D64);
				}
			}else{
				TopIndex = 0;
				SelectedIndex = 0;
				FindMsgAtStart();
			}
			ReDraw();
		}else
		if(aKey == VK_F1 ){
			//Setup Device Property
			//IsInputTime = 1;
			//IsAllTimeTemp = IsAllTime;
			//DrawBoxTimeOutLine();
			//DrawBoxTimeString();
			//DrawIfAllTime();
			//DrawTimeEntry();
			//ShowTip();
		}else
		if(aKey == VK_F2){
			//Query Device Property

		}else
		if(aKey == VK_RETURN){
			aMsg = guiMsgReturn;
		}
	}
	
	if(IsInputTime){
		if(aKey == VK_UP){
			if(TimeEditFrom >0){
				RestoreFromEdit();
				TimeEditFrom =0;
				SetEditStart();
			}else{
				if(TimeEditFrom > -1){
					RestoreFromEdit();
					TimeEditFrom = -1;
					DrawIfAllTime();
				}
			}
		}else
		if(aKey == VK_DOWN){
			if(TimeEditFrom <1){
				if(IsAllTimeTemp == 0){
					if(TimeEditFrom == 0)RestoreFromEdit();
					TimeEditFrom ++;
					SetEditStart();
					DrawIfAllTime();
				}
			}
		}else
		if(aKey == VK_LEFT){
			if(TimeEditFrom > -1){
				if(TimeIndex >0){
					RestoreFromEdit();
					TimeIndex--;
					SetEditStart();
				}
			}else{
				if(IsAllTimeTemp){
					IsAllTimeTemp = 0;
				}else{
					IsAllTimeTemp = 1;
				}
				DrawBoxTimeString();
				DrawIfAllTime();
				DrawTimeEntry();
			}
		}else
		if(aKey == VK_RIGHT){
			if(TimeEditFrom > -1){
				if(TimeIndex <5){
					RestoreFromEdit();
					TimeIndex++;
					SetEditStart();
				}
			}else{
				if(IsAllTimeTemp){
					IsAllTimeTemp = 0;
				}else{
					IsAllTimeTemp = 1;
				}
				DrawBoxTimeString();
				DrawIfAllTime();
				DrawTimeEntry();
			}
		}else
		if(aKey == VK_EXECUTE){
			//Read Here
			if(IsAllTimeTemp){
				IsAllTime = IsAllTimeTemp;
				LoadData();
				DrawItemCount(Count);
				ReDraw();
			}else{
				IsAllTime = IsAllTimeTemp;
				GetStartStopTime();
				FindInTimeEntry();
				DrawItemCount(Count);
				IsInputTime = 0;
				RenderContent();
				ReDraw();
			}
			ShowTip();
		}else
		if(aKey == VK_RETURN){
			IsInputTime = 0;
			RenderContent();
			ReDraw();
			ShowTip();			
		}else{
			if(IsAllTimeTemp == 0){
				if(IsEditing){
					switch(aKey){
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
						case VK_DELETE:
							vEdit.KeyIn(aKey);
							vEdit.Show();
							vEdit.ShowCursor(clNearWhite);
						break;
					}
				}
			}
		}
	}
	
	if(IsGoPrint){
		if(aKey == VK_LEFT){
			if(PrintWhat ==0)PrintWhat=1;
			else PrintWhat = 0;
			DrawPrintWhat();
		}else
		if(aKey == VK_RIGHT){
			if(PrintWhat ==0)PrintWhat=1;
			else PrintWhat = 0;
			DrawPrintWhat();
		}else
		if(aKey == VK_EXECUTE){
			//Print Here
			Go4Print();
			IsGoPrint = 0;
			RenderContent();
			ReDraw();
			ShowTip();
		}else
		if(aKey == VK_RETURN){
			IsGoPrint = 0;
			RenderContent();
			ReDraw();
			ShowTip();
		}
	}
	//____________________________________________________________________<- List View
	
	return aMsg;
}

void TQueryMsg::Task1000Ms()
{
	if(IsShowing){
		if(Count != Record.Buf4Fire.Count){
			TopIndex = Count - ListLineCnt +1;
			if(TopIndex < 0)TopIndex = 0;
			SelectedIndex =TopIndex;
		}
	}
}

int TQueryMsg::ExtRequst(unsigned char Prm)
{
	IsShowing = 0;
	return 0;
}
