#include "RuntimeData.h"
#include "BusPadProgram.h"
#include "DevMask.h"


void TBusPadProgram::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);
	
	RectPanel.Set(Left + 80,Top + CH + 6, Right - 80, Top + CH + 6 + 156);
	Color = aColor;
	#undef CH
	
	Count = 0;
	ItemsIndex =0;
	EditIndex =0;
	OnEditing =0;
	CtlIndex =1;
	IsUsing = 0;
	IsInputSymbol = 0;
	IsReadWriteReq = 0;
	IsProgram = 0;
	
	Caption.Border.Color = clGray;
	
	EditML.Init(80,206,637,126,Bound.Left,Bound.Top,1,clTeal);
	EditML.SelectedColor = clTeal;
	EditML.SetMaxLen(199);
	TMyString::sClear(EditML.Str.Text);
	EditML.SetPstTail();
	EditML.IsShowCursor = 1;
	EditML.Enable = 1;
	EditML.Visible = 1;
	
}

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

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

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

void TBusPadProgram::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 TBusPadProgram::DrawCaption(void)
{	
	if(IsProgram){
		TMyString::sFromStr("设置总线盘联动编程",  Caption.Text);
	}else{
		TMyString::sFromStr("查看总线盘联动编程",  Caption.Text);
	}
	Caption.Show();
}

void TBusPadProgram::Show()
{
	short x,y,i;
	DrawSelf();
	DrawCaption();
	
	x = RectPanel.Left;
	y = Bound.Top + 336;
	cText[0] = 0;
	cText[1] = 0;
	cText[2] = 0;
	cText[3] = 0;
	for(i=0; i<10; i++){
		cText[0] = i + '0';
		cText[1] = '.';
		TextRender_string24(x, y,clNearBlack, cText);
		x += 48;
	}
	x = RectPanel.Left+24;
	cText[0] = 0;
	cText[1] = 0;
	cText[2] = 0;
	cText[3] = 0;
	for(i=0; i<10; i++){
		cText[0] = CharBusPadProgram[i];
		TextRender_string24(x, y,clBlue, cText);
		x += 48;
	}
	TextRender_string24(RectPanel.Left, Bound.Top + 366 ,clNearBlack, "*键输入或切换符号,数字键确认符号; #键输入空格");
}

void TBusPadProgram::TryDecValue()
{
	int aVal;
	if(CtlIndex == 1){
		aVal = aPanel->Edit[0].Str.ToInteger();
		if(aVal >1)aVal--;
		aPanel->Edit[0].Str.FromInt(aVal);
		aPanel->Edit[0].SetPstTail();
		aPanel->Edit[0].Show();
	}else
	if(CtlIndex == 2){
		aVal = aPanel->Edit[1].Str.ToInteger();
		if(aVal >1)aVal--;
		aPanel->Edit[1].Str.FromInt(aVal);
		aPanel->Edit[1].SetPstTail();
		aPanel->Edit[1].Show();		
	}else
	if(CtlIndex == 3){
		if(IsUsing == 0){
			IsUsing = 1;
			aPanel->Edit[2].Str.FromStr("启动");
		}else{
			IsUsing = 0;
			aPanel->Edit[2].Str.FromStr("禁用");
		}
		aPanel->Edit[2].ReDraw();
	}	
}

void TBusPadProgram::TryIncValue()
{
	int aVal;
	if(CtlIndex == 1){
		aVal = aPanel->Edit[0].Str.ToInteger();
		if(aVal < 8)aVal++;
		aPanel->Edit[0].Str.FromInt(aVal);
		aPanel->Edit[0].SetPstTail();
		aPanel->Edit[0].Show();
	}else
	if(CtlIndex == 2){
		aVal = aPanel->Edit[1].Str.ToInteger();
		if(aVal < 128)aVal++;
		aPanel->Edit[1].Str.FromInt(aVal);
		aPanel->Edit[1].SetPstTail();
		aPanel->Edit[1].Show();		
	}else
	if(CtlIndex == 3){
		if(IsUsing == 0){
			IsUsing = 1;
			aPanel->Edit[2].Str.FromStr("启动");
		}else{
			IsUsing = 0;
			aPanel->Edit[2].Str.FromStr("禁用");
		}
		aPanel->Edit[2].ReDraw();
	}
}

