📄 textinput.cpp
字号:
int nNum = m_nScrollPos - m_dwCursor.X - 1;
if (nStart+nNum > nTextLen)
nNum = nTextLen - nStart;
CString strTemp(m_strText.Mid(nStart,nNum));
HideCursor();
PrintCh(ch);
int x = m_dwCursor.X;
Gotoxy(x,m_nYPos);
Print(strTemp);
Gotoxy(x,m_nYPos);
ShowCursor();
m_nTextIndex++;
}
}
else
{
if (m_dwCursor.X == m_nScrollPos)
{
m_strText.SetAt(m_nTextIndex,ch);
ScrollLeft();
m_nTextIndex++;
}
else
{
PrintCh(ch);
m_strText.SetAt(m_nTextIndex,ch);
m_nTextIndex++;
}
}
}
void CTextInput::ArrowLeft()
{
if (!m_nTextIndex)
return;
if (m_dwCursor.X == m_nXPos)
ScrollRight();
m_nTextIndex--;
if (m_dwCursor.X != m_nXPos)
Gotoxy(m_dwCursor.X-1,m_nYPos);
}
void CTextInput::ArrowRight()
{
if (m_strText.GetLength() == m_nTextIndex)
return;
if (m_dwCursor.X == m_nScrollPos)
ScrollLeft();
m_nTextIndex++;
if (m_dwCursor.X != m_nScrollPos)
Gotoxy(m_dwCursor.X+1,m_nYPos);
}
void CTextInput::WordLeft()
{
// Start at 1 char behind cursor.
int nIndex = m_nTextIndex-1;
// If the next char is a space, need to skip all the spaces
// and start with next alpha.
while(nIndex && m_strText.GetAt(nIndex) == ' ')
nIndex--;
// Walk up until the next space.
while(nIndex && m_strText.GetAt(nIndex) != ' ')
nIndex--;
// If not at the head of the buffer, put the cursor one char
// forward (don't want to leave it on the space).
if (nIndex)
nIndex++;
// An cheap and easy fix, just scroll the cursor 1 at a time. This
// way I won't have to figure out if anything needs to be scrolled.
int nDif = m_nTextIndex - nIndex;
while(nDif)
{
ArrowLeft();
nDif--;
}
}
void CTextInput::WordRight()
{
// Start at the next cursor char.
int nIndex = m_nTextIndex+1;
// Walk all the alpha chars until we hit a space.
while(nIndex < m_nMaxLen && m_strText.GetAt(nIndex) != ' ')
nIndex++;
// Eat up all the spaces to find the next word.
while(nIndex < m_nMaxLen && m_strText.GetAt(nIndex) == ' ')
nIndex++;
if (nIndex >= m_nMaxLen)
return;
int nDif = nIndex - m_nTextIndex;
while(nDif)
{
ArrowRight();
nDif--;
}
}
void CTextInput::Delete()
{
int nLen = m_strText.GetLength();
// If at the end of the line, nothing to delete.
// Or, if the the scrollable position don't let a
// delete occurr since you cannot see what you would
// be deleting.
if (m_nTextIndex == nLen || m_dwCursor.X == m_nScrollPos)
return;
// Grab the right portion of the string to appent.
CString strRight(m_strText.Right(nLen-m_nTextIndex-1));
m_strText = m_strText.Left(m_nTextIndex) + strRight;
// Get the text to the right of the character being
// deleted that will fit in the visible area of the
// control.
nLen--; // Len is now 1 less.
int nNum = m_nScrollPos - m_dwCursor.X;
if (m_nTextIndex+nNum > nLen)
nNum = nLen - m_nTextIndex;
CString strTemp(m_strText.Mid(m_nTextIndex,nNum));
HideCursor();
int x = m_dwCursor.X;
Print(strTemp);
// If number printed was less than will fit need to
// erase the character still hanging out at the end.
if (nNum != m_nScrollPos - x)
PrintCh(' ');
Gotoxy(x,m_nYPos);
ShowCursor();
}
void CTextInput::Home()
{
if (!m_nTextIndex)
return;
Gotoxy(m_nXPos,m_nYPos);
HideCursor();
if (m_strText.GetLength() > m_nMaxVisible)
Print(m_strText.Left(m_nMaxVisible));
else
{
Print(m_strText);
int nLen = m_strText.GetLength();
int nBlanks = m_nMaxVisible - nLen;
FillChar(m_nXPos+nLen,m_nYPos,nBlanks,' ',m_wAttr);
}
Gotoxy(m_nXPos,m_nYPos);
ShowCursor();
m_nTextIndex = 0;
}
void CTextInput::End()
{
int nLen = m_strText.GetLength();
if (nLen == m_nTextIndex)
return;
m_nTextIndex = nLen;
if (m_nTextIndex <= m_nMaxVisible)
Gotoxy(m_nXPos+nLen,m_nYPos);
else
{
HideCursor();
Gotoxy(m_nXPos,m_nYPos);
Print(m_strText.Right(m_nMaxVisible));
ShowCursor();
}
}
void CTextInput::ScrollLeft()
{
HideCursor();
int nLen = m_strText.GetLength();
int nStart = m_nTextIndex-m_nMaxVisible+1;
int nNum = m_nMaxVisible;
if (nNum > nLen-nStart)
nNum = nLen-nStart;
CString strTemp(m_strText.Mid(nStart,nNum));
Gotoxy(m_nXPos,m_nYPos);
CString strPadding(' ',m_nMaxVisible-nNum);
Print(strTemp+strPadding);
Gotoxy(m_nXPos+nNum,m_nYPos);
ShowCursor();
}
void CTextInput::ScrollRight()
{
HideCursor();
CString strTemp(m_strText.Mid(m_nTextIndex-1,m_nMaxVisible));
Print(strTemp);
Gotoxy(m_nXPos,m_nYPos);
ShowCursor();
}
void CTextInput::SetText(const char *pszText)
{
FillChar(m_nXPos,m_nYPos,m_nWidth,' ',m_wAttr);
m_strText = pszText;
if (m_strText.GetLength() > m_nMaxLen)
m_strText = m_strText.Left(m_nMaxLen);
// This will force Home() to redraw the text.
m_nTextIndex = m_nMaxVisible+1;
Home();
}
void CTextInput::InsertText(const char *pszAdd)
{
CString strAdd(pszAdd);
int nLen = strAdd.GetLength();
m_strText = m_strText.Left(m_nTextIndex)+strAdd+m_strText.Right(m_strText.GetLength()-m_nTextIndex);
m_nTextIndex += nLen;
// Need to print this new text. But there may be a need to scroll
// the input window.
HideCursor();
int nNewX = m_dwCursor.X + nLen;
if (nNewX >= m_nScrollPos)
{
int nStartIndex = m_nTextIndex-m_dwCursor.X;
Gotoxy(m_nXPos,m_nYPos);
Print(m_strText.Mid(nStartIndex,m_nMaxVisible));
}
else
Print(strAdd);
ShowCursor();
}
void CTextInput::TabList()
{
if (!m_nTextIndex)
return;
// If pressed after the first one, need to cycles to next word in the
// tabl list that matches. Start by erasing the last word we printed.
/* if (m_nTabPressed)
{
int nBackWards = m_strTabWord.GetLength();
HideCursor();
while(nBackWards)
{
InsertChar(8);
nBackWards--;
}
InsertText(m_strTabOriginal);
ShowCursor();
} */
int nIndex = m_nTextIndex-1;
while(nIndex && m_strText.GetAt(nIndex) != ' ')
nIndex--;
int nCharsToCopy = m_nTextIndex-nIndex;
if (nIndex)
{
nIndex++;
nCharsToCopy--;
}
if (!nCharsToCopy)
return;
m_strTabOriginal = m_strText.Mid(nIndex,nCharsToCopy);
TAB *pTab = _tabList.Lookup(m_strTabOriginal);
if (!pTab)
return;
/* m_nTabPressed++;
int nTabCount = 0;
while(pTab && nTabCount != m_nTabPressed-1)
{
nTabCount++;
pTab = _tabList.GetNext();
}
if (!pTab || pTab->strText.Find(m_strTabOriginal))
{
m_nTabPressed = 0;
return;
}
m_strTabWord = pTab->strText; */
int nLen = pTab->strText.GetLength()-nCharsToCopy;
CString strAdd(pTab->strText.Mid(nCharsToCopy,nLen));
InsertText(strAdd);
}
void CTextInput::SetFore(int nColor)
{
m_nFore = nColor;
WORD wFore = ForeColorAttribute(nColor);
// Clear the current foreground bits.
m_wAttr &= 0x00F0;
m_wAttr |= wFore;
Redraw();
}
void CTextInput::SetBack(int nColor)
{
m_nBack = nColor;
WORD wBack = BackColorAttribute(nColor);
// Clear the current background bits.
m_wAttr &= 0x000F;
m_wAttr |= wBack;
Redraw();
}
WORD CTextInput::BackColorAttribute(int nColor)
{
WORD wAttr = B_BLUE;
switch(nColor)
{
case 0 : wAttr = B_BLACK; break;
case 1 : wAttr = B_BLUE; break;
case 2 : wAttr = B_GREEN; break;
case 3 : wAttr = B_CYAN; break;
case 4 : wAttr = B_RED; break;
case 5 : wAttr = B_MAGENTA; break;
case 6 : wAttr = B_BROWN; break;
case 7 : wAttr = B_LIGHTGREY; break;
}
return(wAttr);
}
WORD CTextInput::ForeColorAttribute(int nColor)
{
WORD wAttr = F_WHITE;
switch(nColor)
{
case 0 : wAttr = F_BLACK; break;
case 1 : wAttr = F_BLUE; break;
case 2 : wAttr = F_GREEN; break;
case 3 : wAttr = F_CYAN; break;
case 4 : wAttr = F_RED; break;
case 5 : wAttr = F_MAGENTA; break;
case 6 : wAttr = F_BROWN; break;
case 7 : wAttr = F_LIGHTGREY; break;
case 8 : wAttr = F_DARKGREY; break;
case 9 : wAttr = F_LIGHTBLUE; break;
case 10 : wAttr = F_LIGHTGREEN; break;
case 11 : wAttr = F_LIGHTCYAN; break;
case 12 : wAttr = F_LIGHTRED; break;
case 13 : wAttr = F_LIGHTMAGENTA; break;
case 14 : wAttr = F_YELLOW; break;
case 15 : wAttr = F_WHITE; break;
}
return(wAttr);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -