-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchWindow.xaml.cs
322 lines (294 loc) · 13.6 KB
/
SearchWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Media;
namespace MSGEdit {
/// <summary>
/// Логика взаимодействия для SearchWindow.xaml
/// </summary>
public partial class SearchWindow : Window {
public int last_found_index = -1; // we use this value as a start index <=> it != -1
public int first_found_index; // when we reach this index, we stop searching
public Key[] allowed_keys; // assigned to from the MainWindow
public SearchWindow() {
InitializeComponent();
// maybe initialize its owner somewhere?
DataObject.AddPastingHandler(start_index_textbox, OnPaste);
}
private void Search_window_Closed(object sender, EventArgs e) {
MainWindow owner = this.Owner as MainWindow;
owner.is_search_window_shown = false;
owner.Activate();
}
private void Search_window_Loaded(object sender, RoutedEventArgs e) {
MainWindow owner = this.Owner as MainWindow;
if (owner.settings.autopaste_from_clipboard) {
string prompt;
if (Clipboard.ContainsText() && !(prompt = Clipboard.GetText()).Contains('\n')) {
// If the clipboard contains text and this text does not contain '\n', we use it as a prompt
search_textbox.Text = prompt;
search_textbox.Focus();
search_textbox.SelectAll();
}
else {
if (owner.EntryStringTextBox.SelectionLength != 0) {
search_textbox.Text = owner.EntryStringTextBox.SelectedText;
search_textbox.Focus();
search_textbox.SelectAll();
}
}
}
else {
if (owner.EntryStringTextBox.SelectionLength != 0) {
search_textbox.Text = owner.EntryStringTextBox.SelectedText;
search_textbox.Focus();
search_textbox.SelectAll();
}
}
}
private void Find_next_button_Click(object sender, RoutedEventArgs e) {
if (search_textbox.Text == "") {
MessageBox.Show("The search field is empty.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
MainWindow owner = this.Owner as MainWindow;
Tuple<int, int> res;
// evaluate the first needed index
int start = 0;
if (last_found_index != -1) { // the search operation is "find next"
start = last_found_index;
// now we need to mofidy it in accordance with the search parameters and current core state
if (search_backwards_checkbox.IsChecked == true) {
if (start == 0) {
if (cycle_search_checkbox.IsChecked == true) {
start = owner.core.Count - 1;
}
else {
// we are done and cannot proceed
last_found_index = -1;
MessageBox.Show("Nothing else found.", "Information",
MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
}
else {
--start;
}
}
else {
if (start == owner.core.Count - 1) {
if (cycle_search_checkbox.IsChecked == true) {
start = 0;
}
else {
// we are done and cannot proceed
last_found_index = -1;
MessageBox.Show("Nothing else found.", "Information",
MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
}
else {
++start;
}
}
// now it may turn out that the start is not a correct index
if (start > owner.core.Count) {
if (MessageBox.Show("The data has been modified in between calls to the find operation. The next attempt will start over.",
"Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.Cancel) {
}
last_found_index = -1;
return;
}
}
else { // the search operation is "find first"
// start over
if (search_starts_from_selected_index_checkbox.IsChecked == true) {
if (owner.FileContentListBox.SelectedIndex != -1) {
// we start the search from selected index
start = owner.FileContentListBox.SelectedIndex;
}
else {
start = 0; // exceptional case.
}
}
else {
// else we don't...
try {
start = Convert.ToInt32(start_index_textbox.Text);
}
catch (Exception except) {
MessageBox.Show($"Start index format error: {except.Message}", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
}
// debug
//MessageBox.Show($"Debug: starting from {start}");
if (regex_checkbox.IsChecked == true) {
res = owner.core.FindRegex(search_textbox.Text, start, !search_backwards_checkbox.IsChecked.Value,
cycle_search_checkbox.IsChecked.Value);
}
else {
res = owner.core.Find(search_textbox.Text, start, case_sensitive_checkbox.IsChecked.Value,
whole_words_checkbox.IsChecked.Value, !search_backwards_checkbox.IsChecked.Value,
cycle_search_checkbox.IsChecked.Value);
}
if (owner.core.LastError != "") {
MessageBox.Show($"Search error: {owner.core.LastError}", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (res == null) {
// Not found
MessageBox.Show($"...but nothing found.", "Success...", MessageBoxButton.OK, MessageBoxImage.Information);
}
else {
// Found
if (last_found_index == -1) { // find first
first_found_index = res.Item1;
}
else { // find next
if (first_found_index == res.Item1) {
MessageBox.Show("The search has reached its starting point", "Information",
MessageBoxButton.OK, MessageBoxImage.Information);
last_found_index = -1;
owner.EntryIndexTextBox.Text = res.Item1.ToString();
return;
}
}
last_found_index = res.Item1;
owner.EntryIndexTextBox.Text = last_found_index.ToString();
}
}
private void Find_all_button_Click(object sender, RoutedEventArgs e) {
if (search_textbox.Text == "") {
MessageBox.Show("The search field is empty.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
last_found_index = -1;
MainWindow owner = this.Owner as MainWindow;
List<Tuple<int, int>> res;
if (regex_checkbox.IsChecked == true) {
res = owner.core.FindRegexAll(search_textbox.Text);
}
else {
res = owner.core.FindAll(
search_textbox.Text,
case_sensitive_checkbox.IsChecked.Value,
whole_words_checkbox.IsChecked.Value
);
}
if (owner.core.LastError != "") {
MessageBox.Show($"Search error: {owner.core.LastError}", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
search_results_combobox.Items.Clear();
if (res.Count != 0) {
for (int i = 0; i < res.Count; ++i) {
search_results_combobox.Items.Add($"{res[i].Item1}"); // in this case the position ends up being unused.
}
if (res.Count == 1) {
MessageBox.Show($"Found 1 message.", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
}
else {
MessageBox.Show($"Found {res.Count} messages.", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
else {
MessageBox.Show($"...but nothing found.", "Success...", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void Help_button_Click(object sender, RoutedEventArgs e) {
MainWindow owner = this.Owner as MainWindow;
owner.show_about_window();
}
private void Cancel_button_Click(object sender, RoutedEventArgs e) {
this.Close();
}
private void Search_start_checkbox_Checked(object sender, RoutedEventArgs e) {
if (start_index_textbox == null)
return;
if (start_index_textblock == null)
return;
start_index_textbox.IsEnabled = false;
start_index_textblock.IsEnabled = false;
}
private void Search_start_checkbox_Unchecked(object sender, RoutedEventArgs e) {
start_index_textbox.IsEnabled = true;
start_index_textblock.IsEnabled = true;
}
private void Regex_checkbox_Checked(object sender, RoutedEventArgs e) {
whole_words_checkbox.IsEnabled = false;
case_sensitive_checkbox.IsEnabled = false;
}
private void Regex_checkbox_Unchecked(object sender, RoutedEventArgs e) {
whole_words_checkbox.IsEnabled = true;
case_sensitive_checkbox.IsEnabled = true;
}
private void Search_results_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
if (search_results_combobox.SelectedIndex == -1) {
return;
}
MainWindow owner = this.Owner as MainWindow;
owner.EntryIndexTextBox.Text = search_results_combobox.SelectedItem as string;
}
private void Search_textbox_TextChanged(object sender, TextChangedEventArgs e) {
last_found_index = -1; // the search must be restarted
}
private void Start_index_textbox_TextChanged(object sender, TextChangedEventArgs e) {
last_found_index = -1; // the search must be restarted
}
private void OnPaste(object sender, DataObjectPastingEventArgs e) {
var text = e.SourceDataObject.GetData(DataFormats.UnicodeText);
if (!(text as string).All(char.IsDigit)) {
e.CancelCommand();
}
}
private void FilterInput(object sender, KeyEventArgs e) {
if (!allowed_keys.Contains(e.Key)) {
// If this key is not in our whitelist
if (!(
(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) &&
(Keyboard.IsKeyDown(Key.C) || Keyboard.IsKeyDown(Key.V) || Keyboard.IsKeyDown(Key.Z) || Keyboard.IsKeyDown(Key.Y))
)) {
// not "Ctrl + C", not "Ctrl + V", not "Ctrl + Z" and not "Ctrl + Y"
e.Handled = true;
}
}
}
private void Search_window_PreviewKeyDown(object sender, KeyEventArgs e) {
//switch (e.Key) {
// case Key.F5: {
// // Regular search
// find_next_button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
// break;
// }
// case Key.F7: {
// // Force restart
// last_found_index = -1;
// find_next_button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
// break;
// }
//}
if (e.Key == Key.F5) {
// Regular search
find_next_button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}
if (e.Key == Key.F1) {
MainWindow owner = this.Owner as MainWindow;
owner.show_about_window();
}
}
}
}