-
Notifications
You must be signed in to change notification settings - Fork 1
/
SourceForm.cs
493 lines (430 loc) · 17.3 KB
/
SourceForm.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//********************************************************************************************
//Author: Sergey Stoyan, CliverSoft.com
// http://cliversoft.com
// stoyan@cliversoft.com
// sergey.stoyan@gmail.com
// 17 February 2008
//Copyright: (C) 2008, Sergey Stoyan
//********************************************************************************************
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
namespace Cliver.DataSifter
{
/// <summary>
/// Form that hosts parsed text and filter tree editor control
/// </summary>
internal partial class SourceForm : Form//
{
#region initializing
string title = Program.FullName;
internal static readonly SourceForm This;
static SourceForm()
{
This = new SourceForm();
}
static internal readonly Color SplitterColor = Color.Gray;
SourceForm()
{
InitializeComponent();
Icon = Win.AssemblyRoutines.GetAppIcon();
set_tool_tip();
splitContainer1.Panel2.Controls.Add(FilterTreeForm.This);
FilterTreeForm.This.Dock = DockStyle.Fill;
splitContainer1.BackColor = SplitterColor;
splitContainer1.Panel1.BackColor = SystemColors.Control;
splitContainer1.Panel2.BackColor = SystemColors.Control;
WrapText.CheckedChanged += delegate
{
TextBox.WordWrap = WrapText.Checked;
};
WrapText.Checked = Settings.General.WrapText;
TextBox.WordWrap = WrapText.Checked;
Document.DocumentUpdated += new Document.DocumentUpdatedEventHandler(Document_DocumentUpdated);
Document.SetTestText();
//TreeForm_Click(null, null);
if (System.IO.File.Exists(Settings.History.LastSourceFile))
Document.LoadFromFile(Settings.History.LastSourceFile);
string fltr_file = null;
foreach (string a in Environment.GetCommandLineArgs())
{
if (Regex.IsMatch(a, @"\." + Program.FilterTreeFileExtension + @"$", RegexOptions.IgnoreCase))
{
fltr_file = a;
break;
}
}
if (fltr_file == null)
{
if (System.IO.File.Exists(Settings.History.LastFilterTreeFile))
fltr_file = Settings.History.LastFilterTreeFile;
}
FilterTreeForm.This.LoadFilterTree(fltr_file);
}
void Document_DocumentUpdated()
{
ignore_selection_changed = true;
//TextBox is used to display parse results
TextBox.Rtf = null;
TextBox.Text = Document.Text;
count_positions_ignored_by_rich_text_box();
Text = Document.Title + " - " + title;
CaptureLabels.Clear();
fill_NavigateBy(true);
////set rtf header
//StringBuilder rtf = new StringBuilder(TextBox.Rtf);
////get the needed font declared in the head
//TextBox.Text = "12";
//TextBox.Select(1, 1);
//TextBox.SelectionFont = Settings.Default.CaptureLabelFont;
//string rtf2 = TextBox.Rtf;
//string head = rtf2.Substring(0, rtf2.IndexOf("\r\n"));
////create color table
//Color html_tags_color = Settings.Default.HtmlTagsColor;
//Color html_comment_color = Settings.Default.HtmlCommentColor;
//string colortb = string.Format(@"{{\colortbl;\red{0}\green{1}\blue{2};\red{3}\green{4}\blue{5};}}",
// html_comment_color.R, html_comment_color.B, html_comment_color.G,
// html_tags_color.R, html_tags_color.B, html_tags_color.G);
//head += colortb;
//rtf.Remove(0, RtfEditor.IndexOf(rtf, "\r\n"));
//rtf.Insert(0, head);
if (Settings.Appearance.HighlightHtmlTags)
{ //set rtf header
StringBuilder rtf = new StringBuilder(TextBox.Rtf);
Color html_tags_color = Settings.Appearance.HtmlTagsColor;
Color html_comment_color = Settings.Appearance.HtmlCommentColor;
Color html_javascript_color = Settings.Appearance.HtmlJavascriptColor;
string colortb = string.Format(@"{{\colortbl;\red{0}\green{1}\blue{2};\red{3}\green{4}\blue{5};\red{6}\green{7}\blue{8};}}",
html_comment_color.R, html_comment_color.G, html_comment_color.B,
html_tags_color.R, html_tags_color.G, html_tags_color.B,
html_javascript_color.R, html_javascript_color.G, html_javascript_color.B);
rtf.Insert(RtfEditor.IndexOf(rtf, "\r\n"), colortb);
int rtf_length = rtf.Length;
for (int i = 0; i < rtf_length; i++)
{
if (rtf[i] != '<')
continue;
if (RtfEditor.IsEqualIgnoringCase(rtf, i + 1, "!--"))
{
rtf.Insert(i, @"\cf1 ");
i += 5;
rtf_length += 5;
int tag_end = RtfEditor.IndexOf(rtf, "-->", i);
if (tag_end >= 0)
{
rtf.Insert(tag_end + 1, @"\cf0 ");
i = tag_end + 5;
rtf_length += 5;
}
}
else if (RtfEditor.IsEqualIgnoringCase(rtf, i + 1, "script"))
{
rtf.Insert(i, @"\cf2 ");
i += 5;
rtf_length += 5;
int tag_end = RtfEditor.IndexOf(rtf, ">", i);
if (tag_end >= 0)
{
rtf.Insert(tag_end + 1, @"\cf3 ");
i = tag_end + 5;
rtf_length += 5;
}
}
else
{
rtf.Insert(i, @"\cf2 ");
i += 5;
rtf_length += 5;
int tag_end = RtfEditor.IndexOf(rtf, ">", i);
if (tag_end >= 0)
{
rtf.Insert(tag_end + 1, @"\cf0 ");
i = tag_end + 5;
rtf_length += 5;
}
}
}
TextBox.Rtf = rtf.ToString();
}
ignore_selection_changed = false;
}
/// <summary>
/// RichTextBox takes '\r\n' as a single char so it counts them to restore a correct position
/// </summary>
void count_positions_ignored_by_rich_text_box()
{
positions_ignored_by_rich_text_box.Clear();
MatchCollection mc = Regex.Matches(Document.Text, @"\r{1,2}\n", RegexOptions.Compiled | RegexOptions.Singleline);
foreach (Match m in mc)
for (int i = m.Length - 1; i > 0; i--)
positions_ignored_by_rich_text_box.Add(m.Index + 1);
}
List<int> positions_ignored_by_rich_text_box = new List<int>();
private void SourceForm_Load(object sender, EventArgs e)
{
try
{
NavigateBy.DisplayMember = "name";
NavigateBy.ValueMember = "value";
FilterTreeForm.This.Show();
}
catch (Exception ex)
{
Message.Error(ex);
}
}
#endregion
#region parsing
/// <summary>
/// start parsing by the current debug regex chain
/// </summary>
/// <returns></returns>
internal bool RunParse()
{
ignore_selection_changed = true;
PrevMark.Enabled = true;
NextMark.Enabled = true;
if (current_capture_label_index >= 0 && current_capture_label_index < CaptureLabels.Count)
unmark_capture_branch(CaptureLabels[current_capture_label_index]);
current_capture_label_index = -1;
current_path_capture_count = 0;
fill_NavigateBy(true);
CaptureLabels.Clear();
TextBox.SelectAll();
TextBox.SelectionBackColor = Color.White;
TextBox.DeselectAll();
//RefreshDocument();
parser = FilterTreeForm.This.GetFilterChainParser();
if (parser.RootFilters == null || parser.RootFilters.Length < 1)
return false;
//page = TextBox.Text;
ThreadStart ts = new ThreadStart(parse);
parse_thread = new Thread(ts);
parse_thread.IsBackground = true;
parse_thread.Start();
return true;
}
/// <summary>
/// breaks parsing thread and stops parsing
/// </summary>
internal void StopParse()
{
if (parse_thread != null)
{
if (parse_thread.IsAlive)
parse_thread.Abort();
parse_thread = null;
System.GC.Collect();
}
FilterTreeForm.This.buttonParse.Checked = false;
ignore_selection_changed = false;
}
Thread parse_thread;
Parser parser;
//used by OutputForm
internal DateTime FilterTreeParsedTime;
//used by OutputForm
internal Capture GroupCapture0 = null;
//used by OutputForm
internal DateTime FilterTreeChangedTime;
//used by OutputForm
internal Parser ParserForCurrentChain
{
get
{
return parser;
}
}
void parse()
{
try
{
GroupCapture0 = parser.Parse(Document.Text);
FilterTreeParsedTime = DateTime.Now;
ControlRoutines.Invoke(this, () =>
{
TextBox.DeselectAll();
FilterTreeForm.This.buttonParse.Checked = false;
fill_NavigateBy(false);
});
}
catch (ThreadAbortException)
{
Thread.ResetAbort();
}
catch (Exception ex)
{
Message.Error(ex);
}
ignore_selection_changed = false;
}
#endregion
#region handlers
private void File_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog d = new OpenFileDialog();
d.Title = "Pick a file to be parsed";
d.InitialDirectory = get_corresponding_source_folder(Settings.History.LastFilterTreeFile);
if (string.IsNullOrWhiteSpace(d.InitialDirectory) || !Directory.Exists(d.InitialDirectory))
d.InitialDirectory = null;
if (d.ShowDialog(this) != DialogResult.OK || string.IsNullOrWhiteSpace(d.FileName))
return;
Settings.History.LastSourceFile = d.FileName;
Settings.History.Save();
Document.LoadFromFile(d.FileName);
}
catch (Exception ex)
{
Message.Error(ex);
}
}
string get_corresponding_source_folder(string filter_tree_file)
{
if (string.IsNullOrWhiteSpace(filter_tree_file))
return "";
string ft_folder = Path.GetDirectoryName(filter_tree_file);
if (Settings.History.FilterTreeFolders2SourceFolder.Contains(ft_folder))
return (string)Settings.History.FilterTreeFolders2SourceFolder[ft_folder];
return Path.GetDirectoryName(Settings.History.LastSourceFile);
}
private void About_Click(object sender, EventArgs e)
{
AboutForm a = new AboutForm();
a.ShowDialog();
}
private void Help_Click(object sender, EventArgs e)
{
Program.Help();
}
private void TreeForm_Click(object sender, EventArgs e)
{
//this.Location = new Point(0, 0);
//this.Height = Screen.PrimaryScreen.WorkingArea.Height - FilterTreeForm.This.Height;
//this.Width = Screen.PrimaryScreen.WorkingArea.Width;
//this.WindowState = FormWindowState.Normal;
//FilterTreeForm.This.Location = new Point(0, Screen.PrimaryScreen.WorkingArea.Height - FilterTreeForm.This.Height);
//FilterTreeForm.This.Width = Screen.PrimaryScreen.WorkingArea.Width;
//FilterTreeForm.This.WindowState = FormWindowState.Normal;
//FilterTreeForm.This.Visible = true;
//FilterTreeForm.This.Activate();
////FilterTreeForm.This.Focus();
}
private void cHideFilters_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.Panel2Collapsed = cHideFilters.Checked;
cHideFilters.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
private void SourceForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (FilterTreeForm.This.ToSavePreviousFilterTree())
return;
if (!Message.YesNo("Exiting " + Application.ProductName + ". Proceed?"))
{
e.Cancel = true;
return;
}
Settings.General.WrapText = WrapText.Checked;
Settings.General.Save();
This.Dispose();
}
internal void ReadFilterTreeXmlFile(string filter_tree_xml_file)
{
FilterTreeForm.This.LoadFilterTree(filter_tree_xml_file);
}
private void Abort_Click(object sender, EventArgs e)
{
if (parse_thread == null)
return;
parse_thread.Abort();
//RefreshDocument();
}
private void SourceForm_Activated(object sender, EventArgs e)
{
}
private void TextBox_SelectionChanged(object sender, EventArgs e)
{
if (ignore_selection_changed)
return;
set_status_by_position(TextBox.SelectionStart, TextBox.SelectionLength);
if (!Settings.General.CopySelectionToClipboard)
return;
TextBox.Copy();
}
bool ignore_selection_changed = true;
private void bSettings_Click(object sender, EventArgs e)
{
SettingsForm sf = new SettingsForm();
sf.ShowDialog(this);
}
internal void SetStatus(string status)
{
if (string.IsNullOrEmpty(status))
Status.Text = " ";
else
Status.Text = status;
}
/// <summary>
/// selects the complete captured group within the parsed text box by double click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBox_DoubleClick(object sender, EventArgs e)
{
if (CaptureLabels == null)
return;
if (current_capture_label_index < 0 || current_capture_label_index >= CaptureLabels.Count)
{
SetStatus("");
return;
}
CaptureLabel owner_cl = null;
int start = TextBox.SelectionStart;
for (CaptureLabel c = CaptureLabels[current_capture_label_index]; c != null; c = c.parent)
{
if (!c.visible)
continue;
if (start >= c.start3 && (owner_cl == null || c.level > owner_cl.level))
owner_cl = c;
}
if (owner_cl == null)
return;
TextBox.SelectionStart = owner_cl.start3;
TextBox.SelectionLength = owner_cl.end3 - owner_cl.start3;
}
static ThreadRunner tw;
private void NavigateBy_DropDown(object sender, EventArgs e)
{
int width = 0;
Graphics g = NavigateBy.CreateGraphics();
Font font = NavigateBy.Font;
foreach (string s in NavigateBy.Items)
{
int w = (int)g.MeasureString(s, font).Width;
if (w > width)
width = w;
}
int vert_scroll_bar_width = (NavigateBy.Items.Count > NavigateBy.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
NavigateBy.DropDownWidth = width + vert_scroll_bar_width + 10;
}
private void NavigateBy_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
return;
Brush brush;
OutputGroup og = (OutputGroup)output_groups[e.Index];
brush = new SolidBrush(Settings.Appearance.GetFilterBackColor(og.Level));
e.Graphics.FillRectangle(brush, e.Bounds);
brush = new SolidBrush(e.ForeColor);
e.Graphics.DrawString((string)((ComboBox)sender).Items[e.Index], e.Font, brush, e.Bounds);
e.DrawFocusRectangle();
}
#endregion
}
}