void TBusPadProgram::Check4Selected(void)
{
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	aPanel->Edit[2].Selected = 0;
	aPanel->Btn[0].Selected = 0;
	EditML.Selected = 0;
	switch(CtlIndex){
		case 0:
			aPanel->Btn[0].Selected = 1;
			break;
		case 1:
			aPanel->Edit[0].Selected = 1;
			break;
		case 2:
			aPanel->Edit[1].Selected = 1;
			break;
		case 3:
			aPanel->Edit[2].Selected = 1;
			break;
		case 4:
			EditML.Selected = 1;
			break;
		default:

			break;
	}		
}

void TBusPadProgram::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	x = RectPanel.Left;
	y = RectPanel.Top;
	x2 = RectPanel.Right;
	y2 = RectPanel.Bottom;
	aW = RectPanel.Width;
	aH = RectPanel.Height;
	
	yt = y+6;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);
	
	aPanel->SText[0].Init(126,39,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(126,75,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(126,111,70,30,x,yt,clNearBlack,clFrmFace);

	TMyString::sFromStr("盘号", aPanel->SText[0].Text);
	TMyString::sFromStr("按键号", aPanel->SText[1].Text);
	TMyString::sFromStr("启用或禁用", aPanel->SText[2].Text);
	
	for(i=0; i<3;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	
	aPanel->Edit[0].Init(260,39,80,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(260,75,80,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(260,111,80,30,x,yt,clNearBlack,clNearWhite);
	
	for(i=0; i<3;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
	}
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[0].SetInputMaskEn();
	aPanel->Edit[0].Str.FromStr("01");
	aPanel->Edit[0].Pst = 1;
	
	aPanel->Edit[1].SetMaxLen(3);
	aPanel->Edit[1].SetInputMaskEn();
	aPanel->Edit[1].Str.FromStr("001");
	aPanel->Edit[1].Pst = 2;
	
	aPanel->Edit[2].SetMaxLen(4);
	aPanel->Edit[2].SetInputMaskEn();
	aPanel->Edit[2].Str.FromStr("----");
	aPanel->Edit[2].Pst = 3;
	
	aPanel->Btn[0].Init(240,0,120,32,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[0].Caption.FromStr("保存所有");
	for(i=0; i<1;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(10);
		aPanel->Btn[i].SelectedColor = clBlue;
	}
	
	if(IsProgram == 0){
		aPanel->Edit[2].Enable = 0;
		EditML.Enable = 0;
	}

	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	EditML.Show();
}

void TBusPadProgram::LoadExpression()
{
	unsigned int i;
	for(i=0; i<190;i++){
//		ExpressionText[i] = 'A';
 	}
}

void TBusPadProgram::SetEditStart(void)
{
	int x,y,w,h;
	OnEditing = 1;
	EditIndex = CtlIndex;
	LoadExpression();
	EditML.SetMaxLen(199);
//	EditML.Str.FromStr(ExpressionText);
	EditML.SetPstTail();
	EditML.Show();			
}

void TBusPadProgram::RestoreFromEdit(void)
{
//	TMyString::sFromStr(EditML.Str.Text, ExpressionText);
}

TGuiMsgReturn TBusPadProgram::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn aMsgMBox;
	unsigned char LoadKey;
	char LastChr;
	
	if(0){
		aMsgMBox = MessageBox.KeyIn(aKey);
		if(aMsgMBox == guiMsgSel1AndReturn){
			//Read
		}else
		if(aMsgMBox == guiMsgSel2AndReturn){
			//Write
		}else{
			
		}
		IsReadWriteReq = 0;
		aPanel->Show();
		Check4Selected();
		aPanel->ReDrawItems();
		EditML.ReDraw();
		IsInputSymbol = 0;
		return aMsg;
	}
	if(IsProgram){
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
			case VK_DECIMAL:
			case VK_OEM_2:
				LoadKey = aKey;
				if( aKey == VK_DECIMAL){
					// '*'
					if(CtlIndex ==4){
						if(IsInputSymbol){
							LastChr = EditML.GetCurrentChar();
							if(LastChr == CharBusPadProgram[0])EditML.SetCurrentChar(CharBusPadProgram[1]);
							else if(LastChr == CharBusPadProgram[1])EditML.SetCurrentChar(CharBusPadProgram[2]);
							else if(LastChr == CharBusPadProgram[2])EditML.SetCurrentChar(CharBusPadProgram[3]);
							else if(LastChr == CharBusPadProgram[3])EditML.SetCurrentChar(CharBusPadProgram[4]);
							else if(LastChr == CharBusPadProgram[4])EditML.SetCurrentChar(CharBusPadProgram[5]);
							else if(LastChr == CharBusPadProgram[5])EditML.SetCurrentChar(CharBusPadProgram[6]);
							else if(LastChr == CharBusPadProgram[6])EditML.SetCurrentChar(CharBusPadProgram[7]);
							else if(LastChr == CharBusPadProgram[7])EditML.SetCurrentChar(CharBusPadProgram[8]);
							else if(LastChr == CharBusPadProgram[8])EditML.SetCurrentChar(CharBusPadProgram[9]);
							else if(LastChr == CharBusPadProgram[9])EditML.SetCurrentChar(CharBusPadProgram[0]);
							else EditML.KeyIn(CharBusPadProgram[0]);
							EditML.ReDraw();	
						}else{
							EditML.KeyIn(CharBusPadProgram[0]);
							EditML.ReDraw();
							IsInputSymbol = 1;
						}
					}
				}else
				if(aKey == VK_OEM_2){
					//'#'
					if(CtlIndex ==4){
						IsInputSymbol = 0;
						EditML.KeyIn(' ');
						EditML.ReDraw();
					}
				}else{
					if(CtlIndex ==1){
						if(aPanel != nullptr){
							aPanel->Edit[0].KeyIn(aKey);
							aPanel->Edit[0].ReDraw();
						}
					}else
					if(CtlIndex ==2){
						if(aPanel != nullptr){
							aPanel->Edit[1].KeyIn(aKey);
							aPanel->Edit[1].ReDraw();
						}
					}else
					if(CtlIndex == 3){
						if(IsUsing){
							IsUsing = 0;
							if(aPanel != nullptr){
								aPanel->Edit[2].Str.FromStr("禁用");
								aPanel->Edit[2].ReDraw();
							}
						}else{
							IsUsing = 1;
							if(aPanel != nullptr){
								aPanel->Edit[2].Str.FromStr("启用");
								aPanel->Edit[2].ReDraw();
							}
						}
					}
					if(CtlIndex ==4){
						if(IsInputSymbol){
							IsInputSymbol = 0;
							LoadKey = aKey - '0';
							if(LoadKey < 10){
								EditML.SetCurrentChar(CharBusPadProgram[LoadKey]);
								EditML.ReDraw();
							}
						}else{
							EditML.KeyIn(aKey);
							EditML.ReDraw();
						}
					}
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					if(aPanel != nullptr){
						Check4Selected();
						aPanel->ReDrawItems();
					}
					IsInputSymbol = 0;
					EditML.ReDraw();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <4){
					CtlIndex++;
					if(aPanel != nullptr){
						Check4Selected();
						aPanel->ReDrawItems();
					}
					IsInputSymbol = 0;
					EditML.ReDraw();
				}
				break;
			case dfKEY_SETUP:		//0x70		//  Key value for SETUP
				TryDecValue();
				break;
			case dfKEY_QUERY:		//0x71		//  Key value for QUERY
				TryIncValue();
				break;
			case VK_TAB:
				/*IsReadWriteReq = 1;
				MessageBox.Init(0, 36, 800, 406, 0, 0, clMoneyGreen) ;
				MessageBox.SetBtnOk1Ok2Cancel();
				MessageBox.SetReturnValue(guiMsgSel1AndReturn, guiMsgSel2AndReturn, guiMsgReturn, guiMsgNone);
				MessageBox.Show();
				MessageBox.DoReadWriteBusPad();*/
				break;
			case VK_EXECUTE:
				IsInputSymbol = 0;
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
		}
	}else{
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				if(CtlIndex ==1){
					if(aPanel != nullptr){
						aPanel->Edit[0].KeyIn(aKey);
						aPanel->Edit[0].ReDraw();
					}
				}else
				if(CtlIndex ==2){
					if(aPanel != nullptr){
						aPanel->Edit[1].KeyIn(aKey);
						aPanel->Edit[1].ReDraw();
					}
				}
				break;
			case VK_UP:
				if(CtlIndex >1){
					CtlIndex--;
					if(aPanel != nullptr){
						Check4Selected();
						aPanel->ReDrawItems();
					}
					EditML.ReDraw();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <2){
					CtlIndex++;
					if(aPanel != nullptr){
						Check4Selected();
						aPanel->ReDrawItems();
					}
					EditML.ReDraw();
				}
				break;
			case dfKEY_SETUP:		//0x70		//  Key value for SETUP
				TryDecValue();
				break;
			case dfKEY_QUERY:		//0x71		//  Key value for QUERY
				TryIncValue();
				break;
			case VK_TAB:
				break;
			case VK_EXECUTE:
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
		}
	}		

	return aMsg;	
}











