📄 ppeditbox.cpp
字号:
#include "GUIpp.h"
#include "ppEditBox.h"
#include "IME_PY.h"
#if _USE_GUI == 1
//////////////////////////////////////////////////////////////////////
//数字和字母对应关系表
char *ppIME_EN_Table[10]={
" ,.\'\":;?!@#$%&*+-()[]{}", //0
" ,.\'\":;?!@#$%&*+-()[]{}", //1
"abcABC", //2
"defDEF", //3
"ghiGHI", //4
"jklJKL", //5
"mnoMNO", //6
"pqrsPQRS", //7
"tuvTUV", //8
"wxyzWXYZ" //9
};
//////////////////////////////////////////////////////////////////////
CEditBox::CEditBox() : w_EditBoxTimer(this)
{
w_nTextLimint = 0;
w_bShowCursor = 0;
w_bHaveFocus = 0;
w_fontType = enFont16Bit;
w_nIME_Index = IME_ID_digit;
w_nIME_State = IME_ST_digit;
w_nStyle = 0;
w_pCandidateBar = CCandidateBar::Instance();
}
CEditBox::~CEditBox()
{
}
int CEditBox::CreateWindow(
CWnd * pParentWnd,
int nX1,
int nY1,
int nWidth,
int nHeight,
int nTextLimit)
{
w_nTextLimint = nTextLimit;
w_bShowCursor = 0;
w_bHaveFocus = 0;
w_fontType = enFont16Bit;
w_nIME_Index = IME_ID_digit;
w_nIME_State = IME_ST_digit;
Clear();
//创建窗体
int nRet = CWnd::CreateWindow(pParentWnd, nX1, nY1, nWidth, nHeight);
w_EditBoxTimer.Start(5, 5);
return nRet;
}
void CEditBox::OnPaint()
{
int x;
int y;
int nCurrsorPosition;
int nTextLenght;
CRect rc;
GetWindowRect(&rc);
rc.w_nY2 = rc.w_nY2 -1;
rc.w_nX2 = rc.w_nX2 -1;
// 默认状态
if(!(w_nStyle))
{
//ButtonFrame(rc.x1,rc.y1,rc.x2-rc.x1,rc.y2-rc.y1,1);
// @ltq001.2005-8-4 修改文本框的外形为方角
RectFrame(rc.w_nX1, rc.w_nY1, rc.w_nX2-rc.w_nX1, rc.w_nY2-rc.w_nY1);
TextOut((const unsigned char *)(w_strText),
rc.w_nX1 + 3,
rc.w_nY1 + (rc.w_nY2-rc.w_nY1-GetLineCNT(w_fontType))/2,
w_fontType,
0);
if(w_bShowCursor)
{
//draw cursor
nCurrsorPosition = GetTextWidth((const unsigned char *)(w_strText), w_fontType, w_nCursorIndex);
DrawVerticalLine(rc.w_nX1 + 3+nCurrsorPosition,rc.w_nY1+3,rc.w_nY2-rc.w_nY1-6,1);
}
}
else if((w_nStyle & EDIT_STYLE_NEG)) // 在反显状态下文本框为无框的,且不会显示光标和图标
{
RectangleBlock(rc.w_nX1, rc.w_nY1, rc.w_nX2-rc.w_nX1, rc.w_nY2-rc.w_nY1, 0);
// 判断为居中显示还是向左对齐
if(w_nStyle & EDIT_STYLE_ALIGN_MID)
{
// 居中对齐
TextOut((const unsigned char *)(w_strText),
rc.w_nX1 + ((rc.w_nX2-rc.w_nX1-GetTextWidth((const unsigned char *)(w_strText),
w_fontType,-1))/2),
rc.w_nY1 + (rc.w_nY2-rc.w_nY1-GetLineCNT(w_fontType))/2,
w_fontType,
1);
}
else
{
// 左对齐
TextOut((const unsigned char *)(w_strText),
rc.w_nX1 + 3,
rc.w_nY1 + (rc.w_nY2-rc.w_nY1-GetLineCNT(w_fontType))/2,
w_fontType,
1);
}
}
else
{
// 正显状态下
if(w_nStyle & EDIT_STYLE_NO_FRAME)
{
// 无框
RectangleBlock(rc.w_nX1, rc.w_nY1, rc.w_nX2-rc.w_nX1, rc.w_nY2-rc.w_nY1, 1);
}
else if(w_nStyle & EDIT_STYLE_UNDERLINE) // 添加下滑线支持
{
RectangleBlock(rc.w_nX1, rc.w_nY1, rc.w_nX2-rc.w_nX1, rc.w_nY2-rc.w_nY1, 1);
DrawHorizontalLine(rc.w_nX1, rc.w_nY2, rc.w_nX2-rc.w_nX1, 1);
}
else
{
// 有框
//ButtonFrame(rc.x1,rc.y1,rc.x2-rc.x1,rc.y2-rc.y1,1);
//////////////////////////////////
// @ltq001.2005-8-4 修改文本框的外形为方角
RectFrame(rc.w_nX1, rc.w_nY1, rc.w_nX2-rc.w_nX1, rc.w_nY2-rc.w_nY1);
}
if(w_nStyle & EDIT_STYLE_ALIGN_MID)
{
// 居中对齐则不支持光标
TextOut((const unsigned char *)(w_strText),
rc.w_nX1 + ((rc.w_nX2-rc.w_nX1-GetTextWidth((const unsigned char *)(w_strText),
w_fontType,-1))/2),
rc.w_nY1 + (rc.w_nY2-rc.w_nY1-GetLineCNT(w_fontType))/2,
w_fontType,
0);
}
else
{
// 左对齐
TextOut((const unsigned char *)(w_strText),
rc.w_nX1 + 3,
rc.w_nY1 + (rc.w_nY2-rc.w_nY1-GetLineCNT(w_fontType))/2,
w_fontType,
0);
if(w_bShowCursor)
{
//draw cursor
nCurrsorPosition = GetTextWidth((const unsigned char *)(w_strText),
w_fontType, w_nCursorIndex);
DrawVerticalLine(rc.w_nX1 + 3+nCurrsorPosition, rc.w_nY1+3, rc.w_nY2-rc.w_nY1-6, 1);
}
}
}
//增加图标表示当前输入方法
if(w_bHaveFocus)
{
x = rc.w_nX2 - 20;
y = (rc.w_nY1 + rc.w_nY2)/2 - 8;
switch(w_nIME_Index)
{
case IME_ID_digit:
DrawIcon(ICON16_NU,x,y,0);
break;
case IME_ID_CH:
DrawIcon(ICON16_CH,x,y,0);
break;
case IME_ID_EN:
DrawIcon(ICON16_EN,x,y,0);
break;
}
}
}
void CEditBox::OnTimer(CTimer *pTimer)
{
if(w_bHaveFocus)
{
// 在正显示模式下,且为左对齐的情况下才有光标闪烁
if( !(w_nStyle & EDIT_STYLE_NEG) &&
!(w_nStyle & EDIT_STYLE_ALIGN_MID) )
{
w_bShowCursor = !w_bShowCursor;
SetUIDirty(1);
// RefreshAll();
}
//计时英文字母选择状态延续的时间
if(w_nIME_State == IME_ST_EN_CHOOSE)
{
w_nEN_TicksRemained--;
if(w_nEN_TicksRemained <= 0)
{
//超时自动切换
w_nIME_State = IME_ST_EN_IDLE;
}
}
//标点选择态
if(w_nIME_State == IME_ST_punc_CHOOSE)
{
w_nEN_TicksRemained--;
if(w_nEN_TicksRemained <= 0)
{
//超时自动切换
if(w_nIME_Index == IME_ID_EN)
{
w_nIME_State = IME_ST_EN_IDLE;
}
else if(w_nIME_Index == IME_ID_CH)
{
w_nIME_State = IME_ST_CH_IDLE;
}
}
}
}
}
void CEditBox::Clear()
{
int i;
for(i = 0; i < EDIT_MAX_TEXT_LEN; i++)
{
w_strText[i] = 0;
}
w_nCursorIndex = 0;
w_nTextCounter = 0;
}
void CEditBox::CursorMove(int nStep)
{
if(w_nCursorIndex + nStep <0)
{
w_nCursorIndex = 0;
}
else if(w_nCursorIndex + nStep > w_nTextCounter)
{
w_nCursorIndex = w_nTextCounter;
}
else
{
w_nCursorIndex += nStep;
}
w_bShowCursor = 1;
SetUIDirty(1);
}
void CEditBox::SetCurChar(char c)
{
int nCurOffset = GetWordIndex(w_strText, w_nCursorIndex);
if(w_nCursorIndex<= w_nTextCounter)
{
w_strText[nCurOffset-1] = c;
SetUIDirty(1);
}
return;
}
void CEditBox::Insert(char * strChar)
{
int nOffset;
int nInsertLen;
int i;
int nStrLen;
int nInsertWords;
nStrLen=strlen(w_strText);
nOffset = GetWordIndex(w_strText, w_nCursorIndex);
nInsertWords=GetWordLen(strChar);
if(w_nTextCounter + nInsertWords < w_nTextLimint)
{
nInsertLen = strlen(strChar);
for(i = nStrLen; i >= nOffset; i--)//@ltq001.2005-8-3修正bug
{
w_strText[i+nInsertLen] = w_strText[i];
}
strncpy(w_strText + nOffset, strChar, nInsertLen);
w_nTextCounter += nInsertWords;
w_nCursorIndex += nInsertWords;
}
SetUIDirty(1);
}
void CEditBox::Delete()
{
int nCurOffset;
int nNextOffset;
int nTotalLenght;
int i;
char *p;
if( w_nCursorIndex >= w_nTextCounter)
{
return;
}
nCurOffset = GetWordIndex(w_strText, w_nCursorIndex);
nNextOffset = GetWordIndex(w_strText, w_nCursorIndex+1);
nTotalLenght = strlen(w_strText);
p = w_strText + nCurOffset;
for(i = nNextOffset; i <= nTotalLenght; i++)
{
*p++ = w_strText[i];
}
w_nTextCounter -- ;
SetUIDirty(1);
}
void CEditBox::Backspace()
{
int nCurOffset;
int nNextOffset;
int nTotalLenght;
int i;
char *p;
if( w_nCursorIndex == 0)
{
return;
}
nCurOffset = GetWordIndex(w_strText, w_nCursorIndex-1);
nNextOffset = GetWordIndex(w_strText, w_nCursorIndex);
nTotalLenght = strlen(w_strText);
p = w_strText+nCurOffset;
for(i = nNextOffset; i <= nTotalLenght; i++)
{
*p++ = w_strText[i];
}
w_nTextCounter-- ;
w_nCursorIndex--;
SetUIDirty(1);
}
int CEditBox::GetTextCounter()
{
return w_nTextCounter;
}
void CEditBox::SetFont(enum enFontType font)
{
w_fontType = font;
}
void CEditBox::SetStyle(int nStyle)
{
w_nStyle = nStyle;
}
////////////////////////////////////////////////////////////
//
//
void CEditBox::SetIME(int nIME_Index)
{
switch(nIME_Index)
{
case IME_ID_digit:
w_nIME_Index = nIME_Index;
w_nIME_State = IME_ST_digit;
break;
case IME_ID_EN:
w_nIME_Index = nIME_Index;
w_nIME_State = IME_ST_EN_IDLE;
break;
case IME_ID_CH:
w_nIME_Index = nIME_Index;
w_nIME_State = IME_ST_CH_IDLE;
break;
}
}
void CEditBox::SetFocus(int bHaveFocus)
{
if(w_bHaveFocus && !bHaveFocus)
{
//失去焦点时,输入法的内部状态需要复原
switch(w_nIME_State)
{
case IME_ST_digit:
case IME_ST_EN_IDLE:
case IME_ST_CH_IDLE:
case IME_ST_EN_CHOOSE:
case IME_ST_punc_CHOOSE:
break;
case IME_ST_punc:
case IME_ST_CH_CHOOSE:
//销毁候选窗口
w_pCandidateBar->CloseWindow();
break;
default:
break;
}
//恢复到空闲状态
switch(w_nIME_Index)
{
case IME_ID_digit:
w_nIME_State = IME_ST_digit;
break;
case IME_ID_EN:
w_nIME_State = IME_ST_EN_IDLE;
break;
case IME_ID_CH:
w_nIME_State = IME_ST_CH_IDLE;
break;
default:
break;
}
}
w_bHaveFocus = bHaveFocus;
w_bShowCursor = w_bHaveFocus;
SetUIDirty(1);
// RefreshAll();
}
int CEditBox::HaveFocus()
{
return w_bHaveFocus ;
}
void CEditBox::OnKey(int nKeyCode, int bKeyDown)
{
}
////////////////////////////////////////////////////////////////////////////
//
//本函数为一个代理函数,由上层应用程序调用,用来接管处理
//用户按键,本函数返回>0的值代表输入的键值被接受并处理
//
int CEditBox::HandleKey(int nKeyCode, int flag)
{
const IME_PY_DCODE * pIME_py_dcode;
char strTextTemp[10];
//在所有处理之前,首先根据本输入框的enbale状态决定是否接受按键输入
if (!(w_bHaveFocus))
{
return 0;
}
//
// 输入IDLE状态的共同按键响应:上下键移动光标,删除键删除光标前一个字符
//
switch(w_nIME_State)
{
case IME_ST_digit:
case IME_ST_EN_IDLE:
case IME_ST_CH_IDLE:
{
if(nKeyCode == IME_KEY_ESC)
{
if(w_strText[0] == 0)
{
SetFocus(0);
}
else
{
Backspace();
//RefreshAll();
}
return 1;
}
else if(nKeyCode == IME_KEY_SELUP)
{
CursorMove(-1);
// RefreshAll();
return 1;
}
else if(nKeyCode == IME_KEY_SELDOWN)
{
CursorMove(1);
// RefreshAll();
return 1;
}
break;
}
}
//
// 标点输入相关:按下number 1输入标点但是并不改变输入法
//
switch(w_nIME_State)
{
case IME_ST_EN_IDLE:
case IME_ST_CH_IDLE:
{
if(nKeyCode == IME_KEY_NUM1)
{
//显示全部候选标点,转入标点输入状态
CRect rc;
GetWindowRect(&rc);
w_pCandidateBar->CreateWindow(GetParentWnd(),//候选窗显示在和EditBox同级别的窗体中
0,
0, //候选窗的位置坐标在后面的代码中调整
200, //宽度
GetLineCNT(w_fontType)+8, //高度
w_fontType); //候选窗使用和editbox一样的字体
//显示全部候选标点
w_pCandidateBar->SetCandidate("标点", ppIME_EN_Table[0]);
//弹出候选窗到editbox周围
w_pCandidateBar->Pop(&rc);
w_nIME_State = IME_ST_punc;
return 1;
}
else if(nKeyCode == IME_KEY_NUM0 )
{
//简便标点输入
w_nEN_KeyNumber = IME_KEY_NUM0;
w_nEN_CurCandidate = 0;
strTextTemp[0] = ppIME_EN_Table[0][w_nEN_CurCandidate];
strTextTemp[1] =0 ;
Insert(strTextTemp);
//RefreshAll();
w_nIME_State = IME_ST_punc_CHOOSE;
w_nEN_TicksRemained = 3;
return 1;
}
break;
}
case IME_ST_punc_CHOOSE:
{
if(w_nEN_KeyNumber == nKeyCode)
{
//1 second内重复按0键,更改当前输入标点为下一个候选
w_nEN_CurCandidate++;
if(ppIME_EN_Table[0][w_nEN_CurCandidate] == 0)
{
w_nEN_CurCandidate = 0;
}
SetCurChar(ppIME_EN_Table[0][w_nEN_CurCandidate]);
//RefreshAll();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -