#include "LinkageList.h"

static const short svGridX[8] = {20, 44, 92,     140, 164, 212,     400, 600};
static const short xExist = 260;

void TLinkageList::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "联动->编程查询");
		TextRender_string24(svGridX[0], Lv.Grid.Top[0] +4, clNearWhite,  "第");
		TextRender_string24(svGridX[2], Lv.Grid.Top[0] +4, clNearWhite,  "条");
		TextRender_string24(svGridX[3], Lv.Grid.Top[0] +4, clNearWhite,  "共");
		TextRender_string24(svGridX[5], Lv.Grid.Top[0] +4, clNearWhite,  "条");
		TextRender_string24(xExist, Lv.Grid.Top[0] +4, clNearWhite,  "有效");
		TextRender_string24(xExist+48+48, Lv.Grid.Top[0] +4, clNearWhite,  "条");
	
		TextRender_string24(svGridX[6], Lv.Grid.Top[0] +4, clNearWhite,  "执行情况");
		TextRender_string24(svGridX[7], Lv.Grid.Top[0] +4, clNearWhite,  "正常/屏蔽");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Linkage->Expression Overview");
		TextRender_string24(svGridX[0], Lv.Grid.Top[0] +4, clNearWhite,  "Index");
		//TextRender_string24(svGridX[2], Lv.Grid.Top[0] +4, clNearWhite,  "条");
		TextRender_string24(svGridX[3]-36, Lv.Grid.Top[0] +4, clNearWhite,  "Total");
		//TextRender_string24(svGridX[5], Lv.Grid.Top[0] +4, clNearWhite,  "条");
		TextRender_string24(xExist-12, Lv.Grid.Top[0] +4, clNearWhite,  "Valid");
		//TextRender_string24(xExist+48+48, Lv.Grid.Top[0] +4, clNearWhite,  "条");
	
		TextRender_string24(svGridX[6]+20, Lv.Grid.Top[0] +4, clNearWhite,  "Executed");
		TextRender_string24(svGridX[7]+20, Lv.Grid.Top[0] +4, clNearWhite,  "Blocking");
	}
}

void TLinkageList::Init() 
{
	IsShow = 0;
	ShowEntry = 0;
	Tag.Inx =0;
	Tag.TopInx = 0;
	Items.Inx = 0;
	Items.TopInx = 0;
}

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

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

void TLinkageList::DrawSelf(void)
{
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	
	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);

	DrawFixText();
	
	if(LanguageEnCn==0)TextDigitRender4_24(svGridX[0] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Items.Inx+1);
	TextDigitRender4_24(svGridX[3] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, dLINKAGE_MAX_COUNT);
}

void TLinkageList::Show(void)
{	
	IsShow =1;
	RenderBackGround();
	DrawSelf();
	LoadList();
	DrawList();
	Check4Selected();
}

void TLinkageList::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		DrawList();
	}else
	if(Prm == 1){
		if(ShowEntry){
			LinkageProgram.FullRedraw(Prm);
		}else{
			RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
			for(int i=0; i<1; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
			DrawFixText();
			if(LanguageEnCn==0)TextDigitRender4_24(svGridX[0] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Items.Inx+1);
			TextDigitRender4_24(svGridX[3] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, dLINKAGE_MAX_COUNT);
			DrawList();
		}

	}
}

void TLinkageList::LoadList()
{
	Items.Count = InterLink.LinkCount;
	TextDigitRender4_24(xExist+48, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Items.Count);
}

//______________________________________________________________________________________________________________________________

int TLinkageList::GetLnkMask(unsigned int aExp)
{
	unsigned int SysLinkAddr;
	SysLinkAddr = dSdAddrLinkageExp + (aExp * 1024);
	if(*(volatile unsigned char *)(SysLinkAddr + 4) == 'M'){
		return 1;
	}else{
		return 0;
	}	
}
		
