forked from DebugST/STTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrmStyle.cs
More file actions
139 lines (126 loc) · 5.2 KB
/
FrmStyle.cs
File metadata and controls
139 lines (126 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ST.Library.UI.STTextBox;
using System.Text.RegularExpressions;
namespace STTextBoxTest.Demos
{
public partial class FrmStyle : Form
{
public FrmStyle() {
InitializeComponent();
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
STTextBox tbx = new STTextBox();
tbx.Dock = DockStyle.Fill;
tbx.Font = new System.Drawing.Font("Consolas", 10);
tbx.SetTextStyleMonitors( // use the monitor
new StyleDemo(), // Priority is in order.
new CSharpStyleMonitor(),
new SelectionStyleMonitor()
);
this.Controls.Add(tbx);
string strCode = @"
STTextBox tbx = new STTextBox();
tbx.Dock = DockStyle.Fill;
tbx.Font = new System.Drawing.Font(""Consolas"", 10);
tbx.SetTextStyleMonitors( // use the monitor
new StyleDemo(), // Priority is in order.
new CSharpStyleMonitor(),
new SelectionStyleMonitor()
);
this.Controls.Add(tbx);
/*
* this class show you how to customize the style monitor.
* It is very flexible and can be very simple or very complex.
* It depends on what logic the developer wants to use to implement it.
* The following is a very simple implementation,
* while the built-in CSharpStyleMonitor is a complex implementation.
*/
public class StyleDemo : ITextStyleMonitor
{
/*in this class just want to highlight the keyword `StyleDemo` in all text*/
private static Regex m_reg_key = new Regex(@""\bStyleDemo\b"");
private static TextStyle m_style = new TextStyle() { // the key word style
ForeColor = Color.Red,
UnderLineColor = Color.Blue,
StrikeOutColor = Color.FromArgb(200, Color.Lime),
FontStyle = FontStyle.Underline | FontStyle.Strikeout | FontStyle.Italic
};
private List<TextStyleRange> m_lst; // save the style range
public void Init(string strText) {
List<TextStyleRange> lst = new List<TextStyleRange>();
foreach (Match m in m_reg_key.Matches(strText)) {
lst.Add(new TextStyleRange() {
Index = m.Index,
Length = m.Length,
Style = m_style
});
}
//FillDefaultStyle() will auto fill the rang that not have style.
//m_lst must include all the index range in the strText.
//because get the style use GetStyleFromCharIndex().
m_lst = TextStyleMonitor.FillDefaultStyle(lst, strText.Length, TextStyle.Empty);
}
public void OnSelectionChanged(TextManager textManager, int nStart, int nLen) {
//throw new NotImplementedException();
}
public void OnTextChanged(TextManager textManager, List<TextHistoryRecord> thrs) {
this.Init(textManager.GetText());
}
public TextStyleRange GetStyleFromCharIndex(int nIndex) {
return TextStyleMonitor.GetStyleFromCharIndex(nIndex, m_lst);
}
}";
tbx.SetText(strCode.Trim());
}
}
/*
* this class show you how to customize the style monitor.
* It is very flexible and can be very simple or very complex.
* It depends on what logic the developer wants to use to implement it.
* The following is a very simple implementation,
* while the built-in CSharpStyleMonitor is a complex implementation.
*/
public class StyleDemo : ITextStyleMonitor
{
/*in this class just want to highlight the keyword `StyleDemo` in all text*/
private static Regex m_reg_key = new Regex(@"\bStyleDemo\b");
private static TextStyle m_style = new TextStyle() { // the key word style
ForeColor = Color.Red,
UnderLineColor = Color.Blue,
StrikeOutColor = Color.FromArgb(200, Color.Lime),
FontStyle = FontStyle.Underline | FontStyle.Strikeout | FontStyle.Italic
};
private List<TextStyleRange> m_lst; // save the style range
public void Init(string strText) {
List<TextStyleRange> lst = new List<TextStyleRange>();
foreach (Match m in m_reg_key.Matches(strText)) {
lst.Add(new TextStyleRange() {
Index = m.Index,
Length = m.Length,
Style = m_style
});
}
//FillDefaultStyle() will auto fill the rang that not have style.
//m_lst must include all the index range in the strText.
//because get the style use GetStyleFromCharIndex().
m_lst = TextStyleMonitor.FillDefaultStyle(lst, strText.Length, TextStyle.Empty);
}
public void OnSelectionChanged(TextManager textManager, int nStart, int nLen) {
//throw new NotImplementedException();
}
public void OnTextChanged(TextManager textManager, List<TextHistoryRecord> thrs) {
this.Init(textManager.GetText());
}
public TextStyleRange GetStyleFromCharIndex(int nIndex) {
return TextStyleMonitor.GetStyleFromCharIndex(nIndex, m_lst);
}
}
}