-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOptionsForm.cs
294 lines (234 loc) · 9.56 KB
/
OptionsForm.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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace Grepy2
{
public partial class OptionsForm : Form
{
public bool DeferRichText
{
get { return this.DeferRichTextCheckBox.Checked; }
}
public bool UseWindowsFileAssociation
{
get { return this.WindowsFileAssociationCheckBox.Checked; }
}
public string CustomEditorText
{
get { return this.CustomEditorTextBox.Text.Trim(); }
}
public int NumberOfWorkerThreads
{
get { return this.WorkerThreadsComboBox.SelectedIndex + 1; }
}
private bool bWindowInitComplete; // set when form window is done initializing
private ExplorerIntegration WindowsExplorerIntegration;
private bool bWindowsExplorerIntegrationAtStart; // save the state of the Windows Explorer Integration check box at startup (so we can tell if it changed)
public Font ListViewFont;
public bool bListViewFontChanged = false;
public Font RichTextBoxFont;
public bool bRichTextBoxFontChanged = false;
public OptionsForm(Font InListViewFont, Font InRichTextBoxFont)
{
bWindowInitComplete = false; // we aren't done initializing the window yet, don't overwrite any .config settings
ListViewFont = InListViewFont;
RichTextBoxFont = InRichTextBoxFont;
this.DialogResult = DialogResult.Cancel; // set the default dialog result to 'Cancel'
InitializeComponent();
int pos_x = -1;
int pos_y = -1;
if( Config.Get(Config.KEY.OptionsPosX, ref pos_x) && Config.Get(Config.KEY.OptionsPosY, 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 OptionsForm_Load(object sender, EventArgs e)
{
WindowsExplorerIntegration = new ExplorerIntegration();
bWindowsExplorerIntegrationAtStart = WindowsExplorerIntegration.IsEnabled("Directory");
WindowsExplorerCheckBox.Checked = bWindowsExplorerIntegrationAtStart;
int NumberOfProcessors = Math.Max(Environment.ProcessorCount - 1, 1); // subtract one from total numberr of logical processors (so we don't max out the CPU)
for( int index = 1; index <= NumberOfProcessors; index++ )
{
WorkerThreadsComboBox.Items.Add(string.Format("{0}", index));
}
bool bDeferRichTextDisplay = false;
Config.Get(Config.KEY.OptionsDeferRichTextDisplay, ref bDeferRichTextDisplay);
DeferRichTextCheckBox.Checked = bDeferRichTextDisplay;
bool bUseWindowsFileAssociation = true;
Config.Get(Config.KEY.OptionsUseWindowsFileAssociation, ref bUseWindowsFileAssociation);
WindowsFileAssociationCheckBox.Checked = bUseWindowsFileAssociation;
CustomEditorTextBox.ReadOnly = bUseWindowsFileAssociation;
CustomEditorButton.Enabled = !bUseWindowsFileAssociation;
string CustomEditorString = "";
if( Config.Get(Config.KEY.OptionsCustomEditor, ref CustomEditorString) )
{
CustomEditorTextBox.Text = CustomEditorString;
}
WorkerThreadsComboBox.SelectedIndex = Globals.NumWorkerThreads - 1;
FileListFontTextBox.Text = string.Format(@"{0} {1} pt {2}", ListViewFont.Name, ListViewFont.Size, ListViewFont.Style.ToString());
SearchResultsFontTextBox.Text = string.Format(@"{0} {1} pt {2}", RichTextBoxFont.Name, RichTextBoxFont.Size, RichTextBoxFont.Style.ToString());
ToolTip OptionsToolTip = new ToolTip();
OptionsToolTip.AutomaticDelay = 500;
OptionsToolTip.SetToolTip(this.WindowsExplorerCheckBox, "Enable or disable having Grepy2 appear in the right-click menu in Windows File Explorer when right-clicking on a folder (or drive).");
OptionsToolTip.SetToolTip(this.label6, "The number of worker threads that you wish to use when searching through files for the search text.");
OptionsToolTip.SetToolTip(this.WorkerThreadsComboBox, "The number of worker threads that you wish to use when searching through files for the search text.");
OptionsToolTip.SetToolTip(this.DeferRichTextCheckBox, "Enable to defer the displaying of search match text in the RichText box until after the search is complete (this can improve performance).");
OptionsToolTip.SetToolTip(this.WindowsFileAssociationCheckBox, "Whether you wish to use the Windows application associated with a file type to edit the file or whether you wish to specify an editor to use to edit all files.");
}
private void OptionsForm_Shown(object sender, EventArgs e)
{
bWindowInitComplete = true; // window initialization is complete, okay to write config settings now
}
private void OptionsForm_Move(object sender, EventArgs e)
{
if( bWindowInitComplete )
{
Config.Set(Config.KEY.OptionsPosX, Location.X);
Config.Set(Config.KEY.OptionsPosY, Location.Y);
}
}
private void SaveConfig()
{
Config.Set(Config.KEY.OptionsDeferRichTextDisplay, DeferRichText);
Config.Set(Config.KEY.OptionsUseWindowsFileAssociation, UseWindowsFileAssociation);
Config.Set(Config.KEY.OptionsCustomEditor, CustomEditorText);
}
private void ValidateInput()
{
// verify that the Custom Editor executable exists
bool bCustomEditorExecutableExists = false;
string CustomEditorString = CustomEditorText;
if( !UseWindowsFileAssociation )
{
if( CustomEditorString == "" )
{
MessageBox.Show("You must specify a custom editor executable filename if you are not using Windows file association.");
return;
}
// if there's double quotes around the executable name, then get just that part (remove the filename, linenumber and other arguments)
if( (CustomEditorString.Substring(0, 1) == "\"") && (CustomEditorString.Length > 1) )
{
int index = CustomEditorString.IndexOf('"', 1);
if( index > 0 )
{
CustomEditorString = CustomEditorString.Substring(1, index - 1);
// check that the executable file exists
if( File.Exists(CustomEditorString) )
{
bCustomEditorExecutableExists = true;
}
}
if( !bCustomEditorExecutableExists )
{
string msg = string.Format("Custom Editor executable '{0}' does not exist\n\n(or double quotes around the executable name are set up incorrectly.)", CustomEditorString);
MessageBox.Show(msg);
return;
}
}
else
{
// otherwise, if there's no double quotes around the executable name, then check for space before arguments
int space_index = CustomEditorString.IndexOf(" ", 0);
if( space_index > 0 )
{
CustomEditorString = CustomEditorString.Substring(0, space_index);
}
if( !File.Exists(CustomEditorString) )
{
string msg = string.Format("Custom Editor executable '{0}' does not exist\n\n(or it is missing double quotes around it or there is no space before the filename or line number arguments.)", CustomEditorString);
MessageBox.Show(msg);
return;
}
}
}
if( bWindowsExplorerIntegrationAtStart != WindowsExplorerCheckBox.Checked )
{
// set windows explorer integration
if( WindowsExplorerIntegration != null )
{
WindowsExplorerIntegration.Set( Globals.ApplicationPathExe, WindowsExplorerCheckBox.Checked );
}
}
if (bListViewFontChanged)
{
Config.Set(Config.KEY.FileListFont, ListViewFont);
}
if (bRichTextBoxFontChanged)
{
Config.Set(Config.KEY.SearchResultsFont, RichTextBoxFont);
}
SaveConfig();
this.DialogResult = DialogResult.OK;
Close();
}
private void OptionsOkButton_Click(object sender, EventArgs e)
{
ValidateInput();
}
private void OptionsHelpButton_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, "Grepy2Help.chm", HelpNavigator.TopicId, "2");
}
private void OptionsCancelButton_Click(object sender, EventArgs e)
{
Close();
}
private void WindowsFileAssociationCheckBox_CheckedChanged(object sender, EventArgs e)
{
CustomEditorTextBox.ReadOnly = WindowsFileAssociationCheckBox.Checked;
CustomEditorButton.Enabled = !WindowsFileAssociationCheckBox.Checked;
}
private void CustomEditorButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Executables (.exe)|*.exe|Command Files (*.cmd)|*.cmd|All Files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
if( openFileDialog.ShowDialog() == DialogResult.OK )
{
CustomEditorTextBox.Text = "\"" + openFileDialog.FileName + "\"";
}
}
private void FileListFontButton_Click(object sender, EventArgs e)
{
FontPicker fontDialog = new FontPicker(ListViewFont);
try
{
if( fontDialog.ShowDialog() != DialogResult.Cancel && (fontDialog.SelectedFont != null) )
{
ListViewFont = fontDialog.SelectedFont;
bListViewFontChanged = true;
FileListFontTextBox.Text = string.Format(@"{0} {1} pt {2}", ListViewFont.Name, ListViewFont.Size, ListViewFont.Style.ToString());
}
}
catch (Exception)
{
MessageBox.Show("Sorry, this is not a TrueType font and can't be used. Please select another font.");
}
}
private void SearchResultsFontButton_Click(object sender, EventArgs e)
{
FontPicker fontDialog = new FontPicker(RichTextBoxFont);
try
{
if( fontDialog.ShowDialog() != DialogResult.Cancel && (fontDialog.SelectedFont != null) )
{
RichTextBoxFont = fontDialog.SelectedFont;
bRichTextBoxFontChanged = true;
SearchResultsFontTextBox.Text = string.Format(@"{0} {1} pt {2}", RichTextBoxFont.Name, RichTextBoxFont.Size, RichTextBoxFont.Style.ToString());
}
}
catch (Exception)
{
MessageBox.Show("Sorry, this is not a TrueType font and can't be used. Please select another font.");
}
}
}
}