#include"gType.h"
#include"GraphLow.h"
#include"ListBox.h"

#define VCNT 10

void TListBoxForm::Init(int aX, int aY, int aW, int aH, int OwnerX, int OwnerY, u16 aBorderWidth, u32 aBorderColor){
	unsigned short Left,Right,Top,Bottom, aBW,i;
	aBW = aBorderWidth;
	aBW = 2;
	Bound.SetBySize(aX+OwnerX, aY+OwnerY, aW, aH);
	OwnerLeftTop.x 	= OwnerX;
	OwnerLeftTop.y 	= OwnerY;
	Border.Width 	= aBW; 
	Border.Color 	= aBorderColor;
	Left 		= Bound.Left + aBW;
	Right 	= Bound.Right - aBW;
	Top 		= Bound.Top + aBW;
	Bottom 	= Bound.Bottom - aBW;
	VScrollBarBox.Set(Right-20,Top,Right,Bottom);
	Content.Set(Left,Top,Right-20,Bottom);

	SetFontSize(24);																												
	
	Color = clNearWhite;
	TextColor = clNearBlack;
	SelectedColor = clAqua;
	SelectedTextColor = clNearWhite;
	VScrollBar.Color = 0xFFFF7777;
	VScrollBar.RibbonColor = 0xFF007777;
}

void TListBoxForm::SetFontSize(int size)
{
	FontSize = size;
	FontHeight = GetFontHeight(FontSize);
	LineHeight = FontHeight + 4;	
}

void TListBoxForm::DrawSelf(void)
{
	//Draw Border
	if (Border.Width>0){
		VertLineRender(Bound.Left,	Bound.Top, 	Bound.Height-1,	0xFF808080);
		HorizLineRender(Bound.Left,	Bound.Top, 	Bound.Width-1,	0xFF808080);
		
		VertLineRender(Bound.Left+1,	Bound.Top+1, 	Bound.Height-2,	0xFF404040);
		HorizLineRender(Bound.Left+1,	Bound.Top+1, 	Bound.Width-2,	0xFF404040);
	
		VertLineRender(Bound.Right-1,	Bound.Top+1, 	Bound.Height-2,	0xFFD4D0C8);
		HorizLineRender(Bound.Left+1,	Bound.Bottom-1, 	Bound.Width-2,	0xFFD4D0C8);

		VertLineRender(Bound.Right,	Bound.Top, 	  Bound.Height,	0xFFFFFFFF);
		HorizLineRender(Bound.Left,	Bound.Bottom, Bound.Width,	0xFFFFFFFF);	
	}
	//Fill Rect Box
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);	
}

void TListBoxForm::DrawVertScrollBar(void)
{
	int RibbonHeight;
	int RibbonBottom;
	int RibbonTop;
	int UpRemain;
	int DownRemain;
	float f1,f2,f3,f4;

	if(Items.Count <= VCNT){
		RibbonTop = VScrollBarBox.Top+1;
		RibbonBottom = VScrollBarBox.Bottom - 1;
		RibbonHeight = VScrollBarBox.Height - 2;
	}else{
		//get RibbonHeight
		f1 = static_cast<float>(VCNT); 
		f2 = static_cast<float>(Items.Count);
		f3 = static_cast<float>(VScrollBarBox.Height);
		f4 = f1/f2*f3;
		RibbonHeight = static_cast<int>(f4) ;
		if(RibbonHeight > VScrollBarBox.Height) RibbonHeight = VScrollBarBox.Height;
		if(RibbonHeight < 10) RibbonHeight = 10;
		f2 = f4/f1;	//Get One Item Height

		//get RibbonCenter
		UpRemain = Items.TopIndex;
		DownRemain = Items.Count -  Items.TopIndex - VCNT;
		if(UpRemain < DownRemain){
			f1 = UpRemain;
			RibbonTop = static_cast<int>(f1*f2) + VScrollBarBox.Top + 1;
			RibbonBottom = RibbonTop + RibbonHeight;
			if( RibbonBottom > (VScrollBarBox.Bottom -1) )RibbonBottom = VScrollBarBox.Bottom - 1;
		}else{
			f1 = DownRemain;
			RibbonBottom = VScrollBarBox.Bottom - static_cast<int>(f1*f2);
			RibbonTop = RibbonBottom - RibbonHeight;
			if( RibbonTop < (VScrollBarBox.Top +1) )RibbonTop = VScrollBarBox.Top +1;
		}
	}
	
	//Draw Vert ScrollBar BackGround
	RectFillRender(VScrollBarBox.Left, VScrollBarBox.Top, VScrollBarBox.Right, VScrollBarBox.Bottom, VScrollBar.Color);
	//Draw Vert ScrollBar Ribbon
	RectFillRender(VScrollBarBox.Left, RibbonTop, VScrollBarBox.Right, RibbonBottom, VScrollBar.RibbonColor);	
}

