highlights.cpp
来自「MudMaster 2000 的C++源码」· C++ 代码 · 共 623 行 · 第 1/2 页
CPP
623 行
HIGHLIGHT* CHighlights::GetFirst()
{
if (!m_nCount)
return(NULL);
m_nGetIndex = 0;
return((HIGHLIGHT *)m_ptrList.GetAt(m_nGetIndex));
}
HIGHLIGHT* CHighlights::GetNext()
{
if (m_nGetIndex+1 == m_nCount)
return(NULL);
m_nGetIndex++;
return((HIGHLIGHT *)m_ptrList.GetAt(m_nGetIndex));
}
HIGHLIGHT* CHighlights::GetAt(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(NULL);
return((HIGHLIGHT *)m_ptrList.GetAt(nIndex));
}
HIGHLIGHT* CHighlights::FindExact(const char *pszMask)
{
HIGHLIGHT *pHigh;
CString strMask;
for (int i=0;i<m_nCount;i++)
{
pHigh = (HIGHLIGHT *)m_ptrList.GetAt(i);
HighlightToMask(pHigh,strMask);
if (strMask == pszMask)
{
m_nGetIndex = i;
return(pHigh);
}
}
return(NULL);
}
void CHighlights::CheckHighlight(CTextLine &tl)
{
CString strLine(tl.m_ptrText);
const char *ptr;
CString strTemp;
HIGHLIGHT *pHigh;
int nVarCount;
int nOffset;
int nLineLen = strLine.GetLength();
int j;
for (int i=0;i<m_nCount;i++)
{
pHigh = (HIGHLIGHT *)m_ptrList.GetAt(i);
if (!pHigh->bEnabled)
continue;
// If there aren't any vars, it is really simple. Just see if
// we can find the test in the line.
if (!pHigh->nVars)
{
int nIndex = strLine.Find(pHigh->strToken[0]);
if ((pHigh->bAnchored && nIndex == 0) ||
(!pHigh->bAnchored && nIndex != -1))
{
int nLen = pHigh->strToken[0].GetLength();
for (j=0;j<nLen;j++)
if (tl.m_ptrText[nIndex+j] != '\n')
tl.m_ptrAttr[nIndex+j] = pHigh->wAttr;
}
continue;
}
nVarCount = 0;
nOffset = 0;
for (j=0;j<pHigh->nTokens;j++)
{
ptr = strstr(tl.m_ptrText+nOffset,pHigh->strToken[j]);
if (ptr == NULL)
break;
// If the action starts with a variable and we are on the
// very first token.
if (!j)
{
// If it is anchored, need to make sure that ptr is at
// the beginning of the text.
if (pHigh->bAnchored && ptr != tl.m_ptrText)
break;
if (pHigh->bStartVar)
{
// If the string we just found is pointing at the
// front of the line, then there is no var data to
// be picked off the front.
if (ptr == tl.m_ptrText)
break;
nVarCount++;
}
// Offset needs to point to 1 place after the first token
// ptr-pszLine will skip over the var data + the lenght of the token
// +1 because vars cannot be butted up against on another.
nOffset += ptr-tl.m_ptrText + pHigh->strToken[j].GetLength() + 1;
continue;
}
strTemp = strLine.Mid(nOffset-1,ptr-tl.m_ptrText-nOffset+1);
nOffset += strTemp.GetLength() + pHigh->strToken[j].GetLength();
nVarCount++;
} // for (j=0;j<pAction->nTokens;j++)
// If we exits and we don't have all the variables, the trigger must have
// ended with a variable.
if (nVarCount < pHigh->nVars)
nVarCount++;
if (j == pHigh->nTokens)
{
for (j=0;j<nLineLen;j++)
if (tl.m_ptrText[j] != '\n')
tl.m_ptrAttr[j] = pHigh->wAttr;
}
} // for (int i=0;i<m_nCount;i++)
return;
}
void CHighlights::AttrToText(WORD wAttr, CString &strResult)
{
int nFore = wAttr & 0x0F;
int nBack = wAttr & 0xF0;
strResult.Empty();
switch(nFore)
{
case 0 : strResult = "black"; break;
case F_BLUE : strResult = "blue"; break;
case F_GREEN : strResult = "green"; break;
case F_CYAN : strResult = "cyan"; break;
case F_RED : strResult = "red"; break;
case F_MAGENTA : strResult = "magenta"; break;
case F_BROWN : strResult = "brown"; break;
case F_LIGHTGREY : strResult = "light grey"; break;
case F_DARKGREY : strResult = "dark grey"; break;
case F_LIGHTBLUE : strResult = "light blue"; break;
case F_LIGHTGREEN : strResult = "light green"; break;
case F_LIGHTCYAN : strResult = "light cyan"; break;
case F_LIGHTRED : strResult = "light red"; break;
case F_LIGHTMAGENTA : strResult = "light magenta"; break;
case F_YELLOW : strResult = "yellow"; break;
case F_WHITE : strResult = "white"; break;
}
strResult += ",";
switch(nBack)
{
case B_BLACK : strResult += "back black"; break;
case B_BLUE : strResult += "back blue"; break;
case B_GREEN : strResult += "back green"; break;
case B_CYAN : strResult += "back cyan"; break;
case B_RED : strResult += "back red"; break;
case B_MAGENTA : strResult += "back magenta"; break;
case B_BROWN : strResult += "back brown"; break;
case B_LIGHTGREY : strResult += "back light grey"; break;
}
}
WORD CHighlights::TextToAttr(const char *pszColor, WORD wCurrentAttr)
{
CString strColor(pszColor);
strColor.MakeLower();
strColor.TrimRight();
strColor.TrimLeft();
WORD wClearLo = 0x00F0;
WORD wClearHi = 0x000F;
// Foreground colors.
if (strColor == "black")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_BLACK);
}
if (strColor == "blue")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_BLUE);
}
if (strColor == "green")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_GREEN);
}
if (strColor == "cyan")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_CYAN);
}
if (strColor == "red")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_RED);
}
if (strColor == "magenta")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_MAGENTA);
}
if (strColor == "brown")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_BROWN);
}
if (strColor == "light grey")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_LIGHTGREY);
}
if (strColor == "dark grey")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_DARKGREY);
}
if (strColor == "light blue")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_LIGHTBLUE);
}
if (strColor == "light green")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_LIGHTGREEN);
}
if (strColor == "light cyan")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_LIGHTCYAN);
}
if (strColor == "light red")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_LIGHTRED);
}
if (strColor == "light magenta")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_LIGHTMAGENTA);
}
if (strColor == "yellow")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_YELLOW);
}
if (strColor == "white")
{
wCurrentAttr &= wClearLo;
return(wCurrentAttr |= F_WHITE);
}
// Background colors.
if (strColor == "back black")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_BLACK);
}
if (strColor == "back blue")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_BLUE);
}
if (strColor == "back green")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_GREEN);
}
if (strColor == "back cyan")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_CYAN);
}
if (strColor == "back red")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_RED);
}
if (strColor == "back magenta")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_MAGENTA);
}
if (strColor == "back brown")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_BROWN);
}
if (strColor == "back light grey")
{
wCurrentAttr &= wClearHi;
return(wCurrentAttr |= B_LIGHTGREY);
}
return(wCurrentAttr);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?