
#include "gType.h"
#include"GraphLow.h"
#include"EditML.h"


void TEditML::Init(int aX, int aY, int aW, int aH, int OwnerX, int OwnerY, u32 aBorderWidth, u32 aBorderColor)
{
	unsigned short Left,Right,Top,Bottom, aBW;
	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;
			
	Content.Set(Left,Top,Right,Bottom);
	Index = 0;
	
	Color = clNearWhite;
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	SetText("Edit xx", 24);
	
	Enable = true;
	FlickDominance = true;
	FlickTick = 0;
	Pst = -1;
	
	MaxLen = 256;
	LineSpacing = 4;
	SetLineSpacing(LineSpacing);
}

void TEditML::SetLineSpacing(int aSpacing)
{
	int aN, aS;
	LineSpacing = aSpacing;
	aN = Content.Width / 12;
	aS = Content.Width % 12;
	if(aS < 3){
		OneLineTextCount = aN -1;
	}else{
		OneLineTextCount = aN;
	}
	LineNum = Content.Height /24;
}

void TEditML::SetText(char *p, int aFontSize)
{
	Str.FromStr(p);
	FontSize = aFontSize;	
}

void TEditML::SetText(const char *p, int aFontSize)
{
	Str.FromStr(p);
	FontSize = aFontSize;	
}

void TEditML::TextClear(void)
{
	Str.Text[0]=0;
	Str.Text[1]=0;
	Pst = -1;
	ReDraw();
}

void TEditML::SelfDraw(void)
{
	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);	
	}
		
	ReDraw();
}

void TEditML::ReDraw(void)
{
	int i,x,y, xCnt, yCnt, Inx, cpPst;
	unsigned int tClr, bClr;
	TmpChar[0] = 0;
	TmpChar[1] = 0;
	TmpChar[2] = 0;
	TmpChar[3] = 0;
	if(Enable){
		if(Selected){
			bClr = SelectedColor;
			tClr = SelectedTextColor;
		}else{
			bClr = Color;
			tClr = TextColor;
		}
	}else{
		bClr = clFrmFace;
		tClr = clGray;
	}
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom,  bClr);
	x = Content.Left + 2;
	y = Content.Top + 1;
	xCnt = 0;
	yCnt = 0;
	Inx = 0;
	if(Pst == -1){
		if(Enable && Selected && IsShowCursor)
			VertLineRender(x ,	y, 	24,	tClr);
	}
	for(i=0; i<dMYLONGSTRING_LENGTH; i++){
		if(Str.Text[Inx] != 0){
			if( (Str.Text[Inx] & 0x80) == 0){
				TmpChar[0] = Str.Text[Inx];
				TmpChar[1] = 0;
				TextRender_string24(x, y, tClr, TmpChar);
				if( Inx == Pst){
					if(Enable && Selected && IsShowCursor)
						VertLineRender(x + 12 +1,	y, 	24,	tClr);
				}
				x += 12;
				xCnt++;
				Inx++;
			}else{
				TmpChar[0] = Str.Text[Inx];
				TmpChar[1] = Str.Text[Inx +1];
				TmpChar[2] = 0;
				TextRender_string24(x, y, tClr, TmpChar);
				if( Inx == Pst){
					if(Enable && Selected && IsShowCursor)
						VertLineRender(x + 24 +1,	y, 	24,	tClr);
				}
				x += 24;
				xCnt +=2;
				Inx +=2;
			}
			if( (Str.Text[Inx] & 0x80) == 0){
				cpPst = OneLineTextCount;
			}else{
				cpPst = OneLineTextCount -1;
			}
			if(xCnt >= cpPst){
				xCnt = 0;
				yCnt++;
				x = Content.Left + 2;
				y = y + LineSpacing + 24;
				if(yCnt >= LineNum)break;
			}
			if(Inx >= (dMYLONGSTRING_LENGTH -1))break;
		}
	}
}

void TEditML::SetSelect(void)
{
	Selected = 1;
	ReDraw();
}

void TEditML::SetDeSelect(void)
{
	Selected = 0;
	FlickDominance = true;
	FlickTick = 0;
	Pst = Str.GetEndPst();
	ReDraw();
}

void TEditML::Show(void)
{
	SelfDraw();
}

