-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSearchForm.cs
359 lines (297 loc) · 10.4 KB
/
SearchForm.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace Grepy2
{
public partial class SearchForm : Form
{
const int NUM_SEARCH_HISTORY = 30;
const int NUM_FILESPEC_HISTORY = 20;
const int NUM_FOLDER_HISTORY = 20;
public string SearchFor
{
get { return this.SearchForComboBox.Text; }
}
public string FileSpec
{
get { return this.FileSpecComboBox.Text.Trim(); }
}
public string FolderName
{
get { return this.FolderComboBox.Text.Trim(); }
}
public bool bRegularExpression
{
get { return this.RegExCheckBox.Checked; }
}
public bool bMatchCase
{
get { return MatchCaseCheckBox.Checked; }
}
public bool bIsRecursive
{
get { return this.RecurseFolderCheckBox.Checked; }
}
private bool bWindowInitComplete; // set when form window is done initializing
private SearchHelpForm helpForm;
public SearchForm()
{
bWindowInitComplete = false; // we aren't done initializing the window yet, don't overwrite any .config settings
this.DialogResult = DialogResult.Cancel; // set the default dialog result to 'Cancel'
helpForm = new SearchHelpForm();
InitializeComponent();
int pos_x = -1;
int pos_y = -1;
if( Config.Get(Config.KEY.SearchPosX, ref pos_x) && Config.Get(Config.KEY.SearchPosY, ref pos_y) )
{
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(pos_x, pos_y);
}
else // otherwise, center the window on the parent form
{
this.StartPosition = FormStartPosition.CenterParent;
}
}
private void SearchForm_Load(object sender, EventArgs e)
{
bool bConfigRegularExpression = false;
if( Config.Get(Config.KEY.SearchRegularExpression, ref bConfigRegularExpression) )
{
RegExCheckBox.Checked = bConfigRegularExpression;
}
bool bConfigMatchCase = false;
if( Config.Get(Config.KEY.SearchMatchCase, ref bConfigMatchCase) )
{
MatchCaseCheckBox.Checked = bConfigMatchCase;
}
bool bConfigRecursive = false;
if( Config.Get(Config.KEY.SearchRecursive, ref bConfigRecursive) )
{
RecurseFolderCheckBox.Checked = bConfigRecursive;
}
List<string> SearchForList = new List<string>();
if( Config.Get(Config.KEY.SearchText, ref SearchForList) )
{
for( int i = 0; i < SearchForList.Count; i++ )
{
int length = SearchForList[i].Length;
if( (SearchForList[i].Substring(0,1) == "\"") || (SearchForList[i].Substring(length-1,1) == "\"") )
{
string value = SearchForList[i].Substring(1, length-2); // chop off the leading and trailing double quote
SearchForComboBox.Items.Add(value);
if( i == 0 ) // automatically populate the search text box with the most recent config setting
{
SearchForComboBox.Text = value;
}
}
}
}
List<string> FileSpecList = new List<string>();
if( Config.Get(Config.KEY.SearchFileSpec, ref FileSpecList) )
{
for( int i = 0; i < FileSpecList.Count; i++ )
{
FileSpecComboBox.Items.Add(FileSpecList[i]);
if( i == 0 ) // automatically populate the file spec box with the most recent config setting
{
FileSpecComboBox.Text = FileSpecList[i];
}
}
}
List<string> FolderList = new List<string>();
if( Config.Get(Config.KEY.SearchFolder, ref FolderList) )
{
for( int i = 0; i < FolderList.Count; i++ )
{
FolderComboBox.Items.Add(FolderList[i]);
if( i == 0 ) // automatically populate the search folder box with the most recent config setting (or with the commandline search folder)
{
FolderComboBox.Text = FolderList[i];
}
}
}
// if we have specified a search folder as the only argument (like when right clicking on a folder)...
if( Globals.SearchDirectory != "" )
{
FolderComboBox.Text = Globals.SearchDirectory; // populate the search folder box with that folder
}
ToolTip SearchToolTip = new ToolTip();
SearchToolTip.AutomaticDelay = 1000;
SearchToolTip.SetToolTip(this.RegExCheckBox, "Check to enable searching with regular expressions.");
SearchToolTip.SetToolTip(this.MatchCaseCheckBox, "Check to enable searching with a case sensitive search.");
SearchToolTip.SetToolTip(this.RecurseFolderCheckBox, "Check to search files in the specified folder and files in all sub-folders.");
SearchToolTip.SetToolTip(this.SearchForComboBox, "Type in the text you wish to search for here.");
SearchToolTip.SetToolTip(this.FileSpecComboBox, "Type in the file specifications you wish to use when collecting the files to search.");
SearchToolTip.SetToolTip(this.FolderComboBox, "Type in the folder name you wish to start searching from (or click the \"...\" button to browse to a folder).");
}
private void SearchForm_Shown(object sender, EventArgs e)
{
bWindowInitComplete = true; // window initialization is complete, okay to write config settings now
}
private void SearchForm_Closing(object sender, FormClosingEventArgs e)
{
helpForm.Close();
}
private void SearchForm_Move(object sender, EventArgs e)
{
if( bWindowInitComplete )
{
Config.Set(Config.KEY.SearchPosX, Location.X);
Config.Set(Config.KEY.SearchPosY, Location.Y);
}
}
private void SaveConfig()
{
Config.Set(Config.KEY.SearchRegularExpression, RegExCheckBox.Checked);
Config.Set(Config.KEY.SearchMatchCase, MatchCaseCheckBox.Checked);
Config.Set(Config.KEY.SearchRecursive, RecurseFolderCheckBox.Checked);
// get the list of search text items from the combo box
List<string> SearchForComboBoxList = SearchForComboBox.Items.Cast<string>().ToList();
// if the combo box SearchFor text is already in that list, remove it from the list
int searchForIndex = SearchForComboBoxList.IndexOf(SearchFor);
if( searchForIndex != -1 )
{
SearchForComboBoxList.RemoveAt(searchForIndex); // remove duplicate
}
// if the combo box SearchFor text is already at the maximum number of elements, remove the last one
if( SearchForComboBoxList.Count >= NUM_SEARCH_HISTORY )
{
SearchForComboBoxList.RemoveAt(NUM_SEARCH_HISTORY - 1); // prune the list
}
// add the new SearchFor text to the start of the combo box list
SearchForComboBoxList.Insert(0, SearchFor);
// add the double quotes around the search text list
List<string> SearchTextList = new List<string>();
for( int i = 0; i < SearchForComboBoxList.Count; i++ )
{
SearchTextList.Add("\"" + SearchForComboBoxList[i] + "\"");
}
// save the search text list to the config file
Config.Set(Config.KEY.SearchText, SearchTextList);
// do the same for the FileSpec combo box
List<string> FileSpecComboBoxList = FileSpecComboBox.Items.Cast<string>().ToList();
int fileSpecIndex = FileSpecComboBoxList.IndexOf(FileSpec);
if( fileSpecIndex != -1 )
{
FileSpecComboBoxList.RemoveAt(fileSpecIndex); // remove duplicate
}
if( FileSpecComboBoxList.Count >= NUM_FILESPEC_HISTORY )
{
FileSpecComboBoxList.RemoveAt(NUM_FILESPEC_HISTORY - 1); // prune the list
}
FileSpecComboBoxList.Insert(0, FileSpec);
// we don't need to add double quotes to file specs
Config.Set(Config.KEY.SearchFileSpec, FileSpecComboBoxList);
// do the same for the Folder combo box
List<string> FolderComboBoxList = FolderComboBox.Items.Cast<string>().ToList();
int folderIndex = FolderComboBoxList.IndexOf(FolderName);
if( folderIndex != -1 )
{
FolderComboBoxList.RemoveAt(folderIndex); // remove duplicate
}
if( FolderComboBoxList.Count >= NUM_FOLDER_HISTORY )
{
FolderComboBoxList.RemoveAt(NUM_FOLDER_HISTORY - 1); // prune the list
}
FolderComboBoxList.Insert(0, FolderName);
// we don't need to add double quotes to folder names
Config.Set(Config.KEY.SearchFolder, FolderComboBoxList);
}
private void ValidateInput()
{
if( SearchFor == "" ) // make sure text to search for has been provided
{
MessageBox.Show("'Search For' is empty. You must provide text to search for.\n", "Invalid Search String");
return;
}
if( FileSpecComboBox.Text == "" ) // make sure a file specification is provided
{
MessageBox.Show("'File Specification' is empty. You must provide a file specification (or *.*).\n", "Invalid FileSpec");
return;
}
if( FolderName == "" ) // make sure a folder to search has been provided
{
MessageBox.Show("'Folder To Search' is empty. You must provide a folder to search.\n", "Invalid Folder To Search");
return;
}
if( !FolderName.StartsWith("\\\\") && !Directory.Exists(FolderName) )
{
MessageBox.Show("'Folder To Search' directory does not exist.\n", "Invalid Directory");
return;
}
if( FolderName.EndsWith(":") )
{
FolderComboBox.Text = FolderName + "\\";
}
if( RegExCheckBox.Checked ) // if using Regular Expression search, make sure the search text is a valid regular expression
{
try
{
Regex SearchRegEx = new Regex(SearchFor, RegexOptions.IgnoreCase);
}
catch
{
MessageBox.Show("The 'Search For' string is not a valid Regular Expression (See 'Help').\n(Correct the regular expression or uncheck the 'Regular Expression' checkbox)", "Invalid Regular Expression");
return;
}
}
SaveConfig();
this.DialogResult = DialogResult.OK;
Close();
}
private void FoldersButton_Click(object sender, EventArgs e)
{
FolderSelectDialog FolderSelect = new FolderSelectDialog();
if( (FolderName != "") && Directory.Exists(FolderName) )
{
FolderSelect.InitialDirectory = FolderName;
}
else
{
FolderSelect.InitialDirectory = "";
}
if( FolderSelect.ShowDialog() && (FolderSelect.FileName != "") )
{
FolderComboBox.Text = FolderSelect.FileName;
}
}
private void SearchOkButton_Click(object sender, EventArgs e)
{
ValidateInput();
}
private void SearchHelpButton_Click(object sender, EventArgs e)
{
helpForm.Show(this);
}
private void SearchCancelButton_Click(object sender, EventArgs e)
{
Close();
}
private void SearchForComboBox_KeyDown(object sender, KeyEventArgs e)
{
if( e.KeyCode == Keys.Enter )
{
ValidateInput();
}
}
private void FileSpecComboBox_KeyDown(object sender, KeyEventArgs e)
{
if( e.KeyCode == Keys.Enter )
{
ValidateInput();
}
}
private void FolderComboBox_KeyDown(object sender, KeyEventArgs e)
{
if( e.KeyCode == Keys.Enter )
{
ValidateInput();
}
}
}
}