RedPanda-CPP/RedPandaIDE/qsynedit/TextPainter.cpp

1096 lines
46 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-05-14 23:56:43 +08:00
#include "TextPainter.h"
#include "SynEdit.h"
#include "Constants.h"
#include <cmath>
2021-05-24 00:41:00 +08:00
#include <QDebug>
2021-05-14 23:56:43 +08:00
2021-05-24 00:41:00 +08:00
SynEditTextPainter::SynEditTextPainter(SynEdit *edit, QPainter *painter, int FirstRow, int LastRow, int FirstCol, int LastCol)
2021-05-14 23:56:43 +08:00
{
this->edit = edit;
2021-05-24 00:41:00 +08:00
this->painter = painter;
2021-05-21 23:33:53 +08:00
this->aFirstRow = FirstRow;
this->aLastRow = LastRow;
this->FirstCol = FirstCol;
this->LastCol = LastCol;
}
void SynEditTextPainter::paintTextLines(const QRect& clip)
{
painter->fillRect(clip, edit->mBackgroundColor);
2021-05-21 23:33:53 +08:00
AClip = clip;
vFirstLine = edit->rowToLine(aFirstRow);
vLastLine = edit->rowToLine(aLastRow);
bCurrentLine = false;
// If the right edge is visible and in the invalid area, prepare to paint it.
// Do this first to realize the pen when getting the dc variable.
bDoRightEdge = false;
if (edit->mRightEdge > 0) { // column value
2021-05-24 21:48:03 +08:00
nRightEdge = edit->textOffset()+ edit->mRightEdge * edit->mCharWidth; // pixel value
2021-05-21 23:33:53 +08:00
if (nRightEdge >= AClip.left() &&nRightEdge <= AClip.right()) {
bDoRightEdge = true;
QPen pen(edit->mRightEdgeColor,1);
painter->setPen(pen);
}
}
// Paint the visible text lines. To make this easier, compute first the
// necessary information about the selected area: is there any visible
// selected area, and what are its lines / columns?
if (vLastLine >= vFirstLine) {
2022-07-24 22:53:20 +08:00
computeSelectionInfo();
paintLines();
2021-05-21 23:33:53 +08:00
}
2021-05-26 18:32:17 +08:00
//painter->setClipping(false);
2021-05-21 23:33:53 +08:00
// If anything of the two pixel space before the text area is visible, then
// fill it with the component background color.
2021-05-26 18:32:17 +08:00
if (AClip.left() <edit->mGutterWidth + 2) {
2021-05-21 23:33:53 +08:00
rcToken = AClip;
rcToken.setLeft( std::max(AClip.left(), edit->mGutterWidth));
rcToken.setRight(edit->mGutterWidth + 2);
// Paint whole left edge of the text with same color.
// (value of WhiteAttribute can vary in e.g. MultiSyn)
2021-05-27 20:33:25 +08:00
painter->fillRect(rcToken,colEditorBG());
2021-05-21 23:33:53 +08:00
// Adjust the invalid area to not include this area.
AClip.setLeft(rcToken.right());
}
// If there is anything visible below the last line, then fill this as well.
rcToken = AClip;
rcToken.setTop((aLastRow - edit->mTopLine + 1) * edit->mTextHeight);
if (rcToken.top() < rcToken.bottom()) {
2021-05-27 20:33:25 +08:00
painter->fillRect(rcToken,colEditorBG());
2021-05-21 23:33:53 +08:00
// Draw the right edge if necessary.
if (bDoRightEdge) {
QPen pen(edit->mRightEdgeColor,1);
painter->setPen(pen);
painter->drawLine(nRightEdge, rcToken.top(),nRightEdge, rcToken.bottom() + 1);
}
}
// This messes with pen colors, so draw after right margin has been drawn
2022-07-24 22:53:20 +08:00
paintFoldAttributes();
2021-05-21 23:33:53 +08:00
}
void SynEditTextPainter::paintGutter(const QRect& clip)
{
int cRow;
QRect rcLine, rcFold;
QString s;
int vLine;
int vLineTop;
int x;
AClip = clip;
2021-05-27 20:33:25 +08:00
painter->fillRect(AClip,edit->mGutter.color());
2021-05-21 23:33:53 +08:00
2021-05-27 20:33:25 +08:00
rcLine=AClip;
2021-05-21 23:33:53 +08:00
if (edit->mGutter.showLineNumbers()) {
2021-05-26 18:32:17 +08:00
// prepare the rect initially
rcLine = AClip;
rcLine.setRight( std::max(rcLine.right(), edit->mGutterWidth - 2));
rcLine.setBottom(rcLine.top());
2021-05-21 23:33:53 +08:00
if (edit->mGutter.useFontStyle()) {
painter->setFont(edit->mGutter.font());
} else {
QFont newFont = painter->font();
newFont.setBold(false);
newFont.setItalic(false);
newFont.setStrikeOut(false);
newFont.setUnderline(false);
painter->setFont(newFont);
}
QColor textColor;
2021-08-22 23:48:00 +08:00
if (edit->mGutter.textColor().isValid()) {
textColor = edit->mGutter.textColor();
2021-05-21 23:33:53 +08:00
} else {
textColor = edit->mForegroundColor;
2021-05-21 23:33:53 +08:00
}
// draw each line if it is not hidden by a fold
BufferCoord selectionStart = edit->blockBegin();
BufferCoord selectionEnd = edit->blockEnd();
2021-05-21 23:33:53 +08:00
for (int cRow = aFirstRow; cRow <= aLastRow; cRow++) {
vLine = edit->rowToLine(cRow);
if ((vLine > edit->mDocument->count()) && (edit->mDocument->count() > 0 ))
2021-05-21 23:33:53 +08:00
break;
if (edit->mGutter.activeLineTextColor().isValid()) {
if (
(edit->mCaretY==vLine) ||
(edit->mActiveSelectionMode == SynSelectionMode::Column && vLine >= selectionStart.line && vLine <= selectionEnd.line)
)
painter->setPen(edit->mGutter.activeLineTextColor());
else
painter->setPen(textColor);
} else {
painter->setPen(textColor);
}
2021-05-21 23:33:53 +08:00
vLineTop = (cRow - edit->mTopLine) * edit->mTextHeight;
// next line rect
rcLine.setTop(vLineTop);
rcLine.setBottom(rcLine.top() + edit->mTextHeight);
s = edit->mGutter.formatLineNumber(vLine);
2021-05-24 00:41:00 +08:00
edit->onGutterGetText(vLine,s);
2021-05-21 23:33:53 +08:00
QRectF textRect;
textRect = painter->boundingRect(textRect, Qt::AlignLeft,s);
painter->drawText(
(edit->mGutterWidth - edit->mGutter.rightOffset() - 2) - textRect.width(),
2021-05-24 18:11:07 +08:00
rcLine.bottom() + ((edit->mTextHeight - int(textRect.height())) / 2 - painter->fontMetrics().descent()),
2021-05-21 23:33:53 +08:00
s
);
}
}
// Draw the folding lines and squares
if (edit->mUseCodeFolding) {
for (cRow = aLastRow; cRow>=aFirstRow; cRow--) {
vLine = edit->rowToLine(cRow);
if ((vLine > edit->mDocument->count()) && (edit->mDocument->count() != 0))
continue;
// Form a rectangle for the square the user can click on
rcFold.setLeft(edit->mGutterWidth - edit->mGutter.rightOffset());
rcFold.setTop((cRow - edit->mTopLine) * edit->mTextHeight);
rcFold.setRight(rcFold.left() + edit->mGutter.rightOffset() - 4);
rcFold.setBottom(rcFold.top() + edit->mTextHeight);
painter->setPen(edit->mCodeFolding.folderBarLinesColor);
// Need to paint a line?
if (edit->foldAroundLine(vLine)) {
x = rcFold.left() + (rcFold.width() / 2);
painter->drawLine(x,rcFold.top(), x, rcFold.bottom());
}
// Need to paint a line end?
if (edit->foldEndAtLine(vLine)) {
x = rcFold.left() + (rcFold.width() / 2);
painter->drawLine(x,rcFold.top(), x, rcFold.top() + rcFold.height() / 2);
painter->drawLine(x,
rcFold.top() + rcFold.height() / 2,
rcFold.right() - 2 ,
rcFold.top() + rcFold.height() / 2);
}
// Any fold ranges beginning on this line?
PSynEditFoldRange FoldRange = edit->foldStartAtLine(vLine);
if (FoldRange) {
// Draw the bottom part of a line
if (!FoldRange->collapsed) {
x = rcFold.left() + (rcFold.width() / 2);
painter->drawLine(x, rcFold.top() + rcFold.height() / 2,
x, rcFold.bottom());
}
// make a square rect
inflateRect(rcFold,-2, 0);
rcFold.setTop(
rcFold.top() + ((edit->mTextHeight - rcFold.width()) / 2));
rcFold.setBottom(rcFold.top() + rcFold.width());
// Paint the square the user can click on
painter->setBrush(edit->mGutter.color());
painter->setPen(edit->mCodeFolding.folderBarLinesColor);
painter->drawRect(rcFold);
// Paint minus sign
painter->drawLine(
rcFold.left() + 2, rcFold.top() + (rcFold.height() / 2 ),
rcFold.right() - 2, rcFold.top() + (rcFold.height() / 2 ));
// Paint vertical line of plus sign
if (FoldRange->collapsed) {
x = rcFold.left() + (rcFold.width() / 2);
painter->drawLine(x, rcFold.top() + 2,
x, rcFold.bottom() - 2);
}
}
}
}
2021-05-21 23:33:53 +08:00
for (cRow = aFirstRow; cRow <=aLastRow; cRow++) {
vLine = edit->rowToLine(cRow);
if ((vLine > edit->mDocument->count()) && (edit->mDocument->count() != 0))
break;
2021-05-24 00:41:00 +08:00
edit->onGutterPaint(*painter,vLine, 0, (cRow - edit->mTopLine) * edit->mTextHeight);
2021-05-21 23:33:53 +08:00
}
2021-05-14 23:56:43 +08:00
}
QColor SynEditTextPainter::colEditorBG()
{
if (edit->mActiveLineColor.isValid() && bCurrentLine) {
return edit->mActiveLineColor;
} else {
return edit->mBackgroundColor;
2021-05-14 23:56:43 +08:00
}
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::computeSelectionInfo()
2021-05-14 23:56:43 +08:00
{
BufferCoord vStart;
BufferCoord vEnd;
bAnySelection = false;
// Only if selection is visible anyway.
if (!edit->mHideSelection || edit->hasFocus()) {
bAnySelection = true;
// Get the *real* start of the selected area.
if (edit->mBlockBegin.line < edit->mBlockEnd.line) {
2021-05-14 23:56:43 +08:00
vStart = edit->mBlockBegin;
vEnd = edit->mBlockEnd;
} else if (edit->mBlockBegin.line > edit->mBlockEnd.line) {
2021-05-14 23:56:43 +08:00
vEnd = edit->mBlockBegin;
vStart = edit->mBlockEnd;
} else if (edit->mBlockBegin.ch != edit->mBlockEnd.ch) {
// it is only on this line.
vStart.line = edit->mBlockBegin.line;
vEnd.line = vStart.line;
if (edit->mBlockBegin.ch < edit->mBlockEnd.ch) {
vStart.ch = edit->mBlockBegin.ch;
vEnd.ch = edit->mBlockEnd.ch;
2021-05-14 23:56:43 +08:00
} else {
vStart.ch = edit->mBlockEnd.ch;
vEnd.ch = edit->mBlockBegin.ch;
2021-05-14 23:56:43 +08:00
}
} else
bAnySelection = false;
if (edit->mInputPreeditString.length()>0) {
if (vStart.line == edit->mCaretY && vStart.ch >=edit->mCaretX) {
vStart.ch+=edit->mInputPreeditString.length();
}
if (vEnd.line == edit->mCaretY && vEnd.ch >edit->mCaretX) {
vEnd.ch+=edit->mInputPreeditString.length();
}
}
2021-05-14 23:56:43 +08:00
// If there is any visible selection so far, then test if there is an
// intersection with the area to be painted.
if (bAnySelection) {
// Don't care if the selection is not visible.
bAnySelection = (vEnd.line >= vFirstLine) && (vStart.line <= vLastLine);
2021-05-14 23:56:43 +08:00
if (bAnySelection) {
// Transform the selection from text space into screen space
vSelStart = edit->bufferToDisplayPos(vStart);
vSelEnd = edit->bufferToDisplayPos(vEnd);
if (edit->mInputPreeditString.length()
&& vStart.line == edit->mCaretY) {
QString sLine = edit->lineText().left(edit->mCaretX-1)
+ edit->mInputPreeditString
+ edit->lineText().mid(edit->mCaretX-1);
vSelStart.Column = edit->charToColumn(sLine,vStart.ch);
}
if (edit->mInputPreeditString.length()
&& vEnd.line == edit->mCaretY) {
QString sLine = edit->lineText().left(edit->mCaretX-1)
+ edit->mInputPreeditString
+ edit->lineText().mid(edit->mCaretX-1);
vSelEnd.Column = edit->charToColumn(sLine,vEnd.ch);
}
2021-05-14 23:56:43 +08:00
// In the column selection mode sort the begin and end of the selection,
// this makes the painting code simpler.
if (edit->mActiveSelectionMode == SynSelectionMode::Column && vSelStart.Column > vSelEnd.Column)
2021-05-14 23:56:43 +08:00
std::swap(vSelStart.Column, vSelEnd.Column);
}
}
}
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::setDrawingColors(bool selected)
2021-05-14 23:56:43 +08:00
{
2022-07-24 22:53:20 +08:00
if (selected) {
if (colSelFG.isValid())
painter->setPen(colSelFG);
else
painter->setPen(colFG);
if (colSelBG.isValid())
painter->setBrush(colSelBG);
else
painter->setBrush(colBG);
painter->setBackground(edit->mBackgroundColor);
2021-05-14 23:56:43 +08:00
} else {
painter->setPen(colFG);
painter->setBrush(colBG);
painter->setBackground(edit->mBackgroundColor);
2021-05-14 23:56:43 +08:00
}
}
2022-07-24 22:53:20 +08:00
int SynEditTextPainter::columnToXValue(int col)
2021-05-14 23:56:43 +08:00
{
2022-07-24 22:53:20 +08:00
return edit->textOffset() + (col - 1) * edit->mCharWidth;
2021-05-14 23:56:43 +08:00
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::paintToken(const QString &token, int tokenCols, int columnsBefore,
int first, int last, bool /*isSelection*/, const QFont& font,
const QFont& fontForNonAscii)
2021-05-14 23:56:43 +08:00
{
bool startPaint;
int nX;
2022-07-24 22:53:20 +08:00
if (last >= first && rcToken.right() > rcToken.left()) {
// qDebug()<<"Paint Token"<<Token<<ColumnsBefore<<TokenCols<<First<<Last;
2022-07-24 22:53:20 +08:00
nX = columnToXValue(first);
first -= columnsBefore;
last -= columnsBefore;
QRect rcTokenBack = rcToken;
rcTokenBack.setWidth(rcTokenBack.width()-1);
painter->fillRect(rcTokenBack,painter->brush());
2022-07-24 22:53:20 +08:00
if (first > tokenCols) {
2021-05-14 23:56:43 +08:00
} else {
2021-05-24 00:41:00 +08:00
int tokenColLen=0;
startPaint = false;
2022-07-24 22:53:20 +08:00
for (int i=0;i<token.length();i++) {
int charCols=0;
2022-07-24 22:53:20 +08:00
QString textToPaint = token[i];
if (token[i] == SynTabChar) {
charCols = edit->tabWidth() - ((columnsBefore+tokenColLen) % edit->tabWidth());
2021-05-24 00:41:00 +08:00
} else {
2022-07-24 22:53:20 +08:00
charCols = edit->charColumns(token[i]);
2021-05-24 00:41:00 +08:00
}
2022-07-24 22:53:20 +08:00
if (tokenColLen+charCols>=first) {
if (!startPaint && (tokenColLen+1!=first)) {
nX-= (first - tokenColLen - 1) * edit->mCharWidth;
2021-05-24 00:41:00 +08:00
}
startPaint = true;
}
2022-07-24 22:53:20 +08:00
if (tokenColLen+charCols > last)
break;
2021-05-24 18:11:07 +08:00
//painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent()*edit->dpiFactor() , Token[i]);
2021-05-24 21:48:03 +08:00
if (startPaint) {
bool drawed = false;
if (painter->fontInfo().fixedPitch()
&& edit->mOptions.testFlag(eoLigatureSupport)
2022-07-24 22:53:20 +08:00
&& !token[i].isSpace()
&& (token[i].unicode()<=0xFF)) {
while(i+1<token.length()) {
if (token[i+1].unicode()>0xFF || token[i+1].isSpace())
break;
i+=1;
2022-07-24 22:53:20 +08:00
charCols += edit->charColumns(token[i]);
textToPaint+=token[i];
}
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , textToPaint);
drawed = true;
}
if (!drawed) {
2022-07-24 22:53:20 +08:00
if (token[i].unicode()<=0xFF)
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , token[i]);
else {
painter->setFont(fontForNonAscii);
2022-07-24 22:53:20 +08:00
painter->drawText(nX,rcToken.bottom()-painter->fontMetrics().descent() , token[i]);
painter->setFont(font);
}
drawed = true;
}
2021-05-24 21:48:03 +08:00
nX += charCols * edit->mCharWidth;
}
2021-05-24 00:41:00 +08:00
tokenColLen += charCols;
}
2021-05-14 23:56:43 +08:00
}
rcToken.setLeft(rcToken.right());
}
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::paintEditAreas(const SynEditingAreaList &areaList)
2021-05-16 20:36:00 +08:00
{
QRect rc;
int x1,x2;
int offset;
2021-05-26 18:32:17 +08:00
//painter->setClipRect(rcLine);
2021-05-16 20:36:00 +08:00
rc=rcLine;
rc.setBottom(rc.bottom()-1);
setDrawingColors(false);
for (const PSynEditingArea& p:areaList) {
2021-05-16 20:36:00 +08:00
if (p->beginX > LastCol)
continue;
if (p->endX < FirstCol)
continue;
if (p->beginX < FirstCol)
x1 = FirstCol;
else
x1 = p->beginX;
if (p->endX > LastCol)
x2 = LastCol;
else
x2 = p->endX;
2022-07-24 22:53:20 +08:00
rc.setLeft(columnToXValue(x1));
rc.setRight(columnToXValue(x2));
2021-05-16 20:36:00 +08:00
painter->setPen(p->color);
painter->setBrush(Qt::NoBrush);
switch(p->type) {
case SynEditingAreaType::eatRectangleBorder:
painter->drawRect(rc);
break;
case SynEditingAreaType::eatUnderLine:
painter->drawLine(rc.left(),rc.bottom(),rc.right(),rc.bottom());
break;
case SynEditingAreaType::eatWaveUnderLine:
offset=3;
int lastX=rc.left();
int lastY=rc.bottom()-offset;
int t = rc.left();
while (t<rc.right()) {
2021-05-16 20:36:00 +08:00
t+=3;
if (t>rc.right())
t = rc.right();
offset = 3 - offset;
painter->drawLine(lastX,lastY,t,rc.bottom()-offset);
lastX = t;
lastY = rc.bottom()-offset;
}
}
}
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::paintHighlightToken(bool bFillToEOL)
2021-05-16 20:36:00 +08:00
{
bool bComplexToken;
int nC1, nC2, nC1Sel, nC2Sel;
bool bU1, bSel, bU2;
int nX1, nX2;
// Compute some helper variables.
nC1 = std::max(FirstCol, TokenAccu.ColumnsBefore + 1);
nC2 = std::min(LastCol, TokenAccu.ColumnsBefore + TokenAccu.Columns + 1);
if (bComplexLine) {
bU1 = (nC1 < nLineSelStart);
bSel = (nC1 < nLineSelEnd) && (nC2 >= nLineSelStart);
bU2 = (nC2 >= nLineSelEnd);
2021-08-16 23:17:48 +08:00
bComplexToken = bSel && (bU1 || bU2);
2021-05-16 20:36:00 +08:00
} else {
bSel = bLineSelected;
bComplexToken = false;
bU1 = false; // to shut up compiler warning.
bU2 = false; // to shut up compiler warning.
2021-05-16 20:36:00 +08:00
}
// Any token chars accumulated?
if (TokenAccu.Columns > 0) {
// Initialize the colors and the font style.
colBG = TokenAccu.BG;
colFG = TokenAccu.FG;
if (bSpecialLine) {
if (colSpFG.isValid())
colFG = colSpFG;
if (colSpBG.isValid())
colBG = colSpBG;
2021-05-16 20:36:00 +08:00
}
// if (bSpecialLine && edit->mOptions.testFlag(eoSpecialLineDefaultFg))
// colFG = TokenAccu.FG;
2021-05-16 20:36:00 +08:00
QFont font = edit->font();
2021-05-26 18:32:17 +08:00
font.setBold(TokenAccu.Style & SynFontStyle::fsBold);
font.setItalic(TokenAccu.Style & SynFontStyle::fsItalic);
font.setStrikeOut(TokenAccu.Style & SynFontStyle::fsStrikeOut);
font.setUnderline(TokenAccu.Style & SynFontStyle::fsUnderline);
2021-05-16 20:36:00 +08:00
painter->setFont(font);
QFont nonAsciiFont = edit->fontForNonAscii();
nonAsciiFont.setBold(TokenAccu.Style & SynFontStyle::fsBold);
nonAsciiFont.setItalic(TokenAccu.Style & SynFontStyle::fsItalic);
nonAsciiFont.setStrikeOut(TokenAccu.Style & SynFontStyle::fsStrikeOut);
nonAsciiFont.setUnderline(TokenAccu.Style & SynFontStyle::fsUnderline);
2021-05-16 20:36:00 +08:00
// Paint the chars
if (bComplexToken) {
// first unselected part of the token
if (bU1) {
setDrawingColors(false);
2022-07-24 22:53:20 +08:00
rcToken.setRight(columnToXValue(nLineSelStart));
paintToken(TokenAccu.s,TokenAccu.Columns,TokenAccu.ColumnsBefore,nC1,nLineSelStart,false,font,nonAsciiFont);
2021-05-16 20:36:00 +08:00
}
// selected part of the token
setDrawingColors(true);
nC1Sel = std::max(nLineSelStart, nC1);
nC2Sel = std::min(nLineSelEnd, nC2);
2022-07-24 22:53:20 +08:00
rcToken.setRight(columnToXValue(nC2Sel));
paintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore, nC1Sel, nC2Sel,true,font,nonAsciiFont);
2021-05-16 20:36:00 +08:00
// second unselected part of the token
if (bU2) {
setDrawingColors(false);
2022-07-24 22:53:20 +08:00
rcToken.setRight(columnToXValue(nC2));
paintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore,nLineSelEnd, nC2,false,font,nonAsciiFont);
2021-05-16 20:36:00 +08:00
}
} else {
setDrawingColors(bSel);
2022-07-24 22:53:20 +08:00
rcToken.setRight(columnToXValue(nC2));
paintToken(TokenAccu.s, TokenAccu.Columns, TokenAccu.ColumnsBefore, nC1, nC2,bSel,font,nonAsciiFont);
2021-05-16 20:36:00 +08:00
}
}
// Fill the background to the end of this line if necessary.
if (bFillToEOL && rcToken.left() < rcLine.right()) {
if (bSpecialLine && colSpBG.isValid())
colBG = colSpBG;
else
2021-05-16 20:36:00 +08:00
colBG = colEditorBG();
if (bComplexLine) {
2022-07-24 22:53:20 +08:00
nX1 = columnToXValue(nLineSelStart);
nX2 = columnToXValue(nLineSelEnd);
2021-05-16 20:36:00 +08:00
if (rcToken.left() < nX1) {
setDrawingColors(false);
rcToken.setRight(nX1);
// if (TokenAccu.Len != 0 && TokenAccu.Style != SynFontStyle::fsNone)
// AdjustEndRect();
2021-05-27 20:33:25 +08:00
painter->fillRect(rcToken,painter->brush());
2021-05-16 20:36:00 +08:00
rcToken.setLeft(nX1);
}
if (rcToken.left() < nX2) {
setDrawingColors(true);
rcToken.setRight(nX2);
2021-05-27 20:33:25 +08:00
painter->fillRect(rcToken,painter->brush());
2021-05-16 20:36:00 +08:00
rcToken.setLeft(nX2);
}
if (rcToken.left() < rcLine.right()) {
setDrawingColors(false);
rcToken.setRight(rcLine.right());
2021-05-27 20:33:25 +08:00
painter->fillRect(rcToken,painter->brush());
2021-05-16 20:36:00 +08:00
}
} else {
setDrawingColors(bLineSelected);
rcToken.setRight(rcLine.right());
// if (TokenAccu.Len != 0 && TokenAccu.Style != SynFontStyle::fsNone)
// AdjustEndRect();
2021-05-27 20:33:25 +08:00
painter->fillRect(rcToken,painter->brush());
2021-05-16 20:36:00 +08:00
}
}
}
2022-07-24 22:53:20 +08:00
bool SynEditTextPainter::tokenIsSpaces(bool &bSpacesTest, const QString& token, bool& bIsSpaces)
2021-05-16 20:36:00 +08:00
{
if (!bSpacesTest) {
bSpacesTest = true;
2022-07-24 22:53:20 +08:00
for (QChar ch:token) {
2021-05-16 20:36:00 +08:00
//todo: should include tabs?
if (ch!= ' ') {
bIsSpaces = false;
return bIsSpaces;
}
}
bIsSpaces = true;
}
return bIsSpaces;
}
// Store the token chars with the attributes in the TokenAccu
// record. This will paint any chars already stored if there is
// a (visible) change in the attributes.
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::addHighlightToken(const QString &Token, int columnsBefore,
int tokenColumns, int cLine, PSynHighlighterAttribute p_Attri)
2021-05-16 20:36:00 +08:00
{
bool bCanAppend;
2022-07-24 22:53:20 +08:00
QColor foreground, background;
SynFontStyles style;
2021-05-16 20:36:00 +08:00
bool bSpacesTest,bIsSpaces;
if (p_Attri) {
2022-07-24 22:53:20 +08:00
foreground = p_Attri->foreground();
background = p_Attri->background();
style = p_Attri->styles();
2021-05-16 20:36:00 +08:00
} else {
2022-07-24 22:53:20 +08:00
foreground = colFG;
background = colBG;
style = getFontStyles(edit->font());
2021-05-16 20:36:00 +08:00
}
// if (!Background.isValid() || (edit->mActiveLineColor.isValid() && bCurrentLine)) {
// Background = colEditorBG();
// }
2022-07-24 22:53:20 +08:00
if (!background.isValid() ) {
background = colEditorBG();
2021-05-16 20:36:00 +08:00
}
2022-07-24 22:53:20 +08:00
if (!foreground.isValid()) {
foreground = edit->mForegroundColor;
2021-05-16 20:36:00 +08:00
}
edit->onPreparePaintHighlightToken(cLine,edit->mHighlighter->getTokenPos()+1,
2022-07-24 22:53:20 +08:00
Token,p_Attri,style,foreground,background);
2021-05-16 20:36:00 +08:00
// Do we have to paint the old chars first, or can we just append?
bCanAppend = false;
bSpacesTest = false;
if (TokenAccu.Columns > 0) {
// font style must be the same or token is only spaces
2022-07-24 22:53:20 +08:00
if (TokenAccu.Style == style || ( (style & SynFontStyle::fsUnderline) == (TokenAccu.Style & fsUnderline)
&& tokenIsSpaces(bSpacesTest,Token,bIsSpaces)) ) {
if (
2021-05-16 20:36:00 +08:00
// background color must be the same and
2022-07-24 22:53:20 +08:00
((TokenAccu.BG == background) &&
2021-05-16 20:36:00 +08:00
// foreground color must be the same or token is only spaces
2022-07-24 22:53:20 +08:00
((TokenAccu.FG == foreground) || (tokenIsSpaces(bSpacesTest,Token,bIsSpaces) && !edit->mOptions.testFlag(eoShowSpecialChars))))) {
2021-05-16 20:36:00 +08:00
bCanAppend = true;
}
}
// If we can't append it, then we have to paint the old token chars first.
if (!bCanAppend)
2022-07-24 22:53:20 +08:00
paintHighlightToken(false);
2021-05-16 20:36:00 +08:00
}
// Don't use AppendStr because it's more expensive.
if (bCanAppend) {
TokenAccu.s.append(Token);
2022-07-24 22:53:20 +08:00
TokenAccu.Columns+=tokenColumns;
2021-05-16 20:36:00 +08:00
} else {
2022-07-24 22:53:20 +08:00
TokenAccu.Columns = tokenColumns;
2021-05-26 18:32:17 +08:00
TokenAccu.s = Token;
2022-07-24 22:53:20 +08:00
TokenAccu.ColumnsBefore = columnsBefore;
TokenAccu.FG = foreground;
TokenAccu.BG = background;
TokenAccu.Style = style;
2021-05-16 20:36:00 +08:00
}
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::paintFoldAttributes()
2021-05-16 20:36:00 +08:00
{
2021-05-26 18:32:17 +08:00
int TabSteps, LineIndent, LastNonBlank, X, Y, cRow, vLine;
2021-05-16 20:36:00 +08:00
// Paint indent guides. Use folds to determine indent value of these
// Use a separate loop so we can use a custom pen
// Paint indent guides using custom pen
if (edit->mCodeFolding.indentGuides || edit->mCodeFolding.fillIndents) {
QColor paintColor;
if (edit->mCodeFolding.indentGuidesColor.isValid()) {
paintColor = edit->mCodeFolding.indentGuidesColor;
} else {
paintColor = edit->palette().color(QPalette::Text);
}
QColor gradientStart = paintColor;
QColor gradientEnd = paintColor;
2021-05-16 20:36:00 +08:00
QPen oldPen = painter->pen();
2021-05-16 20:36:00 +08:00
// Now loop through all the lines. The indices are valid for Lines.
for (cRow = aFirstRow; cRow<=aLastRow;cRow++) {
vLine = edit->rowToLine(cRow);
if (vLine > edit->mDocument->count() && edit->mDocument->count() > 0)
2021-05-16 20:36:00 +08:00
break;
// Set vertical coord
Y = (cRow - edit->mTopLine) * edit->mTextHeight; // limit inside clip rect
2021-05-16 20:36:00 +08:00
if (edit->mTextHeight % 2 == 1 && vLine % 2 == 0) {
Y++;
}
// Get next nonblank line
LastNonBlank = vLine - 1;
while (LastNonBlank + 1 < edit->mDocument->count() && edit->mDocument->getString(LastNonBlank).isEmpty())
2021-05-16 20:36:00 +08:00
LastNonBlank++;
if (LastNonBlank>=edit->document()->count())
continue;
LineIndent = edit->getLineIndent(edit->mDocument->getString(LastNonBlank));
int braceLevel = edit->mDocument->ranges(LastNonBlank).braceLevel;
int indentLevel = braceLevel ;
if (edit->tabWidth()>0)
indentLevel = LineIndent / edit->tabWidth();
2021-05-16 20:36:00 +08:00
// Step horizontal coord
//TabSteps = edit->mTabWidth;
TabSteps = 0;
indentLevel = 0;
2021-05-16 20:36:00 +08:00
while (TabSteps < LineIndent) {
2021-05-24 21:48:03 +08:00
X = TabSteps * edit->mCharWidth + edit->textOffset() - 2;
TabSteps+=edit->tabWidth();
indentLevel++ ;
if (edit->mHighlighter) {
if (edit->mCodeFolding.indentGuides) {
PSynHighlighterAttribute attr = edit->mHighlighter->symbolAttribute();
2022-07-24 22:53:20 +08:00
getBraceColorAttr(indentLevel,attr);
paintColor = attr->foreground();
}
if (edit->mCodeFolding.fillIndents) {
PSynHighlighterAttribute attr = edit->mHighlighter->symbolAttribute();
2022-07-24 22:53:20 +08:00
getBraceColorAttr(indentLevel,attr);
gradientStart=attr->foreground();
attr = edit->mHighlighter->symbolAttribute();
2022-07-24 22:53:20 +08:00
getBraceColorAttr(indentLevel+1,attr);
gradientStart=attr->foreground();
}
}
if (edit->mCodeFolding.fillIndents) {
int X1;
if (TabSteps>LineIndent)
X1 = LineIndent * edit->mCharWidth + edit->textOffset() - 2;
else
X1 = TabSteps * edit->mCharWidth + edit->textOffset() - 2;
gradientStart.setAlpha(20);
gradientEnd.setAlpha(10);
QLinearGradient gradient(X,Y,X1,Y);
gradient.setColorAt(1,gradientStart);
gradient.setColorAt(0,gradientEnd);
painter->fillRect(X,Y,(X1-X),edit->mTextHeight,gradient);
}
2021-05-16 20:36:00 +08:00
// Move to top of vertical line
if (edit->mCodeFolding.indentGuides) {
QPen dottedPen(Qt::PenStyle::DashLine);
dottedPen.setColor(paintColor);
painter->setPen(dottedPen);
painter->drawLine(X,Y,X,Y+edit->mTextHeight);
}
2021-05-16 20:36:00 +08:00
}
}
painter->setPen(oldPen);
}
if (!edit->mUseCodeFolding)
2021-05-21 23:33:53 +08:00
return;
2021-05-16 20:36:00 +08:00
// Paint collapsed lines using changed pen
if (edit->mCodeFolding.showCollapsedLine) {
painter->setPen(edit->mCodeFolding.collapsedLineColor);
for (int i=0; i< edit->mAllFoldRanges.count();i++) {
PSynEditFoldRange range = edit->mAllFoldRanges[i];
if (range->collapsed && !range->parentCollapsed() &&
(range->fromLine <= vLastLine) && (range->fromLine >= vFirstLine) ) {
// Get starting and end points
Y = (edit->lineToRow(range->fromLine) - edit->mTopLine + 1) * edit->mTextHeight - 1;
painter->drawLine(AClip.left(),Y, AClip.right(),Y);
}
}
}
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::getBraceColorAttr(int level, PSynHighlighterAttribute &attr)
2021-05-16 20:36:00 +08:00
{
if (!edit->mOptions.testFlag(SynEditorOption::eoShowRainbowColor))
return;
if (attr != edit->mHighlighter->symbolAttribute())
return;
PSynHighlighterAttribute oldAttr = attr;
2021-05-16 20:36:00 +08:00
switch(level % 4) {
case 0:
attr = edit->mRainbowAttr0;
2021-05-26 18:32:17 +08:00
break;
2021-05-16 20:36:00 +08:00
case 1:
attr = edit->mRainbowAttr1;
2021-05-26 18:32:17 +08:00
break;
2021-05-16 20:36:00 +08:00
case 2:
attr = edit->mRainbowAttr2;
2021-05-26 18:32:17 +08:00
break;
2021-05-16 20:36:00 +08:00
case 3:
attr = edit->mRainbowAttr3;
2021-05-26 18:32:17 +08:00
break;
2021-05-16 20:36:00 +08:00
}
if (!attr)
attr = oldAttr;
2021-05-16 20:36:00 +08:00
}
2022-07-24 22:53:20 +08:00
void SynEditTextPainter::paintLines()
2021-05-16 20:36:00 +08:00
{
int cRow; // row index for the loop
int vLine;
QString sLine; // the current line
QString sToken; // highlighter token info
2021-05-26 18:32:17 +08:00
int nTokenColumnsBefore, nTokenColumnLen;
2021-05-16 20:36:00 +08:00
PSynHighlighterAttribute attr;
int vFirstChar;
int vLastChar;
2021-05-21 23:33:53 +08:00
SynEditingAreaList areaList;
2021-05-16 20:36:00 +08:00
PSynEditFoldRange foldRange;
PSynHighlighterAttribute preeditAttr;
2021-05-24 00:41:00 +08:00
int nFold;
2021-05-16 20:36:00 +08:00
QString sFold;
// Initialize rcLine for drawing. Note that Top and Bottom are updated
// inside the loop. Get only the starting point for this.
rcLine = AClip;
rcLine.setBottom((aFirstRow - edit->mTopLine) * edit->mTextHeight);
TokenAccu.Columns = 0;
TokenAccu.ColumnsBefore = 0;
// Now loop through all the lines. The indices are valid for Lines.
BufferCoord selectionBegin = edit->blockBegin();
BufferCoord selectionEnd= edit->blockEnd();
2021-05-18 15:49:58 +08:00
for (cRow = aFirstRow; cRow<=aLastRow; cRow++) {
vLine = edit->rowToLine(cRow);
if (vLine > edit->mDocument->count() && edit->mDocument->count() != 0)
2021-05-18 15:49:58 +08:00
break;
// Get the line.
sLine = edit->mDocument->getString(vLine - 1);
2021-05-18 15:49:58 +08:00
// determine whether will be painted with ActiveLineColor
if (edit->mActiveSelectionMode == SynSelectionMode::Column) {
bCurrentLine = (vLine >= selectionBegin.line && vLine <= selectionEnd.line);
} else {
bCurrentLine = (edit->mCaretY == vLine);
}
if (bCurrentLine && !edit->mInputPreeditString.isEmpty()) {
int col = edit->charToColumn(edit->mCaretY,edit->mCaretX);
int ch = edit->columnToChar(vLine,col);
sLine = sLine.left(ch-1) + edit->mInputPreeditString
+ sLine.mid(ch-1);
}
2021-05-18 15:49:58 +08:00
// Initialize the text and background colors, maybe the line should
// use special values for them.
colFG = edit->mForegroundColor;
2021-05-18 15:49:58 +08:00
colBG = colEditorBG();
colSpFG = QColor();
colSpBG = QColor();
bSpecialLine = edit->onGetSpecialLineColors(vLine, colSpFG, colSpBG);
2021-05-24 18:11:07 +08:00
colSelFG = edit->mSelectedForeground;
colSelBG = edit->mSelectedBackground;
2021-05-24 00:41:00 +08:00
edit->onGetEditingAreas(vLine, areaList);
2021-05-18 15:49:58 +08:00
// Removed word wrap support
vFirstChar = FirstCol;
vLastChar = LastCol;
// Get the information about the line selection. Three different parts
// are possible (unselected before, selected, unselected after), only
// unselected or only selected means bComplexLine will be FALSE. Start
// with no selection, compute based on the visible columns.
bComplexLine = false;
nLineSelStart = 0;
nLineSelEnd = 0;
// Does the selection intersect the visible area?
if (bAnySelection && (cRow >= vSelStart.Row) && (cRow <= vSelEnd.Row)) {
// Default to a fully selected line. This is correct for the smLine
// selection mode and a good start for the smNormal mode.
nLineSelStart = FirstCol;
nLineSelEnd = LastCol + 1;
if ((edit->mActiveSelectionMode == SynSelectionMode::Column) ||
((edit->mActiveSelectionMode == SynSelectionMode::Normal) && (cRow == vSelStart.Row)) ) {
int ch = edit->columnToChar(vLine,vSelStart.Column);
ch = edit->charToColumn(vLine,ch);
if (ch > LastCol) {
2021-05-18 15:49:58 +08:00
nLineSelStart = 0;
nLineSelEnd = 0;
} else if (ch > FirstCol) {
nLineSelStart = ch;
2021-05-18 15:49:58 +08:00
bComplexLine = true;
}
}
if ( (edit->mActiveSelectionMode == SynSelectionMode::Column) ||
((edit->mActiveSelectionMode == SynSelectionMode::Normal) && (cRow == vSelEnd.Row)) ) {
int ch = edit->columnToChar(vLine,vSelEnd.Column);
int col = edit->charToColumn(vLine,ch);
if (col<vSelEnd.Column)
col = edit->charToColumn(vLine,ch+1);
if (col < FirstCol) {
2021-05-18 15:49:58 +08:00
nLineSelStart = 0;
nLineSelEnd = 0;
} else if (col < LastCol) {
nLineSelEnd = col;
2021-05-18 15:49:58 +08:00
bComplexLine = true;
}
}
} //endif bAnySelection
// Update the rcLine rect to this line.
// rcLine.setTop(rcLine.bottom());
// rcLine.setBottom(rcLine.bottom()+edit->mTextHeight);
rcLine.setTop((cRow - edit->mTopLine) * edit->mTextHeight);
rcLine.setHeight(edit->mTextHeight);
2021-05-18 15:49:58 +08:00
bLineSelected = (!bComplexLine) && (nLineSelStart > 0);
rcToken = rcLine;
if (!edit->mHighlighter || !edit->mHighlighter->enabled()) {
sToken = sLine;
if (bCurrentLine) {
nTokenColumnLen = edit->stringColumns(sLine,0);
} else {
nTokenColumnLen = edit->mDocument->lineColumns(vLine-1);
}
2021-05-24 18:11:07 +08:00
if (edit->mOptions.testFlag(eoShowSpecialChars) && (!bLineSelected) && (!bSpecialLine) && (nTokenColumnLen < vLastChar)) {
2021-05-18 15:49:58 +08:00
sToken = sToken + SynLineBreakGlyph;
2021-05-21 23:33:53 +08:00
nTokenColumnLen += edit->charColumns(SynLineBreakGlyph);
2021-05-18 15:49:58 +08:00
}
if (bComplexLine) {
setDrawingColors(true);
2022-07-24 22:53:20 +08:00
rcToken.setLeft(std::max(rcLine.left(), columnToXValue(nLineSelStart)));
rcToken.setRight(std::min(rcLine.right(), columnToXValue(nLineSelEnd)));
paintToken(sToken, nTokenColumnLen, 0, nLineSelStart, nLineSelEnd,false,edit->font(),edit->fontForNonAscii());
2021-05-18 15:49:58 +08:00
setDrawingColors(false);
2022-07-24 22:53:20 +08:00
rcToken.setLeft(std::max(rcLine.left(), columnToXValue(FirstCol)));
rcToken.setRight(std::min(rcLine.right(), columnToXValue(nLineSelStart)));
paintToken(sToken, nTokenColumnLen, 0, FirstCol, nLineSelStart,false,edit->font(),edit->fontForNonAscii());
rcToken.setLeft(std::max(rcLine.left(), columnToXValue(nLineSelEnd)));
rcToken.setRight(std::min(rcLine.right(), columnToXValue(LastCol)));
paintToken(sToken, nTokenColumnLen, 0, nLineSelEnd, LastCol,true,edit->font(),edit->fontForNonAscii());
2021-05-18 15:49:58 +08:00
} else {
setDrawingColors(bLineSelected);
2022-07-24 22:53:20 +08:00
paintToken(sToken, nTokenColumnLen, 0, FirstCol, LastCol,bLineSelected,edit->font(),edit->fontForNonAscii());
2021-05-18 15:49:58 +08:00
}
//Paint editingAreaBorders
if (bCurrentLine && edit->mInputPreeditString.length()>0) {
PSynEditingArea area = std::make_shared<SynEditingArea>();
int col = edit->charToColumn(edit->mCaretY,edit->mCaretX);
int ch = edit->columnToChar(vLine,col);
area->beginX = edit->charToColumn(sLine,ch);
area->endX = edit->charToColumn(sLine,ch + edit->mInputPreeditString.length());
area->type = SynEditingAreaType::eatUnderLine;
area->color = colFG;
areaList.append(area);
2022-07-24 22:53:20 +08:00
paintEditAreas(areaList);
}
2021-05-18 15:49:58 +08:00
} else {
// Initialize highlighter with line text and range info. It is
// necessary because we probably did not scan to the end of the last
// line - the internal highlighter range might be wrong.
if (vLine == 1) {
edit->mHighlighter->resetState();
} else {
edit->mHighlighter->setState(
edit->mDocument->ranges(vLine-2));
2021-05-16 20:36:00 +08:00
}
2021-05-26 18:32:17 +08:00
edit->mHighlighter->setLine(sLine, vLine - 1);
2021-05-18 15:49:58 +08:00
// Try to concatenate as many tokens as possible to minimize the count
// of ExtTextOut calls necessary. This depends on the selection state
// or the line having special colors. For spaces the foreground color
// is ignored as well.
TokenAccu.Columns = 0;
2021-05-26 18:32:17 +08:00
nTokenColumnsBefore = 0;
2021-05-18 15:49:58 +08:00
// Test first whether anything of this token is visible.
2021-05-26 18:32:17 +08:00
while (!edit->mHighlighter->eol()) {
2021-05-18 15:49:58 +08:00
sToken = edit->mHighlighter->getToken();
// Work-around buggy highlighters which return empty tokens.
if (sToken.isEmpty()) {
edit->mHighlighter->next();
if (edit->mHighlighter->eol())
break;
sToken = edit->mHighlighter->getToken();
// Maybe should also test whether GetTokenPos changed...
2021-06-20 09:27:37 +08:00
if (sToken.isEmpty()) {
qDebug()<<SynEdit::tr("The highlighter seems to be in an infinite loop");
2021-05-18 15:49:58 +08:00
throw BaseError(SynEdit::tr("The highlighter seems to be in an infinite loop"));
2021-06-20 09:27:37 +08:00
}
2021-05-18 15:49:58 +08:00
}
//nTokenColumnsBefore = edit->charToColumn(sLine,edit->mHighlighter->getTokenPos()+1)-1;
nTokenColumnLen = edit->stringColumns(sToken, nTokenColumnsBefore);
2021-05-26 18:32:17 +08:00
if (nTokenColumnsBefore + nTokenColumnLen >= vFirstChar) {
if (nTokenColumnsBefore + nTokenColumnLen >= vLastChar) {
if (nTokenColumnsBefore >= vLastChar)
2021-05-18 15:49:58 +08:00
break; //*** BREAK ***
nTokenColumnLen = vLastChar - nTokenColumnsBefore;
2021-05-18 15:49:58 +08:00
}
// It's at least partially visible. Get the token attributes now.
attr = edit->mHighlighter->getTokenAttribute();
if (sToken == "["
|| sToken == "("
|| sToken == "{"
) {
SynRangeState rangeState = edit->mHighlighter->getRangeState();
2022-07-24 22:53:20 +08:00
getBraceColorAttr(rangeState.bracketLevel
+rangeState.braceLevel
+rangeState.parenthesisLevel
,attr);
} else if (sToken == "]"
|| sToken == ")"
|| sToken == "}"
){
SynRangeState rangeState = edit->mHighlighter->getRangeState();
2022-07-24 22:53:20 +08:00
getBraceColorAttr(rangeState.bracketLevel
+rangeState.braceLevel
+rangeState.parenthesisLevel+1,
attr);
2021-05-18 15:49:58 +08:00
}
if (bCurrentLine && edit->mInputPreeditString.length()>0) {
int startPos = edit->mHighlighter->getTokenPos()+1;
int endPos = edit->mHighlighter->getTokenPos() + sToken.length();
2021-10-29 12:29:21 +08:00
//qDebug()<<startPos<<":"<<endPos<<" - "+sToken+" - "<<edit->mCaretX<<":"<<edit->mCaretX+edit->mInputPreeditString.length();
if (!(endPos < edit->mCaretX
|| startPos >= edit->mCaretX+edit->mInputPreeditString.length())) {
if (!preeditAttr) {
preeditAttr = attr;
} else {
attr = preeditAttr;
}
}
}
2022-07-24 22:53:20 +08:00
addHighlightToken(sToken, nTokenColumnsBefore - (vFirstChar - FirstCol),
2021-05-21 23:33:53 +08:00
nTokenColumnLen, vLine,attr);
2021-05-18 15:49:58 +08:00
}
nTokenColumnsBefore+=nTokenColumnLen;
2021-05-18 15:49:58 +08:00
// Let the highlighter scan the next token.
edit->mHighlighter->next();
}
// Don't assume HL.GetTokenPos is valid after HL.GetEOL == True.
//nTokenColumnsBefore += edit->stringColumns(sToken,nTokenColumnsBefore);
2021-05-26 18:32:17 +08:00
if (edit->mHighlighter->eol() && (nTokenColumnsBefore < vLastChar)) {
int lineColumns = edit->mDocument->lineColumns(vLine-1);
2021-05-18 15:49:58 +08:00
// Draw text that couldn't be parsed by the highlighter, if any.
2021-05-26 18:32:17 +08:00
if (nTokenColumnsBefore < lineColumns) {
if (nTokenColumnsBefore + 1 < vFirstChar)
nTokenColumnsBefore = vFirstChar - 1;
nTokenColumnLen = std::min(lineColumns, vLastChar) - (nTokenColumnsBefore + 1);
2021-05-21 23:33:53 +08:00
if (nTokenColumnLen > 0) {
2021-05-26 18:32:17 +08:00
sToken = edit->substringByColumns(sLine,nTokenColumnsBefore+1,nTokenColumnLen);
2022-07-24 22:53:20 +08:00
addHighlightToken(sToken, nTokenColumnsBefore - (vFirstChar - FirstCol),
2021-05-21 23:33:53 +08:00
nTokenColumnLen, vLine, PSynHighlighterAttribute());
2021-05-18 15:49:58 +08:00
}
}
2021-05-24 18:11:07 +08:00
// Draw LineBreak glyph.
if (edit->mOptions.testFlag(eoShowSpecialChars) && (!bLineSelected) &&
(!bSpecialLine) && (edit->mDocument->lineColumns(vLine-1) < vLastChar)) {
2022-07-24 22:53:20 +08:00
addHighlightToken(SynLineBreakGlyph,
edit->mDocument->lineColumns(vLine-1) - (vFirstChar - FirstCol),
2021-05-24 18:11:07 +08:00
edit->charColumns(SynLineBreakGlyph),vLine, edit->mHighlighter->whitespaceAttribute());
}
2021-05-18 15:49:58 +08:00
}
// Paint folding
foldRange = edit->foldStartAtLine(vLine);
if ((foldRange) && foldRange->collapsed) {
2022-06-28 12:22:40 +08:00
sFold = edit->highlighter()->foldString();
nFold = edit->stringColumns(sFold,edit->mDocument->lineColumns(vLine-1));
2021-05-21 23:33:53 +08:00
attr = edit->mHighlighter->symbolAttribute();
2022-07-24 22:53:20 +08:00
getBraceColorAttr(edit->mHighlighter->getRangeState().braceLevel,attr);
addHighlightToken(sFold,edit->mDocument->lineColumns(vLine-1) - (vFirstChar - FirstCol)
2021-05-21 23:33:53 +08:00
, nFold, vLine, attr);
2021-05-18 15:49:58 +08:00
}
// Draw anything that's left in the TokenAccu record. Fill to the end
// of the invalid area with the correct colors.
2022-07-24 22:53:20 +08:00
paintHighlightToken(true);
2021-05-18 15:49:58 +08:00
//Paint editingAreaBorders
foreach (const PSynEditingArea& area, areaList) {
if (bCurrentLine && edit->mInputPreeditString.length()>0) {
if (area->beginX > edit->mCaretX) {
area->beginX+=edit->mInputPreeditString.length();
}
if (area->endX > edit->mCaretX) {
area->endX+=edit->mInputPreeditString.length();
}
}
area->beginX = edit->charToColumn(sLine, area->beginX);
area->endX = edit->charToColumn(sLine,area->endX);
}
if (bCurrentLine && edit->mInputPreeditString.length()>0) {
PSynEditingArea area = std::make_shared<SynEditingArea>();
int col = edit->charToColumn(edit->mCaretY,edit->mCaretX);
int ch = edit->columnToChar(vLine,col);
area->beginX = edit->charToColumn(sLine,ch);
area->endX = edit->charToColumn(sLine,ch + edit->mInputPreeditString.length());
area->type = SynEditingAreaType::eatUnderLine;
if (preeditAttr) {
area->color = preeditAttr->foreground();
} else {
area->color = colFG;
}
areaList.append(area);
}
2022-07-24 22:53:20 +08:00
paintEditAreas(areaList);
2021-05-18 15:49:58 +08:00
}
// Now paint the right edge if necessary. We do it line by line to reduce
// the flicker. Should not cost very much anyway, compared to the many
// calls to ExtTextOut.
if (bDoRightEdge) {
painter->setPen(edit->mRightEdgeColor);
2021-05-21 23:33:53 +08:00
painter->drawLine(nRightEdge, rcLine.top(),nRightEdge,rcLine.bottom()+1);
2021-05-18 15:49:58 +08:00
}
bCurrentLine = false;
}
2021-05-16 20:36:00 +08:00
}