#include "QueryCfg.h"
#include "XBox.h"
#include "Runtime.h"

void TQueryCfg::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;
	BoundCaption.SetBySize(Left,Top,Width, CH);
	Content.Set(Left, Top+CH, Right, Bottom);
	
	BoxBoundCfg.Set(Left + 100, Top+56, Right -145, Bottom -60);
	
	vEditDescp.Init(BoxBoundCfg.Left +10,BoxBoundCfg.Top + 232,530,34,0,0,1,clTeal); 
	
	vEditDescp.Str.FromStr("未设定描述信息");
	vEditDescp.Pst = vEditDescp.Str.GetEndPst();
	vEditDescp.TextColor = clNearBlack;
	vEditDescp.Selected = 0;
	vEditDescp.SetMaxLen(32);

	EditIndex = 0;
	IsEditing = 0;
	
	vtValue[0] = 0;
	vtValue[1] = 0;
	vtValue[2] = 0;
	vtValue[3] = 0;
	vtValue[4] = 0;
	vtValue[5] = 0;
	
	qType == 0 ;

	#undef CH
}

void TQueryCfg::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);
	
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, clFrmFace);
	
	RectFillRender(BoundCaption.Left, BoundCaption.Top, BoundCaption.Right, BoundCaption.Bottom, clMaroon);
	
	
	if(qType)TextRender_string24(BoundCaption.Left+6, BoundCaption.Top +3, clNearWhite, clMaroon, "查询->网络配置"); 
	else TextRender_string24(BoundCaption.Left+6, BoundCaption.Top +3, clNearWhite, clMaroon, "查询->本机配置");
}


void TQueryCfg::Show(void)
{
	DrawSelf();
	if(qType == 0){
		ShowThisCfg();
	}else
	if(qType == 1){
		ShowNetwork();
	}
}

void TQueryCfg::FullRedraw(int Prm)
{
	if(Prm == 0){
		DrawSelf();
		if(qType == 0){
			ShowThisCfg();
		}else
		if(qType == 1){
			ShowNetwork();
		}
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], clFrmFace);
		if(qType == 0){
			ShowThisCfg();
		}else
		if(qType == 1){
			ShowNetwork();
		}
	}
}

void TQueryCfg::DrawThisCfgOutLine()
{
	int aLen, aW;
	unsigned int aClr,aTClr;
	aLen = 8;
	aW = aLen * 12 +12;
	
	aClr = clGray;
	aTClr = clNearBlack;

	
	VertLineRender(BoxBoundCfg.Left,	BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	VertLineRender(BoxBoundCfg.Left+1,	BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	VertLineRender(BoxBoundCfg.Right,	BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	VertLineRender(BoxBoundCfg.Right-1,BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Top+12, 		6, 	aClr);
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Top+12+1, 	6, 	aClr);
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Bottom, 		BoxBoundCfg.Width, 	aClr);
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Bottom -1, 	BoxBoundCfg.Width, 	aClr);
	
	HorizLineRender(BoxBoundCfg.Left + aW +6, 	BoxBoundCfg.Top+12, 		BoxBoundCfg.Width -6 -aW, 	aClr);
	HorizLineRender(BoxBoundCfg.Left + aW +6, 	BoxBoundCfg.Top+12+1, 	BoxBoundCfg.Width -6 -aW, 	aClr);
	
	TextRender_string24(BoxBoundCfg.Left + 8, BoxBoundCfg.Top, aTClr, clFrmFace, "本机配置");	
}

void TQueryCfg::DrawThisEntry()
{
	
}

void TQueryCfg::ShowThisCfg(void)
{
	int i, x;
	int MtbCount, CirCount, BusPadCount, DirectPadCount;
	RectFillRender(BoxBoundCfg.Left, 	BoxBoundCfg.Top, BoxBoundCfg.Right, BoxBoundCfg.Bottom, clFrmFace);
	
	MtbCount = 0;
	CirCount = 0;
	BusPadCount = 0;
	DirectPadCount = 0;
	for(x=0; x<5; x++){
		for(i=0; i<8; i++){
			if(MainCtl.fData.Split.ExistTablePort[x*8+i]){
				MtbCount++;
				break;
			}
		}
	}
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i]){
			CirCount++;
		}
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		if(HandCtlBoard[i].PadIsProhibit == 0){
			BusPadCount++;
		}
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		if(DirectCtlBoard[i].PadIsProhibit == 0){
			DirectPadCount++;
		}
	}
	DrawThisCfgOutLine();
	
	TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 50, clNearBlack, "注册的回路母板数量:");
	TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 90, clNearBlack, "注册的回路数量:");
	TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 130, clNearBlack,  "配置的总线盘数量:");
	TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 170, clNearBlack,  "配置的多线盘数量:");
	
	TextDigitRender2Right24(BoxBoundCfg.Left + 380, BoxBoundCfg.Top + 50, clNearBlack, MtbCount);
	TextDigitRender2Right24(BoxBoundCfg.Left + 380, BoxBoundCfg.Top + 90, clNearBlack, CirCount);
	TextDigitRender2Right24(BoxBoundCfg.Left + 380, BoxBoundCfg.Top + 130, clNearBlack, BusPadCount);
	TextDigitRender2Right24(BoxBoundCfg.Left + 380, BoxBoundCfg.Top + 170, clNearBlack, DirectPadCount);
	
	DrawThisEntry();
}

