📄 scintillaeditview.cpp
字号:
{
char title[10];
char nb_str[4];
strcat(strcpy(title, UNTITLED_STR), _itoa(nb, nb_str, 10));
// get the doc pointer attached (by default) on the view Scintilla
Document doc = execute(SCI_GETDOCPOINTER, 0, 0);
// create the entry for our list
_buffers.push_back(Buffer(doc, title));
// set current index to 0
_currentIndex = 0;
return _buffers[_currentIndex]._fullPathName;
}
int ScintillaEditView::findDocIndexByName(const char *fn) const
{
int index = -1;
for (int i = 0 ; i < int(_buffers.size()) ; i++)
{
if (!strcmp(_buffers[i]._fullPathName, fn))
{
index = i;
break;
}
}
return index;
}
//! \brief this method activates the doc and the corresponding sub tab
//! \brief return the index of previeus current doc
char * ScintillaEditView::activateDocAt(int index)
{
// before activating another document, we get the current position
// from the Scintilla view then save it to the current document
saveCurrentPos();
Position & prevDocPos = _buffers[_currentIndex]._pos;
// increase current doc ref count to 2
execute(SCI_ADDREFDOCUMENT, 0, _buffers[_currentIndex]._doc);
// change the doc, this operation will decrease
// the ref count of old current doc to 1
execute(SCI_SETDOCPOINTER, 0, _buffers[index]._doc);
// reset all for another doc
//execute(SCI_CLEARDOCUMENTSTYLE);
//bool isDocTypeDiff = (_buffers[_currentIndex]._lang != _buffers[index]._lang);
_currentIndex = index;
//if (isDocTypeDiff)
defineDocType(_buffers[_currentIndex]._lang);
restoreCurrentPos(prevDocPos);
execute(SCI_SETEOLMODE, _buffers[_currentIndex]._format);
//execute(SCI_SETEOLMODE, _buffers[_currentIndex]._format);
return _buffers[_currentIndex]._fullPathName;
}
// this method creates a new doc ,and adds it into
// the end of the doc list and a last sub tab, then activate it
// it returns the name of this created doc (that's the current doc also)
char * ScintillaEditView::createNewDoc(const char *fn)
{
Document newDoc = execute(SCI_CREATEDOCUMENT);
_buffers.push_back(Buffer(newDoc, fn));
_buffers[_buffers.size()-1].checkIfReadOnlyFile();
return activateDocAt(int(_buffers.size())-1);
}
char * ScintillaEditView::createNewDoc(int nbNew)
{
char title[10];
char nb[4];
strcat(strcpy(title, UNTITLED_STR), _itoa(nbNew, nb, 10));
return createNewDoc(title);
}
// return the index to close then (argument) the index to activate
int ScintillaEditView::closeCurrentDoc(int & i2Activate)
{
int oldCurrent = _currentIndex;
Position & prevDocPos = _buffers[_currentIndex]._pos;
// if the file 2 delete is the last one
if (_currentIndex == int(_buffers.size()) - 1)
{
// if current index is 0, ie. the current is the only one
if (!_currentIndex)
{
_currentIndex = 0;
}
// the current is NOT the only one and it is the last one,
// we set it to the index which precedes it
else
_currentIndex -= 1;
}
// else the next current index will be the same,
// we do nothing
// get the iterator and calculate its position with the old current index value
buf_vec_t::iterator posIt = _buffers.begin() + oldCurrent;
// erase the position given document from our list
_buffers.erase(posIt);
// set another document, so the ref count of old active document owned
// by Scintilla view will be decreased to 0 by SCI_SETDOCPOINTER message
execute(SCI_SETDOCPOINTER, 0, _buffers[_currentIndex]._doc);
defineDocType(_buffers[_currentIndex]._lang);
restoreCurrentPos(prevDocPos);
i2Activate = _currentIndex;
return oldCurrent;
}
void ScintillaEditView::closeDocAt(int i2Close)
{
execute(SCI_RELEASEDOCUMENT, 0, _buffers[i2Close]._doc);
// get the iterator and calculate its position with the old current index value
buf_vec_t::iterator posIt = _buffers.begin() + i2Close;
// erase the position given document from our list
_buffers.erase(posIt);
_currentIndex -= (i2Close < _currentIndex)?1:0;
}
void ScintillaEditView::removeAllUnusedDocs()
{
// unreference all docs from list of Scintilla
// by sending SCI_RELEASEDOCUMENT message
for (int i = 0 ; i < int(_buffers.size()) ; i++)
if (i != _currentIndex)
execute(SCI_RELEASEDOCUMENT, 0, _buffers[i]._doc);
// remove all docs except the current doc from list
_buffers.clear();
}
void ScintillaEditView::getText(char *dest, int start, int end)
{
TextRange tr;
tr.chrg.cpMin = start;
tr.chrg.cpMax = end;
tr.lpstrText = dest;
execute(SCI_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&tr));
}
void ScintillaEditView::marginClick(int position, int modifiers){ int lineClick = int(execute(SCI_LINEFROMPOSITION, position, 0)); int levelClick = int(execute(SCI_GETFOLDLEVEL, lineClick, 0)); if (levelClick & SC_FOLDLEVELHEADERFLAG) { if (modifiers & SCMOD_SHIFT) { // Ensure all children visible execute(SCI_SETFOLDEXPANDED, lineClick, 1); expand(lineClick, true, true, 100, levelClick); } else if (modifiers & SCMOD_CTRL) { if (execute(SCI_GETFOLDEXPANDED, lineClick, 0)) { // Contract this line and all children execute(SCI_SETFOLDEXPANDED, lineClick, 0); expand(lineClick, false, true, 0, levelClick); } else { // Expand this line and all children execute(SCI_SETFOLDEXPANDED, lineClick, 1); expand(lineClick, true, true, 100, levelClick); } } else { // Toggle this line execute(SCI_TOGGLEFOLD, lineClick, 0); } }}void ScintillaEditView::expand(int &line, bool doExpand, bool force, int visLevels, int level){ int lineMaxSubord = int(execute(SCI_GETLASTCHILD, line, level & SC_FOLDLEVELNUMBERMASK)); line++; while (line <= lineMaxSubord) { if (force) { if (visLevels > 0) execute(SCI_SHOWLINES, line, line); else execute(SCI_HIDELINES, line, line); } else { if (doExpand) execute(SCI_SHOWLINES, line, line); } int levelLine = level; if (levelLine == -1) levelLine = int(execute(SCI_GETFOLDLEVEL, line, 0)); if (levelLine & SC_FOLDLEVELHEADERFLAG) { if (force) { if (visLevels > 1) execute(SCI_SETFOLDEXPANDED, line, 1); else execute(SCI_SETFOLDEXPANDED, line, 0); expand(line, doExpand, force, visLevels - 1); } else { if (doExpand) { if (!execute(SCI_GETFOLDEXPANDED, line, 0)) execute(SCI_SETFOLDEXPANDED, line, 1); expand(line, true, force, visLevels - 1); } else { expand(line, false, force, visLevels - 1); } } } else { line++; } }}
void ScintillaEditView::makeStyle(const char *lexerName)
{
LexerStyler *pStyler = (_pParameter->getLStylerArray()).getLexerStylerByName(lexerName);
if (pStyler)
{
for (int i = 0 ; i < pStyler->getNbStyler() ; i++)
{
Style & style = pStyler->getStyler(i);
setStyle(style._styleID, style._fgColor, style._bgColor, style._fontName, style._fontStyle, style._fontSize);
}
}
}
void ScintillaEditView::performGlobalStyles()
{
StyleArray & stylers = _pParameter->getMiscStylerArray();
int i = stylers.getStylerIndexByName("Current line background colour");
if (i != -1)
{
Style & style = stylers.getStyler(i);
execute(SCI_SETCARETLINEBACK, style._bgColor);
}
i = stylers.getStylerIndexByName("Mark colour");
if (i != -1)
{
Style & style = stylers.getStyler(i);
execute(SCI_MARKERSETFORE, 1, style._fgColor);
execute(SCI_MARKERSETBACK, 1, style._bgColor);
}
// COLORREF selectColorFore = yellow;
COLORREF selectColorBack = grey;
i = stylers.getStylerIndexByName("Selected text colour");
if (i != -1)
{
Style & style = stylers.getStyler(i);
selectColorBack = style._bgColor;
execute(SCI_SETSELBACK, 1, selectColorBack);
}
COLORREF caretColor = black;
i = stylers.getStylerIndexByID(SCI_SETCARETFORE);
if (i != -1)
{
Style & style = stylers.getStyler(i);
caretColor = style._fgColor;
}
execute(SCI_SETCARETFORE, caretColor);
}
void ScintillaEditView::setLineIndent(int line, int indent) const {
if (indent < 0)
return;
CharacterRange crange = getSelection();
int posBefore = execute(SCI_GETLINEINDENTPOSITION, line);
execute(SCI_SETLINEINDENTATION, line, indent);
int posAfter = execute(SCI_GETLINEINDENTPOSITION, line);
int posDifference = posAfter - posBefore;
if (posAfter > posBefore) {
// Move selection on
if (crange.cpMin >= posBefore) {
crange.cpMin += posDifference;
}
if (crange.cpMax >= posBefore) {
crange.cpMax += posDifference;
}
} else if (posAfter < posBefore) {
// Move selection back
if (crange.cpMin >= posAfter) {
if (crange.cpMin >= posBefore)
crange.cpMin += posDifference;
else
crange.cpMin = posAfter;
}
if (crange.cpMax >= posAfter) {
if (crange.cpMax >= posBefore)
crange.cpMax += posDifference;
else
crange.cpMax = posAfter;
}
}
execute(SCI_SETSEL, crange.cpMin, crange.cpMax);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -