#include "CnInput.h"

#define dfClr_PinyinSelBackGround	clTeal

const char ListPayload[10] = {0,0,3,3,3,3,3,4,3,4};
const char ListCaseUp[10][4] = {
{' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' '},
{'A', 'B', 'C', ' '},
{'D', 'E', 'F', ' '},
{'G', 'H', 'I', ' '},
{'J', 'K', 'L', ' '},
{'M', 'N', 'O', ' '},
{'P', 'Q', 'R', 'S'},
{'T', 'U', 'V', ' '},
{'W', 'X', 'Y', 'Z'}
};
const char ListCaseLow[10][4] = {
{' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' '},
{'A', 'B', 'C', ' '},
{'D', 'E', 'F', ' '},
{'G', 'H', 'I', ' '},
{'J', 'K', 'L', ' '},
{'M', 'N', 'O', ' '},
{'P', 'Q', 'R', 'S'},
{'T', 'U', 'V', ' '},
{'W', 'X', 'Y', 'Z'}
};

const char ccEnList[10][12] = {
{'0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  1},
{'1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  2},
{'2', 'A', 'B', 'C', 'a', 'b', 'c', ' ', ' ', ' ', ' ',  7},
{'3', 'D', 'E', 'F', 'd', 'e', 'f', ' ', ' ', ' ', ' ',  7},
{'4', 'G', 'H', 'I', 'g', 'h', 'i', ' ', ' ', ' ', ' ',  7},
{'5', 'J', 'K', 'L', 'j', 'k', 'l', ' ', ' ', ' ', ' ',  7},
{'6', 'M', 'N', 'O', 'm', 'n', 'o', ' ', ' ', ' ', ' ',  7},
{'7', 'P', 'Q', 'R', 'S', 'p', 'q', 'r', 's', ' ', ' ',  9},
{'8', 'T', 'U', 'V', 't', 'u', 'v', ' ', ' ', ' ', ' ',  7},
{'9', 'W', 'X', 'Y', 'Z', 'w', 'x', 'y', 'z', ' ', ' ',  9}
};

const char ccSymbolList[4][12] = {
{'`', '~',  '!', '@', '#', '$',  '%', '^', ' ', ' ', ' ', 8},
{'&', '*',  '(', ')', '-', '+',  '_', '=', ' ', ' ', ' ', 8},
{'{', '}',  '[', ']', '|', '\\', ':', ';', ' ', ' ', ' ', 8},
{'"', '\'', '<', '>', ',', '.',  '?', '/', ' ', ' ', ' ', 8},
};

void TCnInput::Init(int aX, int aY, int aW, int aH, int OwnerX, int OwnerY, u32 aColor){
	unsigned short Left,Right,Top,Bottom;
	Bound.SetBySize(aX+OwnerX, aY+OwnerY, aW, aH);
	Color = aColor;
	if(1){	
		Left 		= Bound.Left + 1;
		Right 	= Bound.Right - 2;
		Top 		= Bound.Top + 1;
		Bottom 	= Bound.Bottom - 2;
	}else{
		Left 		= Bound.Left + 2;
		Right 	= Bound.Right - 2;
		Top 		= Bound.Top + 2;
		Bottom 	= Bound.Bottom - 2;
	}
	Content.Set(Left,Top,Right,Bottom);
	
	iEdit.Init(0,0,2,28,Content.Left, Content.Top, Color, clDeepGray);
	iEdit.Visible = 0;
	iEdit.Enable = 1;
	iEdit.SetMaxLen(8);
	iEdit.Str.Clear();
	iEdit.IsShowCursor =0;
	
	sEdit.Init(0,0,90,28,Content.Left, Content.Top, Color, clDeepGray);
	sEdit.Visible = 1;
	sEdit.Enable = 1;
	sEdit.SetMaxLen(8);
	sEdit.Str.Clear();
	sEdit.IsShowCursor =1;
	sEdit.Selected = 1;
	
	PySelIndex = 0;
	CnSelIndex = 0;
	
	PyCount =0;
	CnCount =0;
	
	IsShowing = 0;
	SelectedCn = 0;
	InputMode = CnInputModeEn;
}

void TCnInput::DrawSelf(void)
{
	
	/*unsigned int r,g,b, h,l;
	h = Color & 0x00FFFFFF;
	
	r = h / 0x10000;
	g = (h / 256) % 256;
	b = h % 256;
	
	h = r * 3 / 2;	if(h > 255)h=255;		r = h;
	h = g * 3 / 2;	if(h > 255)h=255;		g = h;
	h = b * 3 / 2;	if(h > 255)h=255;		b = h;
	h = (r<<16) + (g<<8) + b;
	l = r * 2 / 3;	r = l;
	l = g * 2 / 3;	g = l;
	l = b * 2 / 3;	b = l;
	l = (r<<16) + (g<<8) + b;
	
	
	//Draw Border
	g = Bound.Right;
	b = Bound.Bottom;

	//Draw Right & Bottom 
	for(r=0; r<BorderWidth; r++){
		//Draw Horiz
		HorizLineRender(Bound.Left+r, Bound.Bottom-r, Bound.Width-r-r, l);
		//Draw Vert
		VertLineRender(Bound.Right-r, Bound.Top+r, Bound.Height-r-r, l);
	}
	//Draw Left & Top
	for(r=0; r<BorderWidth; r++){
		//Draw Horiz
		HorizLineRender(Bound.Left+r, Bound.Top+r, Bound.Width-r-r, h);
		//Draw Vert
		VertLineRender(Bound.Left+r, Bound.Top+r, Bound.Height-r-r, h);
	}*/
	
	if (1){
		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);	
	}else
	if(0){
		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);
	}
	
	//Fill Rect Box
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

void TCnInput::Show(void)
{
	DrawSelf();
	sEdit.Show();
	IsShowing = 1;
	
	VertLineRender(Content.Left + 91,	Content.Top, 	28,	clSilver);
	VertLineRender(Content.Left + 121,	Content.Top, 	28,	clSilver);
	
	HorizLineRender(Content.Left, 	Content.Top + 28, 	Content.Width - 1, clSilver);
	HorizLineRender(Content.Left, 	Content.Top + 56, 	Content.Width - 1, clSilver);
	
	DrawInputType();
	EnShowTip(0);
	
	SetInputMode(CnInputMode);
}

void TCnInput::OverPrint(unsigned int aClr)
{
	RectFillRender(Bound.Left, Bound.Top, Bound.Right, Bound.Bottom, aClr);
}

void TCnInput::FullRedraw()
{
	DrawSelf();
	sEdit.Show();
	IsShowing = 1;
	VertLineRender(Content.Left + 91,	Content.Top, 	28,	clSilver);
	VertLineRender(Content.Left + 121,	Content.Top, 	28,	clSilver);
	HorizLineRender(Content.Left, 	Content.Top + 28, 	Content.Width - 1, clSilver);
	HorizLineRender(Content.Left, 	Content.Top + 56, 	Content.Width - 1, clSilver);
	
	if(InputMode == CnInputModeSymbol){
		DrawInputType();
		DrawSymbolList();
	
		sEdit.Enable = 0;
		sEdit.Str.Clear();
		sEdit.Pst = -1;
		sEdit.IsShowCursor = 0;
		sEdit.Show();
		SymbolShowTip(0);
	}
	if(InputMode == CnInputModeCn){
		DrawInputType();
		ClearCnResult();
	
		SelectedCn = 0;
		PySelIndex = 0;
		CnSelIndex = 0;
	
		ShowPinyinText();
		DrawPinyinList();
		DrawCnList();
	
		CnShowTip(0);
	}
	if(InputMode == CnInputModeEn){
		DrawInputType();
		DrawEnList();
			
		sEdit.Str.Clear();
		sEdit.Enable = 1;
		sEdit.Str.Clear();
		sEdit.Pst = -1;
		sEdit.IsShowCursor = 0;
		sEdit.Show();
		EnShowTip(0);
	}
	if(InputMode == CnInputModeNum){
		DrawInputType();
		DrawEnList();
	
		sEdit.Str.Clear();
		sEdit.Enable = 0;
		sEdit.Str.Clear();
		sEdit.Pst = -1;
		sEdit.IsShowCursor = 0;
		sEdit.Show();
		NumShowTip(0);
	}
}

void TCnInput::Hide(void)
{
	IsShowing = 0;
}

void TCnInput::DrawInputType()
{
	int c, aLen;
	int i, x, y;
	const char *p;
	
	x = Content.Left + 91;
	y =  Content.Top + 1;
	
	x+=4;
	RectFillRender(x, y, x+24, y+24, Color);
	switch(InputMode){
		case CnInputModeEn:
			gui_write_EN2412(x,y,clNearBlack, 'E');
			x+=12;
			gui_write_EN2412(x,y,clNearBlack, 'n');
			break;
		case CnInputModeSymbol:
			gui_write_EN2412(x,y,clNearBlack, '*');
			x+=12;
			gui_write_EN2412(x,y,clNearBlack, ';');
			break;
		case CnInputModeCn:
			if(LanguageEnCn==0)TextRender_string24(x,y,clNearBlack, "中");
			else TextRender_string24(x,y,clNearBlack, "CN");
			break;
		case CnInputModeNum:
			TextRender_string24(x,y,clNearBlack, "09");
			break;
	}
}

void TCnInput::DrawEnList()
{
	int c, aLen;
	int i, x, y;
	const char *p;
	
	x = Content.Left + 10;
	y =  Content.Top + 29;
	
	RectFillRender(x, y, Content.Right-2, y + 24, Color);
	
	x = Content.Left + 120;
	y =  Content.Top + 31;
	if(sEdit.Str.Text[0] != '\0'){
		c = sEdit.Str.Text[0] - '0';
		c %= 10;
		aLen = ccEnList[c][11];
		if(c == 1){
			gui_write_EN2412(x,y,clBlue, '0' + 0);
			x+=12;
			x+=4;
			gui_write_EN2412(x,y,clNearBlack, ccEnList[1][0]);
			x+=40;
			
			TextRender_string24(x,y,clBlue, "1~9");
			x+=36;
			if(LanguageEnCn==0)TextRender_string24(x,y,clNearBlack, "数字键插入空格");
			else TextRender_string24(x,y,clNearBlack, "Num Key Input Space");
		}else{
			for(i=0; i<aLen; i++){
				gui_write_EN2412(x,y,clBlue, '0' + i);
				x+=12;
				x+=4;
				gui_write_EN2412(x,y,clNearBlack, ccEnList[c][i]);
				x+=40;
			}
		}
	}
}

void TCnInput::DrawSymbolList()
{
	int aInx, aLen;
	int i, x, y;
	const char *p;
	
	x = Content.Left + 10;
	y =  Content.Top + 29;
	RectFillRender(x, y, Content.Right-2, y + 24, Color);
	y =  Content.Top + 57;
	RectFillRender(x, y, Content.Right-2, y + 24, Color);
	
	
	y = Content.Top + 31;
	if(SymbolSelIndex >3)SymbolSelIndex = 0;
	aInx = SymbolSelIndex;
	aLen = ccSymbolList[SymbolSelIndex][11];
	for(i=0; i<aLen; i++){
		gui_write_EN2412(x,y,clBlue, '0' + i);
		x+=12;
		x+=4;
		gui_write_EN2412(x,y,clNearBlack, ccSymbolList[SymbolSelIndex][i]);
		x+=40;
	}
	aInx++;
	if(aInx > 3)aInx = 0;
	aLen = ccSymbolList[aInx][11];
	
	x = Content.Left + 10;
	y =  Content.Top + 59;
	for(i=0; i<aLen; i++){
		gui_write_EN2412(x,y,clNearBlack, '0' + i);
		x+=12;
		x+=4;
		gui_write_EN2412(x,y,clNearBlack, ccSymbolList[aInx][i]);
		x+=40;
	}
}

void TCnInput::DrawPinyinList()
{
	int i,g,v,x,y, z;
	unsigned int aClr, bClr;
	const char *p;
	
	x = Content.Left + 10;
	y =  Content.Top + 29;
	
	RectFillRender(x, y, Content.Right-2, Content.Bottom - 2, Color);
	
	z = 0;
	v = 0;
	
	x = Content.Left + 100;
	for(i=0; i<PinyinT9.CnResult.Count; i++){
		if(z > 9)break;
		if(x> (Content.Right-100))break;
		if(z == PySelIndex){
			aClr = clNearWhite;
			bClr = dfClr_PinyinSelBackGround;
		}else{
			aClr = clNearBlack;
			bClr = Color;
		}
		//gui_write_EN2412(x,y,aClr, bClr, '0' + (z+1) % 10);
		//x+=12;
		//RectFillRender(x, y, x+4, y+23, bClr);
		//x+=4;
		p = PinyinT9.GetPinyin(i);
		v =0;
		while(*p != '\0'){
			gui_write_EN2412(x,y,aClr, bClr, *p);
			p++;
			v++;
			x+=12;
			if(v>=7)break;
		}
		x += 20;
		z++;
	}
}

void TCnInput::DrawCnList()
{
	int i,g,v,x,y, aInx;
	unsigned int aClr, bClr, iClr;
	const char *p;
	
	x = Content.Left + 6;
	y =  Content.Top + 57;
	
	if(SelectedCn == 1){
		iClr = clBlue;
		aClr = clNearWhite;
		bClr = dfClr_PinyinSelBackGround;
	}else{
		iClr = clNearBlack;
		aClr = clNearBlack;
		bClr = Color;
	}
	RectFillRender(x, y, Content.Right-2, Content.Bottom - 2, bClr);
	
	if(PinyinT9.CnResult.CnCount > 0){
		y =  Content.Top + 57 + 4;
		if(CnSelIndex > 0){
			y =  Content.Top + 57 + 4;
			for(v=0; v<2; v++){
				aInx = 1;
				x = 620;
				for(i=0; i<8; i++){
					HorizLineRender(x, y, aInx, clBlue);
					x--;
					aInx += 2; 
					y++;
				}
				y += 3;
			}
		}

		y =  Content.Top + 57 + 6;
		if( CnSelIndex < (PinyinT9.CnResult.CnPageCount -1) ){
			for(v=0; v<2; v++){
				aInx = 16;
				x = 640;
				for(i=0; i<8; i++){
					HorizLineRender(x, y, aInx, clBlue);
					x++;
					aInx -= 2; 
					y++;
				}
				y += 3;
			}
		}
	}

	x = Content.Left + 38;
	y =  Content.Top + 57;
	for(i=0; i<10; i++){
		if(x> (Content.Right-50))break;
		aInx = CnSelIndex * 10 + i ;
		if( aInx  >= PinyinT9.CnResult.CnCount)break;
		gui_write_EN2412(x,y,iClr, bClr, '0' + i);
		x+=12;
		RectFillRender(x, y, x+4, y+23, bClr);
		x+=4;
		p = &PinyinT9.CnResult.pCn[aInx * 2];
		TextRender_1CN24(x,y,aClr,p);
		x+=40;
	}	
}

const char * TCnInput::TryGetCn(int cInx)
{
	int PyIndex, CnIndex;
	PyIndex = PinyinT9.CnResult.Index[PySelIndex];
	CnIndex = CnSelIndex * 10 + cInx;
	if(CnIndex < PinyinT9.CnResult.CnCount){
		TheGotCn = PinyinT9.TryGetCn(PinyinT9.CnResult.Index[PySelIndex], CnIndex);
		if(TheGotCn != nullptr){
			iEdit.Str.Clear();
			iEdit.Pst = -1;
			iEdit.Show();
			TMyString::sFromStr(iEdit.Str.Text, PinyinT9.InputStr.Num);
			PinyinT9.GetMatchedPinyinCode();
			SelectedCn = 0;
			PySelIndex = 0;
			DrawPinyinList();
			CnSelIndex = 0;
			PinyinT9.IdentifyCn(PySelIndex);
			DrawCnList();
			GotChar[0] = *TheGotCn;
			GotChar[1] = TheGotCn[1];
			GotChar[2] = '\0';
			return TheGotCn;
		}
	}
	return nullptr;
}

void TCnInput::ClearCnResult()
{
	PinyinT9.CnResult.Count = 0;
	PinyinT9.CnResult.CnCount =0;
	PinyinT9.CnResult.CnPageCount = 0;
	CnSelIndex = 0;
	iEdit.Str.Clear();
	iEdit.Pst = -1;
	sEdit.Str.Clear();
	sEdit.Pst = -1;
	sEdit.Show();
	sEdit.ShowCursor();
}

void TCnInput::ShowPinyinText()
{
	int i,aLen;
	const char *p;
	aLen = iEdit.Str.GetEndPst() +1;
	if(aLen >6)aLen = 6;
	if(PinyinT9.CnResult.Count >0){
		if(PySelIndex < PinyinT9.CnResult.Count){
			p = PinyinT9.GetPinyin(PySelIndex);
			for(i=0; i<aLen; i++){
				sEdit.Str.Text[i] =  *p;
				p++;
			}
			sEdit.Str.Text[i] = '\0';
			sEdit.Pst = i-1;
			sEdit.Show();
			sEdit.ShowCursor();
		}
	}else{
		sEdit.Str.Clear();
		sEdit.Pst = -1;
		sEdit.Show();
		sEdit.ShowCursor();
	}
}

void TCnInput::EnShowTip(int tT)
{
	int aInx, aLen;
	int i, x, y;
	const char *p;
	
	x = Content.Left + 160;
	y =  Content.Top + 3;
	
	RectFillRender(x, y, Content.Right-2, y + 24, Color);
	if(LanguageEnCn==0){
		if(tT == 0){
			TextRender_string24(x,y,clNearBlack, "请输入字母");
		}else
		if(tT == 1){
			TextRender_string24(x,y,clNearBlack, "按相应的数字键选取字母或数字");
		}
	}else{
		if(tT == 0){
			TextRender_string24(x,y,clNearBlack, "Input English Letters");
		}else
		if(tT == 1){
			TextRender_string24(x,y,clNearBlack, "Pick A Letter Or Num");
		}
	}
}

void TCnInput::SymbolShowTip(int tT)
{
	int aInx, aLen;
	int i, x, y;
	const char *p;
	
	x = Content.Left + 160;
	y =  Content.Top + 3;
	
	RectFillRender(x, y, Content.Right-2, y + 24, Color);
	if(LanguageEnCn==0)TextRender_string24(x,y,clNearBlack, 	"按符号前相应数字输入该符号,按左右键翻页");
	else TextRender_string24(x,y,clNearBlack, 			"0..9 To Pick A Symbol, Left-Right Turn The Page");
}

void TCnInput::CnShowTip(int tT)
{
	int aInx, aLen;
	int i, x, y;
	const char *p;
	
	x = Content.Left + 160;
	y =  Content.Top + 3;
	
	RectFillRender(x, y, Content.Right-2, y + 24, Color);
	if(LanguageEnCn==0){
		if(tT == 0){
			TextRender_string24(x,y,clNearBlack, "请输入拼音");
		}else
		if(tT == 1){
			TextRender_string24(x,y,clNearBlack, "* # 及 确认键 进入中文选定");
		}else
		if(tT == 2){
			TextRender_string24(x,y,clNearBlack, "数字键 0~9 拾取中文字, * # 及 确认键 退出中文选定");
		}
	}else{
		if(tT == 0){
			TextRender_string24(x,y,clNearBlack, "Input Pinyin");
		}else
		if(tT == 1){
			TextRender_string24(x,y,clNearBlack, "* # OK Button Enter To Pick Chinese Characters");
		}else
		if(tT == 2){
			TextRender_string24(x,y,clNearBlack, "Num 0~9 To Pick, * # OK Return");
		}
	}
}

void TCnInput::NumShowTip(int tT)
{
	int aInx, aLen;
	int i, x, y;
	const char *p;
	
	x = Content.Left + 160;
	y =  Content.Top + 3;
	
	RectFillRender(x, y, Content.Right-2, y + 24, Color);
	if(LanguageEnCn==0)TextRender_string24(x,y,clNearBlack, "请输入0~9数字");
	else TextRender_string24(x,y,clNearBlack, "Input Num 0~9");
}

const char * TCnInput::EnKeyIn(unsigned char aKey)
{
	int EnGroupX, i = 0;
	switch(aKey){
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case VK_DELETE:
			if(sEdit.Str.Text[0] != '\0'){
				if(aKey == VK_DELETE){
					sEdit.Str.Clear();
					sEdit.Pst = -1;
					sEdit.Show();
					sEdit.ShowCursor();
					DrawEnList();
				}else{
					i = aKey - '0';
					EnGroupX = sEdit.Str.Text[0] - '0';
					sEdit.Str.Clear();
					sEdit.Pst = -1;
					sEdit.Show();
					sEdit.ShowCursor();
					DrawEnList();
					if(EnGroupX == 1){
						GotChar[0] = ccEnList[1][i];
						GotChar[1] = '\0';
						EnShowTip(0);
						return & ccEnList[1][i];
					}else
					if(EnGroupX < 10){
						GotChar[0] = ccEnList[EnGroupX][i];
						GotChar[1] = '\0';
						EnShowTip(0);
						return & ccEnList[EnGroupX][i];
					}
				}
			}else{
				if(aKey != VK_DELETE){
					sEdit.KeyIn(aKey);
					sEdit.Show();
					sEdit.ShowCursor();
					DrawEnList();
				}
			}
			
			if(sEdit.Str.Text[0] == '\0'){
				EnShowTip(0);
			}else{
				EnShowTip(1);
			}
			
			break;
		case VK_LEFT:

			break;
		case VK_RIGHT:

			break;
		case VK_UP:

			break;
		case VK_DOWN:

			break;
		case VK_TAB:
			break;
		case VK_EXECUTE:

			break;
		case VK_RETURN:
			break;
	}
	return nullptr;	
}

const char * TCnInput::SymbolKeyIn(unsigned char aKey)
{
	int EnGroupX, i = 0;
	switch(aKey){
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			i = aKey - '0';
			if(SymbolSelIndex < 4){
				if(i < ccSymbolList[SymbolSelIndex][11]){
					GotChar[0] = ccSymbolList[SymbolSelIndex][i];
					GotChar[1] = '\0';
					return &ccSymbolList[SymbolSelIndex][i];
				}
			}
			break;
		case VK_LEFT:
			if(SymbolSelIndex > 0){
				SymbolSelIndex--;
				DrawSymbolList();
			}
			break;
		case VK_RIGHT:
			if(SymbolSelIndex < 3){
				SymbolSelIndex++;
				DrawSymbolList();
			}
			break;
		case VK_UP:
			if(SymbolSelIndex > 0){
				SymbolSelIndex--;
				DrawSymbolList();
			}
			break;
		case VK_DOWN:
			if(SymbolSelIndex < 3){
				SymbolSelIndex++;
				DrawSymbolList();
			}
			break;
		case VK_TAB:
			break;
		case VK_EXECUTE:
			break;
		case VK_RETURN:
			break;
	}
	return nullptr;	
}

const char * TCnInput::NumKeyIn(unsigned char aKey)
{
	switch(aKey){
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			GotChar[0] = aKey;
			GotChar[1] = 0;
			GotChar[2] = 0;
			return GotChar;
			break;
	}
	return nullptr;	
}

const char * TCnInput::CnKeyIn(unsigned char aKey)
{
	int i;
	switch(aKey){
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case VK_DELETE:
			if( (SelectedCn == 0) || (aKey == VK_DELETE) ){
				iEdit.KeyIn(aKey);
				TMyString::sFromStr(iEdit.Str.Text, PinyinT9.InputStr.Num);
				i = PinyinT9.GetMatchedPinyinCode();
				if(0 == i){
					iEdit.KeyIn(VK_DELETE);
					TMyString::sFromStr(iEdit.Str.Text, PinyinT9.InputStr.Num);
					i = PinyinT9.GetMatchedPinyinCode();
				}
				if(i)CnShowTip(1);
				else CnShowTip(0);
				ShowPinyinText();
				PySelIndex = 0;
				DrawPinyinList();
				CnSelIndex = 0;
				PinyinT9.IdentifyCn(PySelIndex);
				SelectedCn = 0;
				DrawCnList();
			}
			if(SelectedCn == 1){
				if(aKey != VK_DELETE){
					TheGotCn = PinyinT9.TryGetCn(PinyinT9.CnResult.Index[PySelIndex], CnSelIndex * 10 + (aKey - '0') );
					TheGotCn = TryGetCn(aKey - '0');
					iEdit.Str.Clear();
					iEdit.Pst = -1;
					sEdit.Str.Clear();
					sEdit.Pst = -1;
					sEdit.Show();
					SelectedCn = 0;
					ClearCnResult();
					CnShowTip(0);
					return TheGotCn;
				}
			}
			break;
		case VK_LEFT:
			if(PinyinT9.CnResult.Count){
				if(PySelIndex > 0){
					PySelIndex--;
					CnSelIndex = 0;
					ShowPinyinText();
					DrawPinyinList();
					CnSelIndex = 0;
					PinyinT9.IdentifyCn(PySelIndex);
					DrawCnList();
				}
			}
			break;
		case VK_RIGHT:
			if(PinyinT9.CnResult.Count){
				if(PySelIndex < (PinyinT9.CnResult.Count-1)){
					PySelIndex++;
					ShowPinyinText();
					DrawPinyinList();
					CnSelIndex = 0;
					PinyinT9.IdentifyCn(PySelIndex);
					DrawCnList();
				}
			}
			break;
		case VK_UP:
			if(CnSelIndex >0){
				CnSelIndex--;
				DrawCnList();
			}
			break;
		case VK_DOWN:
			if(CnSelIndex < (PinyinT9.CnResult.CnPageCount-1) ){ 
				CnSelIndex++;
				DrawCnList();
			}
			break;
		case VK_TAB:
			SelectedCn = 0;
			DrawCnList();
			break;
		case dfKEY_ASTERISK:		
		case dfKEY_SIG:
		case VK_EXECUTE:
			if(SelectedCn == 0){
				SelectedCn = 1;
				CnShowTip(2);
				DrawCnList();
			}else
			if(SelectedCn == 1){
				SelectedCn = 0;
				CnShowTip(1);
				DrawCnList();
			}
			break;
		case VK_RETURN:
			break;
	}
	return nullptr;	
}

const char * TCnInput::KeyIn(unsigned char aKey)
{
	if(aKey == dfKEY_ENCN){
		switch(InputMode){
			case CnInputModeEn: 
				InputMode = CnInputModeSymbol;
				CnInputMode = CnInputModeSymbol;
				DrawInputType();
				DrawSymbolList();
			
				sEdit.Enable = 0;
				sEdit.Str.Clear();
				sEdit.Pst = -1;
				sEdit.IsShowCursor = 0;
				sEdit.Show();
				SymbolShowTip(0);
				break;
			case CnInputModeSymbol:
				InputMode = CnInputModeCn;
				CnInputMode = CnInputModeCn;
				DrawInputType();
				ClearCnResult();
			
				SelectedCn = 0;
				PySelIndex = 0;
				CnSelIndex = 0;
			
				ShowPinyinText();
				DrawPinyinList();
				DrawCnList();
			
				CnShowTip(0);
				break;
			case CnInputModeCn:
				InputMode = CnInputModeNum;
				CnInputMode = CnInputModeNum;
				DrawInputType();
				DrawEnList();
			
				sEdit.Str.Clear();
				sEdit.Enable = 0;
				sEdit.Str.Clear();
				sEdit.Pst = -1;
				sEdit.IsShowCursor = 0;
				sEdit.Show();
				NumShowTip(0);
				break;
			case CnInputModeNum:
				InputMode = CnInputModeEn;
				CnInputMode = CnInputModeEn;
				DrawInputType();
				DrawEnList();
			
				sEdit.Str.Clear();
				sEdit.Enable = 1;
				sEdit.Str.Clear();
				sEdit.Pst = -1;
				sEdit.IsShowCursor = 0;
				sEdit.Show();
				EnShowTip(0);
				break;
			}
	}else{
		switch(InputMode){
			case CnInputModeEn:
				return EnKeyIn(aKey);
				break;
			case CnInputModeSymbol:
				return SymbolKeyIn(aKey);
				break;
			case CnInputModeCn:
				return CnKeyIn(aKey);
				break;
			case CnInputModeNum:
				return NumKeyIn(aKey);
				break;
		}
	}
	return nullptr;
}


void TCnInput::SetInputMode(TTextInputMode aInputMode)
{
	switch(aInputMode){
		case CnInputModeSymbol: 
			InputMode = CnInputModeSymbol;
			CnInputMode = aInputMode;
			DrawInputType();
			DrawSymbolList();
		
			sEdit.Enable = 0;
			sEdit.Str.Clear();
			sEdit.Pst = -1;
			sEdit.IsShowCursor = 0;
			sEdit.Show();
			SymbolShowTip(0);
			break;
		case CnInputModeCn:
			InputMode = CnInputModeCn;
			CnInputMode = aInputMode;
			DrawInputType();
			ClearCnResult();
		
			SelectedCn = 0;
			PySelIndex = 0;
			CnSelIndex = 0;
		
			ShowPinyinText();
			DrawPinyinList();
			DrawCnList();
		
			CnShowTip(0);
			break;
		case CnInputModeEn:
			InputMode = CnInputModeEn;
			CnInputMode = aInputMode;
			DrawInputType();
			DrawEnList();
		
			sEdit.Str.Clear();
			sEdit.Enable = 1;
			sEdit.Str.Clear();
			sEdit.Pst = -1;
			sEdit.IsShowCursor = 0;
			sEdit.Show();
			EnShowTip(0);
			break;
		case CnInputModeNum:
			InputMode = CnInputModeNum;
			CnInputMode = aInputMode;
			DrawInputType();
			DrawEnList();
		
			sEdit.Str.Clear();
			sEdit.Enable = 0;
			sEdit.Str.Clear();
			sEdit.Pst = -1;
			sEdit.IsShowCursor = 0;
			sEdit.Show();
			NumShowTip(0);
			break;
		default:
			InputMode = CnInputModeEn;
			CnInputMode = CnInputModeEn;
			DrawInputType();
			DrawEnList();
		
			sEdit.Str.Clear();
			sEdit.Enable = 0;
			sEdit.Str.Clear();
			sEdit.Pst = -1;
			sEdit.IsShowCursor = 0;
			sEdit.Show();
			EnShowTip(0);
			break;
	}
}

int TCnInput::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	return 0;
}
