
#include "gType.h"
#include"GraphLow.h"
#include"TEdit.h"

const static unsigned char CharStart = '*';


void TEdit::Init(u16 aX, u16 aY, u16 aW, u16 aH, u16 OwnerX, u16 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);
	
	FontSize = 24;
	Enable = true;
	FlickDominance = true;
	FlickTick = 0;
	Pst = -1;
	IsShowCursor = 1;
	MaxLen = 60;
	
	CursorMode =0;
	MaskMode =0;
	Selected = 0;
}

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

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

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

void TEdit::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 TEdit::CorrectPst()
{
	int aLen = Str.GetEndPst();
	if(Pst > aLen) Pst = aLen;
}

void TEdit::ShowCursor(void)
{
	unsigned int aPst;
	aPst = (Pst + 1) * 12 +1;
	VertLineRender(Content.Left + aPst,	Bound.Top+2, 	Bound.Height-3,	SelectedTextColor);
}

void TEdit::ShowCursor(unsigned int aClr)
{
	unsigned int aPst;
	aPst = (Pst + 1) * 12 +1;
	VertLineRender(Content.Left + aPst,	Bound.Top+2, 	Bound.Height-3,	aClr);
}

void TEdit::ReDraw(void)
{
	int Len,i;
	if(FontSize == 16){
		
	}else{
		if(Enable){
			if(Selected){
				RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom,  SelectedColor);
				if(MaskMode == CharStart){
					Len = Str.GetLength();
					for(i=0; i<Len; i++){
						TextRender_string24(Content.Left + 2 + (i*12), Content.Top + 2, SelectedTextColor, "*");
					}
				}else{
					TextRender_string24(Content.Left + 2, Content.Top + 2, SelectedTextColor, Str.Text);
				}
				if(IsShowCursor)ShowCursor(SelectedTextColor);
			}else{
				RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom,  Color);
				if(MaskMode == CharStart){
					Len = Str.GetLength();
					for(i=0; i<Len; i++){
						TextRender_string24(Content.Left + 2 + (i*12), Content.Top + 2, TextColor, "*");
					}
				}else{
					TextRender_string24(Content.Left + 2, Content.Top + 1, TextColor, Str.Text);
				}
			}
		}else{
			RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom,  clFrmFace);
			if(MaskMode == CharStart){
				Len = Str.GetLength();
				for(i=0; i<Len; i++){
					TextRender_string24(Content.Left + 2 + (i*12), Content.Top + 2, clGray, "*");
				}
			}else{
				TextRender_string24(Content.Left + 2, Content.Top + 1, clGray, Str.Text);
			}
		}
	}
}

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

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

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

void TEdit::KeyIn(unsigned char aKey)
{
	int aEp, aLen,i,PrvCharType;
	switch(aKey){
		case VK_DELETE:
			aEp = Str.Delete(Pst);
			Pst -= aEp;
			if(Pst < -1)Pst =-1;
			break;
		case VK_LEFT:
			if(Pst > 0){
				PrvCharType = 0;
				const int tPst = Str.GetEndPst();
				if(Pst > tPst)Pst = tPst;
				for(i=0; i<=Pst; i++){
					if(Str.Text[i] & 0x80){
						PrvCharType = 1;
						i++;
					}else{
						PrvCharType = 0;
					}
				}
				if( PrvCharType == 0){
					//this is AscII
					Pst--;
				}else{
					Pst -=2;
				}
			}else
			if(Pst >-1){
				Pst--;
			}
			break;
		case VK_RIGHT:
			aEp = Str.GetEndPst();
			if(Pst < (aEp)){
				if( (Str.Text[Pst+1] & 0x80) == 0){
					//this is AscII
					Pst++;
				}else{
					if((Pst+2)>aEp){
						Str.Text[Pst] = 0;
						Pst --;
					}else{
						Pst +=2;
					}
				}
			}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 == VK_DECIMAL){Str.Insert("*",Pst); Pst++;}
		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 TEdit::OnTime100mS(void)
{
	FlickTick++;
	if(FlickTick>4){
		FlickTick = 0;
		if(FlickDominance){
			FlickDominance = false;
		}else{
			FlickDominance = true;
		}
	}
}

void TEdit::SetMaxLen(unsigned int aLen)
{
	if(aLen > 60){
		MaxLen = 60;
	}else{
		MaxLen = aLen;
	}
}

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

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

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

void TEdit::sDrawSelf(int x, int y, int w, int h, unsigned int aClr)
{
	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(x+w-1-1, y+1, 	h-2,	0xFFD4D0C8);
	HorizLineRender(x+1,	y+h-1-1, 	w-2,	0xFFD4D0C8);

	VertLineRender(x+w-1,	y, 	  h,	0xFFFFFFFF);
	HorizLineRender(x,	y+h-1,  w,	0xFFFFFFFF);
	
	RectFillRender(x+2, y+2, x+w-3, y+h-3,  aClr);
		
}


