xref: /MusicPlayer2/MusicPlayer2/ScintillaEditView.h (revision ec0490a72e915b8aa451edfc87be3aa2eecbe491)
1 #pragma once
2 
3 #include "../scintilla/include/SciLexer.h"
4 #include "../scintilla/include/Scintilla.h"
5 #include "ColorConvert.h"
6 
7 // CScintillaEditView 视图
8 
9 #define SCINTILLA_MARGIN_LINENUMBER 0
10 #define MARGIN_FOLD_INDEX 1
11 
12 class CScintillaEditView : public CView
13 {
14 	DECLARE_DYNCREATE(CScintillaEditView)
15 
16 protected:
17 	CScintillaEditView();           // 动态创建所使用的受保护的构造函数
18 	virtual ~CScintillaEditView();
19 
20 public:
21     struct KeepCurrentLine
22     {
23         KeepCurrentLine(CScintillaEditView* view)
24             : m_view(view)
25         {
26             //保存当前行
27             current_line = m_view->GetFirstVisibleLine();
28         }
29         ~KeepCurrentLine()
30         {
31             //恢复当前行
32             m_view->SetFirstVisibleLine(current_line);
33         }
34 
35         CScintillaEditView* m_view{};
36         int current_line{};
37     };
38 
39 	virtual void OnDraw(CDC* pDC);      // 重写以绘制该视图
40 #ifdef _DEBUG
41 	virtual void AssertValid() const;
42 #ifndef _WIN32_WCE
43 	virtual void Dump(CDumpContext& dc) const;
44 #endif
45 #endif
46 
47     void SetText(const wstring& text);
48     void GetText(wstring& text);
49     const wchar_t* GetText(int& size);      //获取文本(返回字符串指针,需要自行释放内存)
50     const char* GetTextUtf8(int& size);      //获取UTF8格式文本(返回字符串指针,需要自行释放内存)
51     void SetFontFace(const wchar_t* font_face);
52     void SetFontSize(int font_size);
53     void SetTabSize(int tab_size);
54     void SetSel(int start, int end, const wstring& edit_str);        //设置选中范围(位置以字符为单位)
55     void GetSel(int& start, int& end);      //获取选中范围(位置以字符为单位)
56     void SetBackgroundColor(COLORREF color);
57     void SetReadOnly(bool read_only);
58     bool IsReadOnly();
59 
60     void Undo();
61     void Redo();
62     void Cut();
63     void Copy();
64     void Paste();
65     void SelectAll();
66     void EmptyUndoBuffer();     //清空撤销缓存
67 
68     void SetWordWrap(bool word_wrap);
69 
70     bool IsEditChangeNotificationEnable();
71 
72     bool CanUndo();
73     bool CanRedo();
74     bool CanPaste();
75     bool IsSelectionEmpty();
76     bool IsModified();
77     void SetSavePoint();
78 
79     void SetLineNumberWidth(int width);
80     void ShowLineNumber(bool show);
81     void SetLineNumberColor(COLORREF color);
82 
83     int GetZoom();
84     void SetZoom(int zoom);
85 
86     enum eEolMode
87     {
88         EOL_CRLF,
89         EOL_CR,
90         EOL_LF
91     };
92     void SetEolMode(eEolMode eolMode);
93     eEolMode GetEolMode();
94 
95     void ConvertEolMode(eEolMode eolMode);
96 
97     void SetViewEol(bool show);
98 
99     int GetFirstVisibleLine();
100     void SetFirstVisibleLine(int line);
101 
102     //语法解析
103     void SetLexer(int lexer);
104     void SetKeywords(int id, const char* keywords);
105     void SetSyntaxColor(int id, COLORREF color);
106     void SetSyntaxFontStyle(int id, bool bold, bool italic);
107     void SetLexerNormalText();
108 
109     static eEolMode JudgeEolMode(const wstring& str);
110     static int CharactorPosToBytePos(int pos, const wchar_t* str, size_t size);     //将字符的位置转换成字节的位置(使用UTF8编码)
111     static int BytePosToCharactorPos(int pos, const char* str, size_t size);     //将字节的位置转换成字符的位置(使用UTF8编码)
112 
113     void SetContextMenu(CMenu* pMenu, CWnd* pMenuOwner);
114 
115     void SetLexerLyric(ColorTable theme_color);       //设置LRC语法解析
116 
117 private:
118 
119 private:
120     bool m_change_notification_enable = true;      //如果为false,则不响应文本改变消息
121     int m_line_number_width = 36;
122     COLORREF m_line_number_color{};
123     COLORREF m_background_color{ RGB(255,255,255) };
124 
125     CMenu* m_pMenu{};
126     CWnd* m_pContextMenuOwner{};
127 
128 protected:
129 	DECLARE_MESSAGE_MAP()
130     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
131 public:
132     afx_msg void OnPaint();
133     virtual void PreSubclassWindow();
134     virtual void OnInitialUpdate();
135     afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
136 };
137 
138 
139