#include "XBox.h"

void TXBox::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);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;

	OutlineColor = clGray;
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	
	LineSpacing = 36;
	CheckPointX = Content.Left +10;
	TextPstX = Content.Left + 40;
	ItemsTopY = Content.Top + 15;
	CheckWidgetSize = 8;
	TextPixelWidth = 240;
	
	Items.FocusIndex =0;
	Items.SelectedIndex =-1;
	
	BoxType = btRadioGroup;
}

void TXBox::ReDrawItems()
{
	int i, x1, x2, y1, y2;
	if(Items.Count > drgMAX_COUNT) Items.Count =drgMAX_COUNT;
	if(CheckWidgetSize > 12)CheckWidgetSize =12;
	x1 = CheckPointX 	+ Content.Left 	+ 12;
	y1 = ItemsTopY 	+ Content.Top 	+ 12;
	x2 = TextPstX 	+ Content.Left;
	y2 = ItemsTopY 	+ Content.Top;
	if(BoxType == btRadioGroup){
		for(i=0; i<Items.Count; i++){
			TCircle::sRender(x1, y1, CheckWidgetSize, clGray, 0);
			if(Items.SelectedIndex == i)
				TCircle::sRender(x1, y1, CheckWidgetSize-1, clBlue, 1);
			else 
				TCircle::sRender(x1, y1, CheckWidgetSize-1, Color, 1);
			
			RectFillRender(x2, y2, x2 + TextPixelWidth, y2 , Color);
			TextRender_string24(x2 +2, y2 +3, TextColor, Str[i].Text);
			
			y1 = y1 + LineSpacing;
			y2 = y2 + LineSpacing;
		}	
	}else
	if(BoxType == btGroupBox){
		for(i=0; i<Items.Count; i++){
			if(Items.SelectedIndex == i){
				TCircle::sRender(x1, y1, CheckWidgetSize+2, clBlue, 0);
				TCircle::sRender(x1, y1, CheckWidgetSize+3, clBlue, 0);
			}else{
				TCircle::sRender(x1, y1, CheckWidgetSize+2, Color, 0);
				TCircle::sRender(x1, y1, CheckWidgetSize+3, Color, 0);
			}
			TCircle::sRender(x1, y1, CheckWidgetSize, clGray, 0);
			if(Items.CheckList[i])
				TCircle::sRender(x1, y1, CheckWidgetSize-1, clMedGreen, 1);
			else 
				TCircle::sRender(x1, y1, CheckWidgetSize-1, Color, 1);
			
			RectFillRender(x2, y2, x2 + TextPixelWidth, y2 , Color);
			TextRender_string24(x2 +2, y2 +3, TextColor, Str[i].Text);
			
			y1 = y1 + LineSpacing;
			y2 = y2 + LineSpacing;
		}		
	}
}

void  TXBox::Show()
{
	int aLen, aW;
	aLen = TMyString::sGetLen(Caption.Text);
	aW = aLen * 12 +12;
	
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
	
	VertLineRender(Bound.Left,		Bound.Top+12,		Bound.Height-12,	OutlineColor);
	VertLineRender(Bound.Left+1,		Bound.Top+12,		Bound.Height-12,	OutlineColor);
	VertLineRender(Bound.Right,		Bound.Top+12,		Bound.Height-12,	OutlineColor);
	VertLineRender(Bound.Right-1,		Bound.Top+12,		Bound.Height-12,	OutlineColor);
	
	HorizLineRender(Bound.Left, 		Bound.Top+12, 		6, 	OutlineColor);
	HorizLineRender(Bound.Left, 		Bound.Top+12+1, 		6, 	OutlineColor);
	HorizLineRender(Bound.Left, 		Bound.Bottom, 		Bound.Width, 	OutlineColor);
	HorizLineRender(Bound.Left, 		Bound.Bottom -1, 		Bound.Width, 	OutlineColor);
	
	HorizLineRender(Bound.Left + aW +6, 		Bound.Top+12, 		Bound.Width -6 -aW, 	OutlineColor);
	HorizLineRender(Bound.Left + aW +6, 		Bound.Top+12+1, 		Bound.Width -6 -aW, 	OutlineColor);
	
	TextRender_string24(Content.Left + 8, Bound.Top, TextColor, Caption.Text);
	
	ReDrawItems();
}