void TEditML::KeyIn(unsigned char aKey)
{
	int aEp, aLen;
	switch(aKey){
		case VK_DELETE:
			aEp = Str.Delete(Pst);
			Pst -= aEp;
			if(Pst < -1)Pst =-1;
			break;
		case VK_LEFT:
			if(Pst > 0){
				if( (Str.Text[Pst] & 0x80) == 0){
					//this is AscII
					Pst--;
				}else{
					Pst -=2;
				}
			}else
			if(Pst >-1){
				Pst--;
			}
			break;
		case VK_RIGHT:
			aEp = Str.GetEndPst();
			if(Pst < (aEp-1)){
				if( (Str.Text[Pst+1] & 0x80) == 0){
					//this is AscII
					Pst++;
				}else{
					Pst +=2;
				}
			}else
			if(Pst<aEp){
				Pst++;
			}else{
				Pst = aEp;
			}
			break;
	}
	
	if(InputMethod == EN){
		if(  (aKey >= VK_0) && (aKey <= VK_9)  ){
			if(aKey == VK_0)Str.Insert("0",Pst);
			if(aKey == VK_1)Str.Insert("1",Pst);
			if(aKey == VK_2)Str.Insert("2",Pst);
			if(aKey == VK_3)Str.Insert("3",Pst);
			if(aKey == VK_4)Str.Insert("4",Pst);
			if(aKey == VK_5)Str.Insert("5",Pst);
			if(aKey == VK_6)Str.Insert("6",Pst);
			if(aKey == VK_7)Str.Insert("7",Pst);
			if(aKey == VK_8)Str.Insert("8",Pst);
			if(aKey == VK_9)Str.Insert("9",Pst);
			Pst++;
		}
		if(aKey == ' '){Str.Insert(" ",Pst); Pst++;}
		else if(aKey == '='){Str.Insert("=",Pst); Pst++;}
		else if(aKey == 'Y'){Str.Insert("Y",Pst); Pst++;}
		else if(aKey == '('){Str.Insert("(",Pst); Pst++;}
		else if(aKey == ')'){Str.Insert(")",Pst); Pst++;}
		else if(aKey == '-'){Str.Insert("-",Pst); Pst++;}
		else if(aKey == '+'){Str.Insert("+",Pst); Pst++;}
		else if(aKey == '&'){Str.Insert("&",Pst); Pst++;}
		else if(aKey == 'S'){Str.Insert("S",Pst); Pst++;}
		else if(aKey == '*'){Str.Insert("*",Pst); Pst++;}
		else if(aKey == VK_DECIMAL){Str.Insert("*",Pst); Pst++;}
		else if(aKey == VK_OEM_2){Str.Insert("#",Pst);Pst++;}
	}else
	if(InputMethod == CN){
		
	}else{
		InputMethod = EN;
	}
	Str.Text[MaxLen] = 0;
	if(Pst >= MaxLen)Pst = MaxLen -1;
}

void TEditML::OnTime100mS(void)
{
	FlickTick++;
	if(FlickTick>4){
		FlickTick = 0;
		if(FlickDominance){
			FlickDominance = false;
		}else{
			FlickDominance = true;
		}
	}
}

void TEditML::SetMaxLen(unsigned int aLen)
{
	if(aLen > (dMYLONGSTRING_LENGTH-2)){
		MaxLen = dMYLONGSTRING_LENGTH-2;
	}else{
		MaxLen = aLen;
	}
}

void TEditML::SetInputMaskEn(void)
{
	InputMethod = EN;
}

void TEditML::SetInputCN(void)
{
	InputMethod = CN;
}

void TEditML::SetPstTail(void)
{
	Pst = Str.GetEndPst();
}

char TEditML::GetLastChar()
{
	int i;
	char aChr = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-2); i++){
		if(Str.Text[i] != 0){
			aChr = Str.Text[i];
		}else{
			return aChr;
		}
	}
	return ' ';
}

void TEditML::SetLastChar(char aChr)
{
	int i, aPst;
	aPst = 0;
	for(i=0; i<(dMYLONGSTRING_LENGTH-2); i++){
		if(Str.Text[i] != 0){
			aPst = i;
		}else{
			if(aPst < (dMYLONGSTRING_LENGTH-2)){
				Str.Text[aPst] = aChr;
				Str.Text[aPst +1] = 0;
			}else{
				Str.Text[aPst] = 0;
			}
		}
	}
}

char TEditML::GetCurrentChar()
{
	return Str.Text[Pst];
}
	
void TEditML::SetCurrentChar(char aChr)
{
	Str.Text[Pst] = aChr;
}


