-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchTextBox.cs
277 lines (258 loc) · 8.7 KB
/
SearchTextBox.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace LVLTool
{
public partial class SearchTextBox : RichTextBox
{
/// <summary>
/// Constructor.
/// </summary>
public SearchTextBox()
{
InitializeComponent();
}
/// <summary>
/// A status control to update.
/// </summary>
public Control StatusControl { get; set; }
/// <summary>
/// the currernt search string
/// </summary>
public string SearchString { get; set; }
private void ContextMenuItem_Click(object source, System.EventArgs e)
{
if (source == mCutMenuItem)
this.Cut();
else if (source == mClearMenuItem)
this.Text = "";
else if (source == mCopyMenuItem)
{
//this.Copy();
Clipboard.SetData(DataFormats.Text, this.SelectedText);
}
else if (source == mPasteMenuItem)
this.Paste(DataFormats.GetFormat(DataFormats.Text));
else if (source == mFindNextMenuItem)
FindNextMatch();
else if (source == mFindPrevMenuItem)
FindPrevMatch();
else if (source == mSelectAllMenuItem)
this.SelectAll();
else if (source == mFindMenuItem)
if (SetSearchString()) FindNextMatch();
}
/// <summary>
/// Overridden for searching on key strokes
/// </summary>
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Control)
{
if (e.KeyCode == Keys.C)
{
Clipboard.SetData(DataFormats.Text, this.SelectedText);
e.Handled = true;
}
else if (e.KeyCode == Keys.F)
{
if (SetSearchString())
FindNextMatch();
}
else if (e.KeyCode == Keys.F3)
FindPrevMatch();
else if (e.KeyCode == Keys.L)
CutLine();
else if (e.KeyCode == Keys.V)
{
this.Paste(DataFormats.GetFormat(DataFormats.Text));
e.Handled = true;
}
}
else if (e.Shift)
{
if (e.KeyCode == Keys.F3)
FindPrevMatch();
}
else if (e.KeyCode == Keys.F3)
FindNextMatch();
else if (e.KeyCode == Keys.F2)
FindPrevMatch();
base.OnKeyDown(e);
}
/// <summary>
/// Finds the next match for 'SearchString.
/// </summary>
/// <returns> the line number the match occured on</returns>
public int FindPrevMatch()
{
int retVal = -1;
if (SearchString != null && !SearchString.Equals(""))
{
Regex r = new Regex(SearchString, RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(this.Text);
Match m = null;
if (mc.Count > 0)
m = mc[mc.Count - 1];
else
goto end;
int i = 0;
while (mc[i].Index < this.SelectionStart - mc[i].Length)
m = mc[i++];
if (m != null && m.Length != 0)
{
this.SelectionStart = m.Index + m.Length;
retVal = this.GetLineFromCharIndex(this.SelectionStart);
}
}
end:
UpdateStatusControl(retVal);
return retVal;
}
private void UpdateStatusControl(int retVal)
{
if (StatusControl != null)
{
if( retVal > -1)
StatusControl.Text = "Found, line: " + retVal;
else
StatusControl.Text = "'" + this.SearchString + "' Not Found";
}
}
/// <summary>
/// Finds the next match of 'SearchString'
/// </summary>
/// <returns>The line number it occurs on</returns>
public int FindNextMatch()
{
int retVal = -1;
if (SearchString != null && !SearchString.Equals(""))
{
try
{
Regex r;
r = new Regex(SearchString, RegexOptions.IgnoreCase);
int startAt = this.SelectionStart + 1;
if (startAt > this.Text.Length)
startAt = 0;
Match m = r.Match(this.Text, startAt);
if (m.Length == 0)
{ // continue at the top if not found
m = r.Match(this.Text);
}
if (m.Length > 0)
{
this.SelectionStart = m.Index;
this.SelectionLength = m.Length;
retVal = this.GetLineFromCharIndex(this.SelectionStart);
}
}
catch { }
}
UpdateStatusControl(retVal);
return retVal;
}
/// <summary>
/// Gets the line the caret is on.
/// </summary>
/// <returns></returns>
public string GetCurrentLine()
{
string retVal = "";
int ls = GetLineStart();
int le = GetLineEnd();
int length = le - ls + 1;
if (length > -1)
retVal = this.Text.Substring(ls, length);
return retVal;
}
/// <summary>
/// Cuts the current line of text.
/// </summary>
private void CutLine()
{
int ls = GetLineStart();
int le = GetLineEnd();
int length = le - ls + 1;
if (length > -1)
{
this.SelectionStart = ls;
this.SelectionLength = length;
this.Cut();
}
}
/// <summary>
/// returns the position of the start of the current line.
/// </summary>
/// <returns></returns>
private int GetLineStart()
{
int i = 0;
int textPosition = this.SelectionStart;
if (textPosition >= this.Text.Length)
{
textPosition--;
}
int lineStart = 0;
for (i = textPosition; i > 0; i--)
{
if (this.Text[i] == '\n')
{
lineStart = i + 1;
break;
}
}
return lineStart;
}
/// <summary>
/// returns the position of the end of the current line.
/// </summary>
/// <returns></returns>
private int GetLineEnd()
{
// int ret = 0;
int i = this.SelectionStart;
if (i >= this.Text.Length)
{
return this.Text.Length - 1;
}
char current = this.Text[i];
while (i < this.Text.Length /*&& current != ' ' &&
current != ',' */
&& current != '\n')
{
// ret++;
i++;
current = this.Text[i];
}
return i;
}
/// <summary>
/// Prompts the user for a string to search for
/// </summary>
/// <returns>true if the search string was set</returns>
public bool SetSearchString()
{
bool ret = false;
if (this.SelectionLength > 0)
{
SearchString = this.Text.Substring(this.SelectionStart, this.SelectionLength);
}
string result = StringInputDlg.GetString(
"Enter Search String",
"Please enter text (or a regex) to search for.",
SearchString);
if (!result.Equals(""))
{
SearchString = result;
ret = true;
}
return ret;
}
}
}