19 using System.Collections.Generic;
21 using System.Windows.Forms;
23 using System.Drawing.Drawing2D;
24 using System.Threading;
25 using System.Runtime.InteropServices;
26 using System.Diagnostics;
30 namespace Irony.GrammarExplorer {
40 private IntPtr _savedEventMask = IntPtr.Zero;
44 #region constructor, initialization and disposing
52 ViewAdapter.SetNewText(TextBox.Text);
54 private void Connect() {
55 TextBox.MouseMove += TextBox_MouseMove;
56 TextBox.TextChanged += TextBox_TextChanged;
57 TextBox.KeyDown += TextBox_KeyDown;
58 TextBox.VScroll += TextBox_ScrollResize;
59 TextBox.HScroll += TextBox_ScrollResize;
60 TextBox.SizeChanged += TextBox_ScrollResize;
61 TextBox.Disposed += TextBox_Disposed;
62 ViewAdapter.ColorizeTokens += Adapter_ColorizeTokens;
63 this.AssignHandle(TextBox.Handle);
66 private void Disconnect() {
67 if (TextBox != null) {
68 TextBox.MouseMove -= TextBox_MouseMove;
69 TextBox.TextChanged -= TextBox_TextChanged;
70 TextBox.KeyDown -= TextBox_KeyDown;
71 TextBox.Disposed -= TextBox_Disposed;
72 TextBox.VScroll -= TextBox_ScrollResize;
73 TextBox.HScroll -= TextBox_ScrollResize;
74 TextBox.SizeChanged -= TextBox_ScrollResize;
84 GC.SuppressFinalize(
this);
87 private void InitColorTable() {
88 TokenColors[TokenColor.Comment] = Color.Green;
89 TokenColors[TokenColor.Identifier] = Color.Black;
90 TokenColors[TokenColor.Keyword] = Color.Blue;
91 TokenColors[TokenColor.Number] = Color.DarkRed;
92 TokenColors[TokenColor.String] = Color.DarkSlateGray;
93 TokenColors[TokenColor.Text] = Color.Black;
98 #region TextBox event handlers
100 void TextBox_MouseMove(
object sender, MouseEventArgs e) {
104 void TextBox_KeyDown(
object sender, KeyEventArgs e) {
108 void TextBox_TextChanged(
object sender,
EventArgs e) {
110 if (_colorizing)
return;
111 ViewAdapter.SetNewText(TextBox.Text);
113 void TextBox_ScrollResize(
object sender,
EventArgs e) {
118 void TextBox_Disposed(
object sender,
EventArgs e) {
121 private void UpdateViewRange() {
122 int minpos = TextBox.GetCharIndexFromPosition(
new Point(0, 0));
123 int maxpos = TextBox.GetCharIndexFromPosition(
new Point(TextBox.ClientSize.Width, TextBox.ClientSize.Height));
124 ViewAdapter.SetViewRange(minpos, maxpos);
130 [DllImport(
"user32", CharSet = CharSet.Auto)]
131 private extern static IntPtr SendMessage(IntPtr hWnd,
int msg,
int wParam, IntPtr lParam);
133 [DllImport(
"user32.dll")]
134 private static extern bool PostMessageA(IntPtr hWnd,
int nBar,
int wParam,
int lParam);
136 [DllImport(
"user32.dll", CharSet = CharSet.Auto)]
137 private static extern int GetScrollPos(
int hWnd,
int nBar);
139 [DllImport(
"user32.dll")]
140 private static extern int SetScrollPos(IntPtr hWnd,
int nBar,
int nPos,
bool bRedraw);
142 private const int WM_SETREDRAW = 0x000B;
143 private const int WM_USER = 0x400;
144 private const int EM_GETEVENTMASK = (WM_USER + 59);
145 private const int EM_SETEVENTMASK = (WM_USER + 69);
146 private const int SB_HORZ = 0x0;
147 private const int SB_VERT = 0x1;
148 private const int WM_HSCROLL = 0x114;
149 private const int WM_VSCROLL = 0x115;
150 private const int SB_THUMBPOSITION = 4;
151 const int WM_PAINT = 0x000F;
153 private int HScrollPos {
156 return GetScrollPos((
int)TextBox.Handle, SB_HORZ);
159 SetScrollPos((IntPtr)TextBox.Handle, SB_HORZ, value,
true);
160 PostMessageA((IntPtr)TextBox.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
164 private int VScrollPos {
166 return GetScrollPos((
int)TextBox.Handle, SB_VERT);
169 SetScrollPos((IntPtr)TextBox.Handle, SB_VERT, value,
true);
170 PostMessageA((IntPtr)TextBox.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
175 #region Colorizing tokens
178 SendMessage(TextBox.Handle, WM_SETREDRAW, 0, IntPtr.Zero );
180 _savedEventMask = SendMessage(TextBox.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
186 SendMessage(TextBox.Handle, EM_SETEVENTMASK, 0, _savedEventMask);
188 SendMessage(TextBox.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
192 if (_disposed)
return;
196 int hscroll = HScrollPos;
197 int vscroll = VScrollPos;
198 int selstart = TextBox.SelectionStart;
199 int selLength = TextBox.SelectionLength;
203 Color color = GetTokenColor(tkn);
204 TextBox.Select(tkn.Location.Position, tkn.Length);
205 TextBox.SelectionColor = color;
208 TextBox.Select(selstart, selLength);
209 HScrollPos = hscroll;
210 VScrollPos = vscroll;
214 TextBox.Invalidate();
218 if (token.
EditorInfo == null)
return Color.Black;
227 if (TokenColors.TryGetValue(colorIndex, out result))
return result;
233 #region IUIThreadInvoker Members
236 TextBox.BeginInvoke(
new MethodInvoker(colorize));
readonly TokenList Tokens
delegate void ColorizeMethod()
KeyTerm KeyTerm
Gets the Key terminal if any.
void InvokeOnUIThread(ColorizeMethod colorize)
bool FlagIsSet(TermFlags flag)
SiliconStudio.Core.Mathematics.Color Color
TokenEditorInfo EditorInfo
TokenEditorInfo EditorInfo
Gets the Editor info.
readonly EditorAdapter Adapter
readonly EditorViewAdapter ViewAdapter
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
System.Windows.Point Point
RichTextBoxHighlighter(RichTextBox textBox, LanguageData language)