void TListBoxForm::DrawList(void)
{
	int i,index;
	for(i=0; i<VCNT; i++){
		index = Items.TopIndex + i;
		if(index < Items.Count){
			if(index == Items.SelectedIndex){
				//SelectedColor  SeletedTextColor
				RectFillRender(Content.Left, (i * FontHeight + Content.Top), Content.Right, ((i+1) * FontHeight + Content.Top), SelectedColor);
			
			}else{
				//Color   TextColor
				RectFillRender(Content.Left, (i * FontHeight + Content.Top), Content.Right, ((i+1) * FontHeight + Content.Top), Color);
				
			}
		}
	}
}

void TListBoxForm::Show(void)
{
	DrawSelf();
	DrawVertScrollBar();
	DrawList();
}

void TListBoxForm::KeyIn(int step)
{
	int ind;
	//Up or Down One Index___________________
	if(VK_UP == step){
		if(Items.SelectedIndex>0){
			Items.SelectedIndex--;
			if(Items.SelectedIndex<Items.TopIndex){
				Items.TopIndex=Items.SelectedIndex;
			}
		}
	}else
	if(VK_DOWN == step){
		if(Items.SelectedIndex<Items.Count){
			Items.SelectedIndex++;
			if( (Items.SelectedIndex - Items.TopIndex) > VCNT){
				Items.TopIndex = Items.SelectedIndex - VCNT +1; 
			}
		}
	}else
	
	//Page up or Down____________________________
	if(VK_PRIOR == step){
		//roll up one page 
		Items.TopIndex = Items.TopIndex - VCNT;
		if(Items.TopIndex < 0){
			Items.TopIndex = 0;
			Items.SelectedIndex = 0;
		}else{
			Items.SelectedIndex = Items.SelectedIndex - VCNT;
			if(Items.SelectedIndex < 0)Items.SelectedIndex = 0;
		}
	}else
	if(VK_NEXT == step){
		//roll Down one page
		ind = Items.TopIndex + VCNT;
		if (ind < Items.Count){
			Items.TopIndex = ind;
			ind = Items.SelectedIndex + VCNT;
			if(ind >= Items.Count)
				Items.SelectedIndex = Items.Count - 1;
		}
	}else
	
	//roll to Top or Bottom
	if(VK_HOME == step){
		//roll up to 0 Index
		Items.TopIndex = 0;
		Items.SelectedIndex = 0;
	}else
	if(VK_END == step){
		//roll down to Tail
		Items.TopIndex = Items.Count - VCNT + 1;
		if(Items.TopIndex < 0)
			Items.TopIndex = 0;
		Items.SelectedIndex = Items.TopIndex;
	}
}

int TListBoxForm::GetTopIndex(void)
{
	return Items.TopIndex;
}

int TListBoxForm::GetSelectedItemIndex(void)
{
	return Items.SelectedIndex;
}

void TListBoxForm::SetTextMount(unsigned char *pTextMount, u32 Ind)
{
	if(Ind<ListBoxMaxLineCount){
		TextMount[Ind]=pTextMount;
	}
}




