scintillaeditview.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #pragma once
  2. #include <qsciscintilla.h>
  3. #include <Scintilla.h>
  4. #include <SciLexer.h>
  5. #include <QMouseEvent>
  6. #include <QMimeData>
  7. #include <unordered_set>
  8. #include "common.h"
  9. #include "Sorters.h"
  10. typedef sptr_t(*SCINTILLA_FUNC) (sptr_t ptr, unsigned int, uptr_t, sptr_t);
  11. typedef sptr_t SCINTILLA_PTR;
  12. //const int MARK_BOOKMARK = 24;
  13. //const int MARK_HIDELINESBEGIN = 23;
  14. //const int MARK_HIDELINESEND = 22;
  15. //const int MARK_HIDELINESUNDERLINE = 21;
  16. const int NB_FOLDER_STATE = 7;
  17. struct LanguageName {
  18. const QString lexerName;
  19. const QString shortName;
  20. const QString longName;
  21. LangType LangID = L_TXT;
  22. int lexerID = 0;
  23. };
  24. struct PosInfo {
  25. int line;
  26. int index;
  27. PosInfo(int line_, int index_):line(line_),index(index_)
  28. {
  29. }
  30. };
  31. enum TextCaseType
  32. {
  33. UPPERCASE,
  34. LOWERCASE,
  35. TITLECASE_FORCE,
  36. TITLECASE_BLEND,
  37. SENTENCECASE_FORCE,
  38. SENTENCECASE_BLEND,
  39. INVERTCASE,
  40. RANDOMCASE
  41. };
  42. enum Comment_Mode
  43. {
  44. cm_comment = 0,
  45. cm_uncomment,
  46. cm_toggle
  47. };
  48. const bool L2R = true;
  49. const bool R2L = false;
  50. struct ColumnModeInfo {
  51. intptr_t _selLpos = 0;
  52. intptr_t _selRpos = 0;
  53. intptr_t _order = -1; // 0 based index
  54. bool _direction = L2R; // L2R or R2L
  55. intptr_t _nbVirtualCaretSpc = 0;
  56. intptr_t _nbVirtualAnchorSpc = 0;
  57. ColumnModeInfo(intptr_t lPos, intptr_t rPos, intptr_t order, bool dir = L2R, intptr_t vAnchorNbSpc = 0, intptr_t vCaretNbSpc = 0)
  58. : _selLpos(lPos), _selRpos(rPos), _order(order), _direction(dir), _nbVirtualAnchorSpc(vAnchorNbSpc), _nbVirtualCaretSpc(vCaretNbSpc) {};
  59. bool isValid() const {
  60. return (_order >= 0 && _selLpos >= 0 && _selRpos >= 0 && _selLpos <= _selRpos);
  61. };
  62. };
  63. struct SortInPositionOrder {
  64. bool operator() (ColumnModeInfo& l, ColumnModeInfo& r) {
  65. return (l._selLpos < r._selLpos);
  66. }
  67. };
  68. struct SortInSelectOrder {
  69. bool operator() (ColumnModeInfo& l, ColumnModeInfo& r) {
  70. return (l._order < r._order);
  71. }
  72. };
  73. typedef std::vector<ColumnModeInfo> ColumnModeInfos;
  74. const int MASK_FORMAT = 0x03;
  75. const int MASK_ZERO_LEADING = 0x04;
  76. const int BASE_10 = 0x00; // Dec
  77. const int BASE_16 = 0x01; // Hex
  78. const int BASE_08 = 0x02; // Oct
  79. const int BASE_02 = 0x03; // Bin
  80. #define URL_INDIC 8
  81. enum urlMode {
  82. urlDisable = 0, urlNoUnderLineFg, urlUnderLineFg, urlNoUnderLineBg, urlUnderLineBg,
  83. urlMin = urlDisable,
  84. urlMax = urlUnderLineBg
  85. };
  86. #define INDIC_EXPLORERLINK 22
  87. enum Font_Set_Bit {
  88. Bold_Bit = 0x1,
  89. Italic_Bit = 0x2,
  90. Underline_Bit = 0x4,
  91. Font_Name_Bit = 0x8,
  92. Font_Size_Bit = 0x10,
  93. ALL_SET_Bit = 0x1f,
  94. };
  95. class FindRecords;
  96. class CCNotePad;
  97. struct BigTextEditFileMgr;
  98. class ScintillaEditView : public QsciScintilla
  99. {
  100. Q_OBJECT
  101. public:
  102. ScintillaEditView(QWidget *parent,bool isBigText = false);
  103. virtual ~ScintillaEditView();
  104. virtual void setLexer(QsciLexer *lexer = 0);
  105. void setNoteWidget(QWidget* win);
  106. //void resetDefaultFontStyle();
  107. sptr_t execute(quint32 Msg, uptr_t wParam = 0, sptr_t lParam = 0) const;
  108. static QsciLexer * createLexer(int lexerId, QString tag="", bool isOrigin=false, int styleId=-1);
  109. static QString getTagByLexerId(int lexerId);
  110. void appendMarkRecord(FindRecords *r);
  111. void releaseAllMark();
  112. QList<FindRecords*>& getCurMarkRecord();
  113. bool gotoPrePos();
  114. bool gotoNextPos();
  115. /*virtual void adjuctSkinStyle();*/
  116. //设置文档的缩进参考线
  117. void setIndentGuide(bool willBeShowed);
  118. void convertSelectedTextTo(const TextCaseType & caseToConvert);
  119. void removeAnyDuplicateLines();
  120. void insertCharsFrom(size_t position, const QByteArray & text2insert) const;
  121. std::pair<size_t, size_t> getSelectionLinesRange(intptr_t selectionNumber = -1) const;
  122. void insertNewLineAboveCurrentLine(bool check = true);
  123. void insertNewLineBelowCurrentLine(bool check = true);
  124. void sortLines(size_t fromLine, size_t toLine, ISorter * pSort);
  125. intptr_t lastZeroBasedLineNumber() const {
  126. auto endPos = execute(SCI_GETLENGTH);
  127. return execute(SCI_LINEFROMPOSITION, endPos);
  128. };
  129. intptr_t getCurrentLineNumber()const {
  130. return execute(SCI_LINEFROMPOSITION, execute(SCI_GETCURRENTPOS));
  131. };
  132. void bookmarkToggle(intptr_t lineno) const;
  133. void bookmarkClearAll() const;
  134. void bookmarkNext(bool forwardScan);
  135. void cutMarkedLines();
  136. void copyMarkedLines();
  137. void replaceMarkedline(int ln, QByteArray & str);
  138. void pasteToMarkedLines();
  139. void deleteMarkedLines(bool isMarked);
  140. void inverseMarks();
  141. intptr_t searchInTarget(QByteArray & text2Find, size_t fromPos, size_t toPos) const;
  142. intptr_t replaceTargetRegExMode(QByteArray & re, intptr_t fromTargetPos = -1, intptr_t toTargetPos = -1) const;
  143. intptr_t replaceTarget(QByteArray & str2replace, intptr_t fromTargetPos = -1, intptr_t toTargetPos = -1) const;
  144. //设置不同风格
  145. void setStyleOptions();
  146. ColumnModeInfos getColumnModeSelectInfo();
  147. void columnReplace(ColumnModeInfos& cmi, QByteArray& str);
  148. void setMultiSelections(const ColumnModeInfos& cmi);
  149. void columnReplace(ColumnModeInfos& cmi, int initial, int incr, int repeat, int format, bool isCapital, QByteArray& prefix);
  150. void setBigTextMode(bool isBigText);
  151. void showBigTextLineAddr(qint64 fileOffset);
  152. void showBigTextLineAddr(qint64 fileStartOffset, qint64 fileEndOffset);
  153. void showBigTextRoLineNum(BigTextEditFileMgr* txtFile, int blockIndex);
  154. void updateThemes();
  155. void clearSuperBitLineCache();
  156. //下面三个函数,是设置全局样式的接口。全局样式不同于每个语法中的样式
  157. void setGlobalFgColor(int style);
  158. void setGlobalBgColor(int style);
  159. void setGlobalFont(int style);
  160. //获取当前块的开始行号。只在大文件只读模式下有效。其余模式下均返回0
  161. quint32 getBigTextBlockStartLine();
  162. void setBigTextBlockStartLine(quint32 line);
  163. signals:
  164. void delayWork();
  165. protected:
  166. virtual void addHotSpot();
  167. void setStylesFont(const QFont& f, int style, int setBitMask = ALL_SET_Bit);
  168. private:
  169. void getText(char * dest, size_t start, size_t end) const;
  170. QString getGenericTextAsQString(size_t start, size_t end) const;
  171. QString getEOLString();
  172. void appandGenericText(const QByteArray & text2Append) const;
  173. QString getMarkedLine(int ln);
  174. void deleteMarkedline(int ln);
  175. void setFoldColor(int margin, QColor fgClack, QColor bkColor, QColor foreActive);
  176. bool doBlockComment(Comment_Mode currCommentMode);
  177. bool undoStreamComment(bool tryBlockComment = true);
  178. bool doStreamComment();
  179. void getVisibleStartAndEndPosition(int * startPos, int * endPos);
  180. void changeStyleColor(int sytleId);
  181. void initStyleColorMenu();
  182. public:
  183. static const int _SC_MARGE_LINENUMBER;
  184. static const int _SC_MARGE_SYBOLE;
  185. static const int _SC_MARGE_FOLDER;
  186. #ifdef Q_OS_WIN //目前这个破玩意压根没用
  187. static LanguageName langNames[L_EXTERNAL + 1];
  188. #endif
  189. static const int _markersArray[][NB_FOLDER_STATE];
  190. protected:
  191. void dragEnterEvent(QDragEnterEvent* event) override;
  192. void dropEvent(QDropEvent* e) override;
  193. void mouseDoubleClickEvent(QMouseEvent *e) override;
  194. void contextUserDefineMenuEvent(QMenu * menu) override;
  195. public slots:
  196. void updateLineNumberWidth(int lineNumberMarginDynamicWidth=0);
  197. void slot_linePosChanged(int line, int pos);
  198. private:
  199. void updateLineNumbersMargin(bool forcedToHide);
  200. void autoAdjustLineWidth(int xScrollValue);
  201. void showMargin(int whichMarge, bool willBeShowed);
  202. void init();
  203. void bookmarkAdd(intptr_t lineno) const;
  204. void bookmarkDelete(size_t lineno) const;
  205. bool bookmarkPresent(intptr_t lineno) const;
  206. void changeCase(const TextCaseType & caseToConvert, QString & strToConvert) const;
  207. void clearIndicator(int indicatorNumber);
  208. void highlightViewWithWord(QString & word2Hilite);
  209. void slot_markColorGroup(QAction * action);
  210. void replaceSelWith(const char* replaceText);
  211. void showWordNums();
  212. private slots:
  213. void slot_delayWork();
  214. void slot_scrollYValueChange(int value);
  215. void slot_clearHightWord();
  216. void slot_bookMarkClicked(int margin, int line, Qt::KeyboardModifiers state);
  217. private:
  218. SCINTILLA_FUNC m_pScintillaFunc;
  219. SCINTILLA_PTR m_pScintillaPtr;
  220. CCNotePad* m_NoteWin;
  221. int m_preFirstLineNum;
  222. QList<FindRecords *> m_curMarkList;
  223. int m_curPos;
  224. //回退到上个位置
  225. QList<int> m_prePosRecord;
  226. //往前到下个位置
  227. QList<int> m_nextPosRecord;
  228. QPixmap* m_bookmarkPng;
  229. QMenu* m_styleColorMenu;
  230. QList<QAction*> m_styleMarkActList;
  231. bool m_isBigText;//大文本
  232. quint32 m_curBlockLineStartNum;
  233. QMap<qint64, quint32> m_addrLineNumMap;//大文本模式下,地址和行号的对应关系。只需要首尾即可
  234. public:
  235. static int s_tabLens;
  236. static bool s_noUseTab;
  237. static int s_bigTextSize;
  238. bool m_hasHighlight;
  239. };