void TQueryCfg::DrawNetworkOutLine()
{
	int aLen, aW;
	unsigned int aClr,aTClr;
	aLen = 8;
	aW = aLen * 12 +12;
	
	aClr = clGray;
	aTClr = clNearBlack;

	VertLineRender(BoxBoundCfg.Left,	BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	VertLineRender(BoxBoundCfg.Left+1,	BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	VertLineRender(BoxBoundCfg.Right,	BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	VertLineRender(BoxBoundCfg.Right-1,BoxBoundCfg.Top+12,		BoxBoundCfg.Height-12,	aClr);
	
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Top+12, 		6, 	aClr);
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Top+12+1, 	6, 	aClr);
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Bottom, 		BoxBoundCfg.Width, 	aClr);
	HorizLineRender(BoxBoundCfg.Left, 	BoxBoundCfg.Bottom -1, 	BoxBoundCfg.Width, 	aClr);
	
	HorizLineRender(BoxBoundCfg.Left + aW +6, 	BoxBoundCfg.Top+12, 		BoxBoundCfg.Width -6 -aW, 	aClr);
	HorizLineRender(BoxBoundCfg.Left + aW +6, 	BoxBoundCfg.Top+12+1, 	BoxBoundCfg.Width -6 -aW, 	aClr);
	
	TextRender_string24(BoxBoundCfg.Left + 8, BoxBoundCfg.Top, aTClr, clFrmFace, "组网设定");	
}

void TQueryCfg::ShowNetwork(void)
{
	int x,y, i;
	RectFillRender(BoxBoundCfg.Left, 	BoxBoundCfg.Top, BoxBoundCfg.Right, BoxBoundCfg.Bottom, clFrmFace);
	
	DrawNetworkOutLine();
	
	TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 50, clNearBlack, "本机模式");
	TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 90, clNearBlack, "本机地址");
	
	if(MainCtl.fData.Split.NetworkMode == 1){
		TextRender_string24(BoxBoundCfg.Left + 344, BoxBoundCfg.Top + 50, clNearBlack, "集中机");
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		TextRender_string24(BoxBoundCfg.Left + 344, BoxBoundCfg.Top + 50, clNearBlack, "区域机");
	}else{
		TextRender_string24(BoxBoundCfg.Left + 344, BoxBoundCfg.Top + 50, clNearBlack, "单机");
	}
	
	TextDigitRender2Right24(BoxBoundCfg.Left + 344, BoxBoundCfg.Top + 90, clNearBlack, MainCtl.fData.Split.MyNum);
	
	if(MainCtl.fData.Split.NetworkMode ==1){
		TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 130, clNearBlack, "区域机号列表:");
		for(i=0; i<ProtoC2C.SlaveCount; i++){
			x = BoxBoundCfg.Left + 130 + ((i % 6) * 80);
			y = BoxBoundCfg.Top + 170 + ((i/6) * 40);
			TextDigitRender2Right24(x, y, clNearBlack, ProtoC2C.SlaveList[i]);
		}
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		TextRender_string24(BoxBoundCfg.Left + 136, BoxBoundCfg.Top + 130, clNearBlack, "集中机号:");
		TextDigitRender2Right24(BoxBoundCfg.Left + 344, BoxBoundCfg.Top + 130, clNearBlack, ProtoC2C.MasterNum);
	}
}

void TQueryCfg::LoadThisCfg()
{
	qType = 0;
}

void TQueryCfg::LoadNetworkCfg(void)
{
	qType = 1;
}

TGuiMsgReturn TQueryCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	if(aKey == VK_EXECUTE){
		aMsg = guiMsgReturn;
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	return aMsg;
}