TGuiMsgReturn TXBox::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	
	if(BoxType == btRadioGroup){
		if(aKey == VK_LEFT){
			if(  (Items.SelectedIndex < Items.Count) && (Items.SelectedIndex > 0) ){
				Items.SelectedIndex --;
				ReDrawItems();
			}
		}else
		if(aKey == VK_RIGHT){
			if(  (Items.SelectedIndex < Items.Count) && (Items.SelectedIndex > 0) ){
				Items.SelectedIndex --;
				ReDrawItems();
			}
		}else
		if(aKey == VK_UP){
			if(Items.SelectedIndex >0){
				Items.SelectedIndex--;
				ReDrawItems();
			}
		}else
		if(aKey == VK_DOWN){
			if(Items.SelectedIndex <(Items.Count-1)){
				Items.SelectedIndex++;
				ReDrawItems();
			}
		}else
		if(aKey == VK_F1){
			Items.SelectedIndex++;
			if(Items.SelectedIndex >= Items.Count){
				Items.SelectedIndex = 0;
			}
			ReDrawItems();
		}else
		if(aKey == VK_TAB){
			Items.SelectedIndex++;
			if(Items.SelectedIndex >= Items.Count){
				Items.SelectedIndex = 0;
			}
			ReDrawItems();
		}
		if(aKey == VK_EXECUTE){
			//Save Data Here

		}else
		if(aKey == VK_RETURN){
			aMsg = guiMsgReturn;
		}
		return aMsg;
	}else
	if(BoxType == btGroupBox){
		if(aKey == VK_LEFT){
			if(  Items.SelectedIndex < Items.Count ){
				if(Items.CheckList[Items.SelectedIndex])
					Items.CheckList[Items.SelectedIndex] =0;
				else
					Items.CheckList[Items.SelectedIndex] =1;
				ReDrawItems();
			}
		}else
		if(aKey == VK_RIGHT){
			if(  Items.SelectedIndex < Items.Count ){
				if(Items.CheckList[Items.SelectedIndex])
					Items.CheckList[Items.SelectedIndex] =0;
				else
					Items.CheckList[Items.SelectedIndex] =1;
				ReDrawItems();
			}
		}else
		if(aKey == VK_UP){
			if(Items.SelectedIndex >0){
				Items.SelectedIndex--;
				ReDrawItems();
			}
		}else
		if(aKey == VK_DOWN){
			if(Items.SelectedIndex <(Items.Count-1)){
				Items.SelectedIndex++;
				ReDrawItems();
			}
		}else
		if(aKey == VK_F1){
			if(  (Items.SelectedIndex < Items.Count) && (Items.SelectedIndex > -1) ){
				if(Items.CheckList[Items.SelectedIndex])
					Items.CheckList[Items.SelectedIndex] =0;
				else
					Items.CheckList[Items.SelectedIndex] =1;
				ReDrawItems();
			}
		}else
		if(aKey == VK_TAB){
			Items.SelectedIndex++;
			if(Items.SelectedIndex >= Items.Count){
				Items.SelectedIndex = 0;
			}
			ReDrawItems();
		}
		if(aKey == VK_EXECUTE){
			//Save Data Here

		}else
		if(aKey == VK_RETURN){
			aMsg = guiMsgReturn;
		}
		return aMsg;
	}
	return aMsg;
}

void TXBox::sDrawBoxBorder(int x, int y, int x2, int y2, char *chr, int tLen, int aEn, int aSel, unsigned int bClr)
{
	int  aW, aH, aTW;
	unsigned int aClr, aTClr;
	aTW = tLen * 12 +12;
	
	if(aSel){
		aClr = clBlue;
		aTClr = clBlue;
	}else{
		if(aEn){
			aClr = clGray;
			aTClr = clGray;
		}else{
			aClr = clSilver;
			aTClr = clSilver;
		}		
	}
	
	aW = x2 - x +1;
	aH = y2 - y +1;
	
	VertLineRender(x,		y+12,		aH-12,	aClr);
	VertLineRender(x+1,	y+12,		aH-12,	aClr);
	VertLineRender(x2,	y+12,		aH-12,	aClr);
	VertLineRender(x2-1,	y+12,		aH-12,	aClr);
	
	HorizLineRender(x, 	y+12, 		6, 	aClr);
	HorizLineRender(x, 	y+12+1, 	6, 	aClr);
	HorizLineRender(x, 	y2, 		aW, 	aClr);
	HorizLineRender(x, 	y2 -1, 	aW, 	aClr);
	
	HorizLineRender(x + aTW +6, 	y+12, 	aW -6 -aTW, 	aClr);
	HorizLineRender(x + aTW +6, 	y+12+1, 	aW -6 -aTW, 	aClr);
	
	TextRender_string24(x + 8, y, aTClr, bClr, chr);	
}