void TLinkageList::DrawList()
{
	int i,t;
	unsigned int SysLinkAddr;
	TDMix Idf;
	unsigned int tClr = clNearWhite;
	for(i=1; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	for(i=0, t=Items.TopInx; (i<10) && (t<dLINKAGE_MAX_COUNT); i++, t++){
		TextDigitRender4Right24(svGridX[0], Lv.Grid.Top[i+1] + 4, tClr,  t+1);
		SysLinkAddr = dSdAddrLinkageExp + (t*1024);
		Idf.D32 = *(volatile unsigned int *)(SysLinkAddr);
		if(LanguageEnCn==0){
			if(  (Idf.D8[0] == 'T') && (Idf.D8[1] == 'R') && (Idf.D8[2] == 'U') && (Idf.D8[3] == 'E') ){
				TextRender_string24(280, Lv.Grid.Top[i+1] + 4, tClr,  "已配置");
				if(InterLink.LinkHasStartTabel[t]){
					TextRender_string24(svGridX[6]+20, Lv.Grid.Top[i+1] + 4, tClr,  "已执行");
				}else{
					TextRender_string24(svGridX[6]+20, Lv.Grid.Top[i+1] + 4, clSilver,  "未执行");
				}
				
				if(GetLnkMask(t)){
					TextRender_string24(svGridX[7]+20, Lv.Grid.Top[i+1] + 4, tClr,  "屏蔽");
				}else{
					TextRender_string24(svGridX[7]+20, Lv.Grid.Top[i+1] + 4, tClr,  "正常");
				}
			}else{
				TextRender_string24(280, Lv.Grid.Top[i+1] + 4, tClr,  "------");
				TextRender_string24(svGridX[6]+20, Lv.Grid.Top[i+1] + 4, tClr,  "------");
				TextRender_string24(svGridX[7]+20, Lv.Grid.Top[i+1] + 4, tClr,  "----");
			}
		}else{
			if(  (Idf.D8[0] == 'T') && (Idf.D8[1] == 'R') && (Idf.D8[2] == 'U') && (Idf.D8[3] == 'E') ){
				TextRender_string24(280, Lv.Grid.Top[i+1] + 4, tClr,  "Valid ");
				if(InterLink.LinkHasStartTabel[t]){
					TextRender_string24(svGridX[6]+20, Lv.Grid.Top[i+1] + 4, tClr,  "Exe-ed");
				}else{
					TextRender_string24(svGridX[6]+20, Lv.Grid.Top[i+1] + 4, clSilver,  "------");
				}
				
				if(GetLnkMask(t)){
					TextRender_string24(svGridX[7]+20, Lv.Grid.Top[i+1] + 4, tClr,  "Blocking");
				}else{
					TextRender_string24(svGridX[7]+20, Lv.Grid.Top[i+1] + 4, tClr,  "--------");
				}
			}else{
				TextRender_string24(280, Lv.Grid.Top[i+1] + 4, tClr,  "------");
				TextRender_string24(svGridX[6]+20, Lv.Grid.Top[i+1] + 4, tClr,  "------");
				TextRender_string24(svGridX[7]+20, Lv.Grid.Top[i+1] + 4, tClr,  "--------");
			}
		}
	}
}

void TLinkageList::ClearSelected(void)
{
	int iT;
	iT = Tag.Inx + 1;
	if(iT == 0)return;
	if(iT > 10)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 TLinkageList::Check4Selected(void)
{
	int iT;
	iT = Tag.Inx + 1;
	if(iT == 0)return;
	if(iT > 10)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 TLinkageList::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone, SubReturn;
	
	if(ShowEntry){
		SubReturn = LinkageProgram.KeyIn(aKey);
		if(SubReturn == guiMsgReturn){
			ShowEntry = 0;
			RenderBackGround();
			DrawSelf();
			LoadList();
			DrawList();
			Check4Selected();
		}		
	}else{
		switch(aKey){
			case VK_UP:
				if(Tag.Inx>0){
					ClearSelected();
					Tag.Inx--;
					Items.Inx--;
					Check4Selected();
				}else{
					if(Items.Inx>0){
						ClearSelected();
						Tag.Inx =0;
						Items.Inx--;
						Items.TopInx =Items.Inx;
						DrawList();
						Check4Selected();
					}
				}
				if(LanguageEnCn==0)TextDigitRender4_24(svGridX[0] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Items.Inx+1);
				break;
			case VK_DOWN:
				if(Tag.Inx<9){
					if(Items.Inx<(dLINKAGE_MAX_COUNT-1)){	
						ClearSelected();
						Tag.Inx++;
						Items.Inx++;
						Check4Selected();
					}
				}else{
					Tag.Inx =9;
					if(Items.Inx<(dLINKAGE_MAX_COUNT-1)){
						ClearSelected();
						Items.Inx++;
						Items.TopInx =Items.Inx-9;
						DrawList();
						Check4Selected();
					}
				}
				if(LanguageEnCn==0)TextDigitRender4_24(svGridX[0] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Items.Inx+1);
				break;
			case VK_LEFT:
				int GoLeft;
				if(Items.Inx <1){
					ClearSelected();
					Items.Inx =2040;
					Items.TopInx =2040;
					Tag.Inx = 0;
					DrawList();
					Check4Selected();
				}else{
					GoLeft = (Items.Inx / 10 -1) * 10;
					if(GoLeft < 0){
						GoLeft = 0;
					}
					ClearSelected();
					Items.Inx =GoLeft;
					Items.TopInx =GoLeft;
					Tag.Inx = 0;
					DrawList();
					Check4Selected();
				}
				if(LanguageEnCn==0)TextDigitRender4_24(svGridX[0] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Items.Inx+1);
				break;
			case VK_RIGHT:
				int GoInx;
				if(Items.Inx >= 2040){
					ClearSelected();
					Items.Inx =0;
					Items.TopInx =0;
					Tag.Inx = 0;
					DrawList();
					Check4Selected();
				}else{
					GoInx = (Items.Inx / 10 +1) * 10;
					if(GoInx >= dLINKAGE_MAX_COUNT){
						GoInx = dLINKAGE_MAX_COUNT-10;
						if(GoInx <0)GoInx = 0;
					}
					ClearSelected();
					Items.Inx =GoInx;
					Items.TopInx =GoInx;
					Tag.Inx = 0;
					DrawList();
					Check4Selected();
				}
				if(LanguageEnCn==0)TextDigitRender4_24(svGridX[0] + 24, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Items.Inx+1);
				break;
			case VK_EXECUTE:
				ShowEntry = 1;
				LinkageProgram.Init();
				LinkageProgram.EnterMethod = 1;
				LinkageProgram.Show();
				LinkageProgram.InitPanel();
				LinkageProgram.Skip2Entry(Items.Inx+1);
				break;
			case VK_RETURN:
				IsShow =0;
				aMsg = guiMsgReturn;
				break;
		}
	}
	return aMsg;	
}

int TLinkageList::ExtRequst(unsigned char Prm)
{
	if(ShowEntry){
		LinkageProgram.ExtRequst(Prm);
	}
	IsShow = 0; return 0;
}

