-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathArchiveChooser.cs
More file actions
246 lines (217 loc) · 5.59 KB
/
ArchiveChooser.cs
File metadata and controls
246 lines (217 loc) · 5.59 KB
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
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.EmuHawk
{
public partial class ArchiveChooser : FormBase
{
protected override string WindowTitleStatic => "Choose File From Archive";
private readonly IList<ListViewItem> _archiveItems = new List<ListViewItem>();
private readonly ToolTip _errorBalloon = new ToolTip();
private static bool _useRegEx;
private static bool _matchWhileTyping = true;
public ArchiveChooser(HawkFile hawkFile)
{
InitializeComponent();
_errorBalloon.IsBalloon = true;
_errorBalloon.InitialDelay = 0;
radRegEx.Checked = _useRegEx;
cbInstantFilter.Checked = _matchWhileTyping;
var items = hawkFile.ArchiveItems;
for (int i = 0; i < items.Count; i++)
{
var item = items[i];
var lvi = new ListViewItem { Tag = i };
lvi.SubItems.Add(new ListViewItem.ListViewSubItem());
lvi.Text = item.Name;
long size = item.Size;
if (size % 1024 is 16 && Path.GetExtension(item.Name)?.EqualsIgnoreCase(".NES") is true) size -= 16;
lvi.SubItems[1].Text = Util.FormatFileSize(size);
_archiveItems.Add(lvi);
}
InitializeFileView();
}
private void InitializeFileView()
{
lvMembers.BeginUpdate();
try
{
lvMembers.Items.Clear();
foreach (ListViewItem i in _archiveItems.OrderBy(x => x.Name))
{
lvMembers.Items.Add(i);
}
}
finally
{
lvMembers.EndUpdate();
}
}
public int SelectedMemberIndex
{
get
{
if (lvMembers.SelectedIndices.Count == 0) return -1;
int? ai = lvMembers.SelectedItems[0].Tag as int?;
return ai ?? -1;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
private void lvMembers_ItemActivate(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void ArchiveChooser_Load(object sender, EventArgs e)
{
lvMembers.Items[0].Selected = true;
tbFilter.Select();
}
private void btnSearch_Click(object sender, EventArgs e)
{
StartMatching(tbSearch, DoSearch);
}
private void cbInstantFilter_CheckedChanged(object sender, EventArgs e)
{
_matchWhileTyping = cbInstantFilter.Checked;
}
private void radRegEx_CheckedChanged(object sender, EventArgs e)
{
_useRegEx = radRegEx.Checked;
}
private void tbFilter_TextChanged(object sender, EventArgs e)
{
if (cbInstantFilter.Checked)
{
btnFilter_Click(sender, e);
}
}
private void btnFilter_Click(object sender, EventArgs e)
{
StartMatching(tbFilter, DoFilter);
}
private void StartMatching(TextBox tb, Action<IMatcher> func)
{
try
{
_errorBalloon.Hide(tb);
var searchMatcher = CreateMatcher(tb.Text);
if (searchMatcher != null)
{
func(searchMatcher);
}
}
catch (ArgumentException ex)
{
string errMsg = ex.Message;
errMsg = errMsg.Substring(startIndex: errMsg.IndexOf('-') + 2);
// Balloon is bugged on first invocation
_errorBalloon.Show($"Error parsing RegEx: {errMsg}", tb);
_errorBalloon.Show($"Error parsing RegEx: {errMsg}", tb);
}
}
private void DoSearch(IMatcher searchMatcher)
{
int count = lvMembers.Items.Count;
int searchStartIdx = 0;
if (lvMembers.SelectedItems.Count > 0)
{
searchStartIdx = (lvMembers.SelectedIndices[0] + 1) % count;
}
int? searchResultIdx = null;
for (int i = 0; i < count; ++i)
{
int curIdx = (searchStartIdx + i) % count;
if (searchMatcher.Matches(lvMembers.Items[curIdx]))
{
searchResultIdx = curIdx;
break;
}
}
if (searchResultIdx != null)
{
lvMembers.Select();
lvMembers.Items[searchResultIdx.Value].Selected = true;
}
else
{
// Balloon is bugged on first invocation
_errorBalloon.Show("Could not find search text", tbSearch);
_errorBalloon.Show("Could not find search text", tbSearch);
}
}
private void DoFilter(IMatcher searchMatcher)
{
lvMembers.BeginUpdate();
try
{
lvMembers.Items.Clear();
foreach (ListViewItem item in _archiveItems)
{
if (searchMatcher.Matches(item))
{
lvMembers.Items.Add(item);
}
}
}
finally
{
lvMembers.EndUpdate();
}
}
private interface IMatcher
{
bool Matches(ListViewItem value);
}
private class SimpleMatcher : IMatcher
{
public string[] Keys { get; set; }
public bool Matches(ListViewItem value)
{
string searchedStr = value.Text.ToLowerInvariant();
foreach (string key in Keys)
{
if (!searchedStr.Contains(key))
{
return false;
}
}
return true;
}
}
private class RegExMatcher : IMatcher
{
public Regex Matcher { get; set; }
public bool Matches(ListViewItem value)
{
return Matcher.IsMatch(value.Text);
}
}
private IMatcher CreateMatcher(string searchKey)
{
if (radSimple.Checked)
{
return new SimpleMatcher
{
Keys = searchKey.ToLowerInvariant().Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries), // splits on all whitespace chars
};
}
else
{
return new RegExMatcher { Matcher = new Regex(searchKey, RegexOptions.IgnoreCase) };
}
}
}
}