📄 qsteeditor.cpp
字号:
{ int braceAtCaret = -1; int braceOpposite = -1; bool isInside = findMatchingBracePosition(braceAtCaret,braceOpposite, true); if (isInside) { if (braceOpposite > braceAtCaret) { braceAtCaret++; } else { braceOpposite++; } } else { // Outside if (braceOpposite > braceAtCaret) { braceOpposite++; } else { braceAtCaret++; } } if (braceOpposite >= 0) { ensureRangeVisible(braceOpposite, braceOpposite); setSelection(braceAtCaret,braceOpposite); }}//abbrevvoid QSteEditor::insertAbbreviation(){ printf("insertAbbreviation\n");}void QSteEditor::expandAbbreviation(){ if(m_abbrev == NULL) return; int currentPos = getCaretInLine(); int position = sendMsgToSci(SCI_GETCURRENTPOS); // from the beginning int linenum = getCurrentLinenum(); SString line(getText(linenum).toStdString().c_str()); int current = getCaretInLine(); if(current == 0) return; int startword = current; while(startword > 0 && wordCharacters.contains(line[startword - 1])){ startword--; } SString abbword = line.substr(startword,current - startword); if(abbword.length() == 0) return; SString data = m_abbrev->getAbbrev(abbword.c_str()); if(data.length() == 0) return; int caret_pos = -1; // caret position int indent = 0; int indentextra = 0; bool isindent = true; int eolMode = sendMsgToSci(SCI_GETEOLMODE); if(m_autoIndent){ indent = getLineIndentation(linenum); } sendMsgToSci(SCI_BEGINUNDOACTION); sendMsgToSci(SCI_SETSEL,position - abbword.length(),position); // add the abbreviation one character at a time for(size_t i = 0;i < data.length();i++){ char c = data[i]; SString abbrevtext(""); if(c == '\\'){ if(data[i+1] == 't') c = '\t'; else if(data[i+1] == 'n') c = '\n'; i++; } if(isindent && c == '\t'){ indentextra++; setLineIndentation(linenum,indent + sendMsgToSci(SCI_GETINDENT) * indentextra); }else{ switch(c){ case '|': // user may want to insert '|' instead of caret if(i < (data.length() - 1) && data[i + 1] == '|'){ //put '|' into the line abbrevtext += c; i++; }else if(caret_pos == -1){ if(i == 0) caret_pos = sendMsgToSci(SCI_GETCURRENTPOS - abbword.length()); else caret_pos = sendMsgToSci(SCI_GETCURRENTPOS); } break; case '\n': if (eolMode == SC_EOL_CRLF || eolMode == SC_EOL_CR) { abbrevtext += '\r'; } if (eolMode == SC_EOL_CRLF || eolMode == SC_EOL_LF) { abbrevtext += '\n'; } break; default: abbrevtext += c; break; } sendMsgToSci(SCI_REPLACESEL,0UL,abbrevtext.c_str()); if(c == '\n') { isindent = true; indentextra = 0; linenum++; setLineIndentation(linenum,indent); } else { isindent = false; } } } if (caret_pos != -1) { sendMsgToSci(SCI_GOTOPOS,caret_pos); } sendMsgToSci(SCI_ENDUNDOACTION);}//find & search & replaceint QSteEditor::doFind(const char *str,int len,int startpos,int endpos){ if(str == NULL) return -1; if(len == -1) len = strlen(str); if(startpos == -1) startpos = 0; if(endpos == -1) endpos = sendMsgToSci(SCI_GETTEXTLENGTH); sendMsgToSci(SCI_SETTARGETSTART,startpos); sendMsgToSci(SCI_SETTARGETEND,endpos); int posfind = sendMsgToSci(SCI_SEARCHINTARGET,len,str); if(posfind != -1){ setSelection(posfind,posfind + len); } return posfind;}int QSteEditor::doReplace(const char *str){ if(str == NULL) return -1; CharacterRange crange; crange.cpMin = sendMsgToSci(SCI_GETSELECTIONSTART); crange.cpMax = sendMsgToSci(SCI_GETSELECTIONEND); if(crange.cpMax == crange.cpMin) return -1; sendMsgToSci(SCI_REPLACESEL,0UL,str); return sendMsgToSci(SCI_GETCURRENTPOS);}//printingvoid QSteEditor::filePrint(QPrinter *printer){ if(printer == NULL) return; int mag = 0; int wrap = SC_WRAP_WORD; // Setup the printing area from parameter in printer QRect def_area; //int right = printer->paperRect().right() - printer->pageRect().right(); //int bottom = printer->paperRect().bottom() - printer->pageRect().bottom(); int left= printer->paperRect().left() - printer->pageRect().left(); int top= printer->paperRect().top() - printer->pageRect().top(); def_area.setY(top); def_area.setX(left); def_area.setWidth(printer->width() - left * 2); def_area.setHeight(printer->height() - top * 2); // Get the page range. int pgfrom, pgto; pgfrom = printer->fromPage(); pgto = printer->toPage(); // Find the position range. long startpos, endpos; endpos = sendMsgToSci(SCI_GETLENGTH); startpos = 0; QPainter painter(printer); bool reverse = (printer->pageOrder() == QPrinter::LastPageFirst); bool neednewpage = false; sendMsgToSci(SCI_SETPRINTMAGNIFICATION,0); sendMsgToSci(SCI_SETPRINTWRAPMODE,wrap); for(int i = 1;i <= printer->numCopies();i++){ QStack<long> pagestarts; int currpage = 1; long pos = startpos; while(pos < endpos){ if(pgto > 0 && pgto < currpage) break; bool render = false; if(pgfrom == 0 || pgfrom <= currpage){ if(reverse){ pagestarts.push(pos); }else{ render = true; if(neednewpage){ if(!printer->newPage()) return; }else{ neednewpage = true; } } } QRect area = def_area;// printer->formatPage(painter,render,area,currpage); pos = sendMsgToSci(SCI_FORMATRANGE,render,&painter,area,pos,endpos); currpage++; } if(!reverse) continue; while(!pagestarts.isEmpty()){ --currpage; long epos = pos; pos = pagestarts.pop(); if(neednewpage){ if(!printer->newPage()){ return; } }else{ neednewpage = true; } QRect area = def_area;// printer->formatPage(painter,true,area,currpage); sendMsgToSci(SCI_FORMATRANGE,true,&painter,area,pos,epos); } }}void QSteEditor::filePrintPreview(QPrinter *printer){ printf("print view\n"); if(printer == NULL) return; QStePrint *steprint = new QStePrint(this,printer);// steprint->setGeometry(this->geometry()); steprint->setHorizontalScrollBar(this->horizontalScrollBar()); steprint->setVerticalScrollBar(this->verticalScrollBar()); // steprint->setFocus(); steprint->show();}void QSteEditor::filePrintPdf(const char *filename){}void QSteEditor::selectionPrint(){}//margin & linenumvoid QSteEditor::showLineNum(bool show){ //some error in here if(show){ int lineNumWidth = 4; // The margin size will be expanded if the current buffer's maximum // line number would overflow the margin. int lineCount = sendMsgToSci(SCI_GETLINECOUNT); lineNumWidth = 1; while (lineCount >= 10) { lineCount /= 10; ++lineNumWidth; } if (lineNumWidth < m_lineNumWidth){ lineNumWidth = m_lineNumWidth; } // The 4 here allows for spacing: 1 pixel on left and 3 on right. int pixelWidth = 4 + lineNumWidth * sendMsgToSci(SCI_TEXTWIDTH,STYLE_LINENUMBER,"9"); sendMsgToSci(SCI_SETMARGINWIDTHN,0UL,pixelWidth); } else { sendMsgToSci(SCI_SETMARGINWIDTHN); }}void QSteEditor::showMargin(bool show){ sendMsgToSci(SCI_SETMARGINWIDTHN,1UL,show?m_marginWidth : 0);}//folding & envolevoid QSteEditor::showFoldMargin(bool show){ sendMsgToSci(SCI_SETMARGINWIDTHN,2UL,show?m_foldMarginWidth : 0);}void QSteEditor::defineMarker(int marker, int markerType, ColourDesired fore, ColourDesired back){ sendMsgToSci(SCI_MARKERDEFINE, marker, markerType); sendMsgToSci(SCI_MARKERSETFORE, marker, fore.AsLong()); sendMsgToSci(SCI_MARKERSETBACK, marker, back.AsLong());}void QSteEditor::makeFoldSymbol(){ if(m_global == NULL) return; sendMsgToSci(SCI_SETMARGINTYPEN,2UL,static_cast<long int>(SC_MARGIN_SYMBOL)); sendMsgToSci(SCI_SETMARGINWIDTHN,2UL,m_foldMargin?m_foldMarginWidth : 0); sendMsgToSci(SCI_SETMARGINMASKN,2UL,SC_MASK_FOLDERS); sendMsgToSci(SCI_SETMARGINSENSITIVEN,2UL,1UL); switch (m_global->GetInt("fold.symbols")) { case 0: // Arrow pointing right for contracted folders, arrow pointing down for expanded defineMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_ARROWDOWN,ColourDesired(0, 0, 0), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDER, SC_MARK_ARROW,ColourDesired(0, 0, 0), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY,ColourDesired(0, 0, 0), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY,ColourDesired(0, 0, 0), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY,ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY,ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); break; case 1: // Plus for contracted folders, minus for expanded defineMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDER, SC_MARK_PLUS, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); defineMarker(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0, 0, 0)); break; case 2: // Like a flattened tree control using circular headers and curved joins defineMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_CIRCLEMINUS, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x40, 0x40, 0x40)); defineMarker(SC_MARKNUM_FOLDER, SC_MARK_CIRCLEPLUS, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x40, 0x40, 0x40)); defineMarker(SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x40, 0x40, 0x40)); defineMarker(SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNERCURVE, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x40, 0x40, 0x40)); defineMarker(SC_MARKNUM_FOLDEREND, SC_MARK_CIRCLEPLUSCONNECTED, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x40, 0x40, 0x40)); defineMarker(SC_MARKNUM_FOLDEROPENMID, SC_MARK_CIRCLEMINUSCONNECTED, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x40, 0x40, 0x40)); defineMarker(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNERCURVE, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x40, 0x40, 0x40)); break; case 3: // Like a flattened tree control using square headers defineMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_BOXMINUS, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x80, 0x80, 0x80)); defineMarker(SC_MARKNUM_FOLDER, SC_MARK_BOXPLUS, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x80, 0x80, 0x80)); defineMarker(SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x80, 0x80, 0x80)); defineMarker(SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNER, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x80, 0x80, 0x80)); defineMarker(SC_MARKNUM_FOLDEREND, SC_MARK_BOXPLUSCONNECTED, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x80, 0x80, 0x80)); defineMarker(SC_MARKNUM_FOLDEROPENMID, SC_MARK_BOXMINUSCONNECTED, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x80, 0x80, 0x80)); defineMarker(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNER, ColourDesired(0xff, 0xff, 0xff), ColourDesired(0x80, 0x80, 0x80)); break; }}void QSteEditor::expand(int &line,bool doExpand,bool force,int visLevels,int level){ int lineMaxSubord = sendMsgToSci(SCI_GETLASTCHILD,line, level & SC_FOLDLEVELNUMBERMASK); line++; while (line <= lineMaxSubord) { if (force) { if (visLevels > 0) sendMsgToSci(SCI_SHOWLINES, line, line); else sendMsgToSci(SCI_HIDELINES, line, line); } else { if (doExpand) sendMsgToSci(SCI_SHOWLINES, line, line); } int levelLine = level; if (levelLine == -1) levelLine = sendMsgToSci(SCI_GETFOLDLEVEL, line); if (levelLine & SC_FOLDLEVELHEADERFLAG) { if (force) { if (visLevels > 1) sendMsgToSci(SCI_SETFOLDEXPANDED, line, 1); else sendMsgToSci(SCI_SETFOLDEXPANDED, line); expand(line, doExpand, force, visLevels - 1); } else { if (doExpand) { if (!sendMsgToSci(SCI_GETFOLDEXPANDED, line)) sendMsgToSci(SCI_SETFOLDEXPANDED, line, 1); expand(line, true, force, visLevels - 1); } else { expand(line, false, force, visLevels - 1); } } } else { line++; } }}void QSteEditor::foldAll(){ sendMsgToSci(SCI_COLOURISE,0,-1); int maxLine = sendMsgToSci(SCI_GETLINECOUNT); bool expanding = true; for (int lineSeek = 0; lineSeek < maxLine; lineSeek++) { if (sendMsgToSci(SCI_GETFOLDLEVEL,lineSeek) & SC_FOLDLEVELHEADERFLAG) { expanding = !sendMsgToSci(SCI_GETFOLDEXPANDED, lineSeek); break; } } for (int line = 0; line < maxLine; line++) { int level = sendMsgToSci(SCI_GETFOLDLEVEL, line); if ((level & SC_FOLDLEVELHEADERFLAG) && (SC_FOLDLEVELBASE == (level & SC_FOLDLEVELNUMBERMASK))) { if (expanding){ sendMsgToSci(SCI_SETFOLDEXPANDED, line, 1); expand(line, true, false, 0, level); line--; }else{ int lineMaxSubord = sendMsgToSci(SCI_GETLASTCHILD, line, -1); sendMsgToSci(SCI_SETFOLDEXPANDED,line); if (lineMaxSubord > line) sendMsgToSci(SCI_HIDELINES, line + 1, lineMaxSubord); } } }}void QSteEditor::ensureAllChildrenVisible(int line,int level){ sendMsgToSci(SCI_SETFOLDEXPANDED, line, 1); expand(line, true, true, 100, level);}void QSteEditor::toggleFoldRecursive(int line,int level){ if(sendMsgToSci(SCI_GETFOLDEXPANDED, line)) { sendMsgToSci(SCI_SETFOLDEXPANDED, line); expand(line, false, true, 0, level); } else { sendMsgToSci(SCI_SETFOLDEXPANDED, line, 1); expand(line, true, true, 100, level); }}//wrap modevoid QSteEditor::setWrapMode(int mode){ sendMsgToSci(SCI_SETWRAPMODE,mode);}//whitespace & eolvoid QSteEditor::setEolMod(int mode){ sendMsgToSci(SCI_SETEOLMODE,mode);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -