-
Notifications
You must be signed in to change notification settings - Fork 4
/
DiffControl.cs
615 lines (492 loc) · 13.8 KB
/
DiffControl.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
namespace Menees.Diffs.Windows.Forms
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Windows.Forms;
using Menees.Windows.Forms;
#endregion
public sealed partial class DiffControl : ExtendedUserControl
{
#region Private Data Members
private readonly FindData findData = new();
private bool showColorLegend = true;
private bool showToolBar = true;
private int currentDiffLine = -1;
#endregion
#region Constructors
public DiffControl()
{
this.InitializeComponent();
this.UpdateButtons();
this.UpdateColors();
DiffOptions.OptionsChanged += this.DiffOptionsChanged;
// We have to manually attach to the GotFocus event because it
// isn't shown by the Event designer. .NET wants us to use the
// Enter event. Unfortunately, the Focused property isn't set
// yet when that event fires, so ActiveView returns the wrong
// information. We really do need the GotFocus event.
this.ViewA.GotFocus += this.View_PositionChanged;
this.ViewB.GotFocus += this.View_PositionChanged;
this.ViewLineDiff.GotFocus += this.View_PositionChanged;
}
#endregion
#region Public Events
public event EventHandler? LineDiffSizeChanged;
public event EventHandler? RecompareNeeded;
public event EventHandler<DifferenceEventArgs>? ShowTextDifferences;
#endregion
#region Public Properties
public bool CanCompareSelectedText => this.ShowTextDifferences != null && this.ViewA.HasSelection && this.ViewB.HasSelection;
public bool CanCopy => this.ActiveView.HasSelection;
public bool CanFind => this.HasText;
public bool CanFindNext => this.HasText && this.HasFindText;
public bool CanFindPrevious => this.HasText && this.HasFindText;
public bool CanGoToFirstDiff => this.HasText && this.ActiveView.CanGoToFirstDiff;
public bool CanGoToLastDiff => this.HasText && this.ActiveView.CanGoToLastDiff;
public bool CanGoToLine => this.HasText && this.ActiveView != this.ViewLineDiff;
public bool CanGoToNextDiff => this.HasText && this.ActiveView.CanGoToNextDiff;
public bool CanGoToPreviousDiff => this.HasText && this.ActiveView.CanGoToPreviousDiff;
public bool CanRecompare => this.RecompareNeeded != null && this.edtLeft.Visible && this.edtLeft.TextLength > 0;
public bool CanViewFile
{
get
{
bool result = (this.ViewA.Focused || this.ViewB.Focused || this.edtLeft.Focused || this.edtRight.Focused) &&
(this.edtLeft.TextLength > 0 && this.edtRight.TextLength > 0);
return result;
}
}
[DefaultValue(38)]
public int LineDiffHeight
{
get
{
return this.pnlBottom.Height;
}
set
{
this.pnlBottom.Height = value;
}
}
[DefaultValue(32)]
public int OverviewWidth
{
get
{
return this.Overview.Width;
}
set
{
this.Overview.Width = value;
this.DiffControl_SizeChanged(this, EventArgs.Empty);
}
}
[DefaultValue(true)]
public bool ShowColorLegend
{
get
{
return this.showColorLegend;
}
set
{
if (this.showColorLegend != value)
{
this.showColorLegend = value;
this.lblDelete.Visible = value;
this.lblChange.Visible = value;
this.lblInsert.Visible = value;
this.tsSep6.Visible = value;
}
}
}
[DefaultValue(true)]
public bool ShowToolBar
{
get
{
return this.showToolBar;
}
set
{
if (this.showToolBar != value)
{
// Note: We have to store the state ourselves because
// Visible may return false even after we set it to true
// if any of its parents are visible.
this.showToolBar = value;
this.ToolBar.Visible = value;
}
}
}
[DefaultValue(false)]
public bool ShowWhiteSpaceInLineDiff
{
get
{
return this.ViewLineDiff.ShowWhitespace;
}
set
{
this.ViewLineDiff.ShowWhitespace = value;
}
}
[DefaultValue(false)]
public bool ShowWhiteSpaceInMainDiff
{
get
{
return this.ViewA.ShowWhitespace && this.ViewB.ShowWhitespace;
}
set
{
this.ViewA.ShowWhitespace = value;
this.ViewB.ShowWhitespace = value;
}
}
[DefaultValue(true)]
public bool UseTranslucentOverview
{
get
{
return this.Overview.UseTranslucentView;
}
set
{
this.Overview.UseTranslucentView = value;
}
}
public Font ViewFont
{
get
{
return this.ViewA.Font;
}
set
{
this.ViewA.Font = value;
this.ViewB.Font = value;
this.ViewLineDiff.Font = value;
}
}
#endregion
#region Private Properties
private DiffView ActiveView
{
get
{
DiffView result = this.ViewLineDiff.Focused
? this.ViewLineDiff
: (this.ViewB.Focused ? this.ViewB : this.ViewA);
return result;
}
}
private bool HasFindText => this.findData.Text.Length > 0;
private bool HasText => this.ActiveView.LineCount > 0;
#endregion
#region Public Methods
public bool CompareSelectedText()
{
bool result = false;
if (this.CanCompareSelectedText)
{
string textA = this.ViewA.SelectedText;
string textB = this.ViewB.SelectedText;
DifferenceEventArgs diffArgs = new(textA, textB);
this.ShowTextDifferences?.Invoke(this, diffArgs);
result = true;
}
return result;
}
public void Copy()
{
Clipboard.SetDataObject(this.ActiveView.SelectedText, true);
}
public bool Find()
{
bool result = this.ActiveView.Find(this.findData);
this.UpdateButtons();
return result;
}
public bool FindNext()
{
bool result = this.ActiveView.FindNext(this.findData);
this.UpdateButtons();
return result;
}
public bool FindPrevious()
{
bool result = this.ActiveView.FindPrevious(this.findData);
this.UpdateButtons();
return result;
}
public bool GoToFirstDiff() => this.ActiveView.GoToFirstDiff();
public bool GoToLastDiff() => this.ActiveView.GoToLastDiff();
public bool GoToLine() => this.ActiveView.GoToLine();
public bool GoToNextDiff() => this.ActiveView.GoToNextDiff();
public bool GoToPreviousDiff() => this.ActiveView.GoToPreviousDiff();
public bool Recompare()
{
bool result = false;
if (this.CanRecompare)
{
this.RecompareNeeded?.Invoke(this, EventArgs.Empty);
result = true;
}
return result;
}
public void SetData(IList<string> listA, IList<string> listB, EditScript script)
{
this.SetData(listA, listB, script, string.Empty, string.Empty);
}
public void SetData(IList<string> listA, IList<string> listB, EditScript script, string nameA, string nameB)
{
this.SetData(listA, listB, script, nameA, nameB, false, false, false);
}
public void SetData(
IList<string> listA,
IList<string> listB,
EditScript script,
string nameA,
string nameB,
bool changeDiffIgnoreCase,
bool changeDiffIgnoreWhiteSpace,
bool changeDiffTreatAsBinaryLines)
{
ChangeDiffOptions changeDiffOptions = ChangeDiffOptions.None;
if (changeDiffTreatAsBinaryLines)
{
changeDiffOptions |= ChangeDiffOptions.IgnoreBinaryPrefix;
}
else
{
if (changeDiffIgnoreCase)
{
changeDiffOptions |= ChangeDiffOptions.IgnoreCase;
}
if (changeDiffIgnoreWhiteSpace)
{
changeDiffOptions |= ChangeDiffOptions.IgnoreWhitespace;
}
}
this.ViewA.ChangeDiffOptions = changeDiffOptions;
this.ViewB.ChangeDiffOptions = changeDiffOptions;
this.ViewLineDiff.ChangeDiffOptions = changeDiffOptions;
this.ViewA.SetData(listA, script, true);
this.ViewB.SetData(listB, script, false);
Debug.Assert(this.ViewA.LineCount == this.ViewB.LineCount, "Both DiffView's LineCounts must be the same");
this.lblSimilarity.Text = string.Format("{0:P}", script.Similarity);
this.ViewA.SetCounterpartLines(this.ViewB);
this.Overview.DiffView = this.ViewA;
bool showNames = !string.IsNullOrEmpty(nameA) || !string.IsNullOrEmpty(nameB);
this.edtLeft.Visible = showNames;
this.edtRight.Visible = showNames;
if (showNames)
{
this.edtLeft.Text = nameA;
this.edtRight.Text = nameB;
}
this.UpdateButtons();
this.currentDiffLine = -1;
this.UpdateLineDiff();
this.ActiveControl = this.ViewA;
}
public void ViewFile()
{
if (this.CanViewFile)
{
string fileName;
if (this.ViewA.Focused || this.edtLeft.Focused)
{
fileName = this.edtLeft.Text;
}
else
{
fileName = this.edtRight.Text;
}
WindowsUtility.ShellExecute(this, fileName);
}
}
#endregion
#region Internal Methods
internal static void PaintColorLegendItem(ToolStripItem? item, PaintEventArgs e)
{
if (item != null)
{
// Make our outermost painting rect a little smaller.
Rectangle r = e.ClipRectangle;
r.Inflate(-1, -1);
// Paint the background.
Graphics g = e.Graphics;
using (Brush b = new SolidBrush(item.BackColor))
{
g.FillRectangle(b, r);
}
// Draw a border.
Rectangle borderRect = new(r.X, r.Y, r.Width - 1, r.Height - 1);
ControlPaint.DrawVisualStyleBorder(g, borderRect);
// Draw the image centered. (I should probably check the
// item's ImageAlign property here, but I know I'm always
// using MiddleCenter for all the passed-in items.)
Image image = item.Image;
Rectangle imageRect = new(r.X + ((r.Width - image.Width) / 2), r.Y + ((r.Height - image.Height) / 2), image.Width, image.Height);
g.DrawImage(image, imageRect);
}
}
#endregion
#region Private Methods
private void Copy_Click(object? sender, EventArgs e)
{
this.Copy();
}
private void Find_Click(object? sender, EventArgs e)
{
this.Find();
}
private void FindNext_Click(object? sender, EventArgs e)
{
this.FindNext();
}
private void FindPrevious_Click(object? sender, EventArgs e)
{
this.FindPrevious();
}
private void FirstDiff_Click(object? sender, EventArgs e)
{
this.GoToFirstDiff();
}
private void GotoLine_Click(object? sender, EventArgs e)
{
this.GoToLine();
}
private void LastDiff_Click(object? sender, EventArgs e)
{
this.GoToLastDiff();
}
private void NextDiff_Click(object? sender, EventArgs e)
{
this.GoToNextDiff();
}
private void PrevDiff_Click(object? sender, EventArgs e)
{
this.GoToPreviousDiff();
}
private void Recompare_Click(object? sender, EventArgs e)
{
this.Recompare();
}
private void ViewFile_Click(object? sender, EventArgs e)
{
this.ViewFile();
}
[SuppressMessage("Design", "CC0091:Use static method", Justification = "Windows Forms designer prefers non-static.")]
private void ColorLegend_Paint(object sender, PaintEventArgs e)
{
PaintColorLegendItem(sender as ToolStripItem, e);
}
private void DiffControl_SizeChanged(object? sender, EventArgs e)
{
this.pnlLeft.Width = (this.Width - this.pnlLeft.Left - this.MiddleSplitter.Width) / 2;
}
private void DiffOptionsChanged(object? sender, EventArgs e)
{
this.UpdateColors();
}
private void TextDiff_Click(object? sender, EventArgs e)
{
this.CompareSelectedText();
}
private void Overview_LineClick(object sender, DiffLineClickEventArgs e)
{
this.ViewA.CenterVisibleLine = e.Line;
this.ActiveView.Position = new DiffViewPosition(e.Line, 0);
}
private void UpdateButtons()
{
this.btnViewFile.Enabled = this.CanViewFile;
this.mnuViewFile.Enabled = this.btnViewFile.Enabled;
this.btnCopy.Enabled = this.CanCopy;
this.mnuCopy.Enabled = this.btnCopy.Enabled;
bool canCompareText = this.CanCompareSelectedText;
this.btnTextDiff.Enabled = canCompareText;
this.mnuTextDiff.Enabled = canCompareText;
this.btnFind.Enabled = this.CanFind;
this.btnFindNext.Enabled = this.CanFindNext;
this.btnFindPrevious.Enabled = this.CanFindPrevious;
this.btnFirstDiff.Enabled = this.CanGoToFirstDiff;
this.btnNextDiff.Enabled = this.CanGoToNextDiff;
this.btnPrevDiff.Enabled = this.CanGoToPreviousDiff;
this.btnLastDiff.Enabled = this.CanGoToLastDiff;
this.btnGotoLine.Enabled = this.CanGoToLine;
this.btnRecompare.Enabled = this.CanRecompare;
}
private void UpdateColors()
{
this.lblDelete.BackColor = DiffOptions.DeletedColor;
this.lblChange.BackColor = DiffOptions.ChangedColor;
this.lblInsert.BackColor = DiffOptions.InsertedColor;
}
private void UpdateLineDiff()
{
int line = (this.ActiveView == this.ViewA) ? this.ViewA.Position.Line : this.ViewB.Position.Line;
if (line != this.currentDiffLine)
{
this.currentDiffLine = line;
DiffViewLine? lineOne = null;
DiffViewLine? lineTwo = null;
if (line < this.ViewA.LineCount)
{
lineOne = this.ViewA.Lines?[line];
}
// Normally, ViewA.LineCount == ViewB.LineCount, but during
// SetData they'll be mismatched momentarily as each view
// rebuilds its lines.
if (line < this.ViewB.LineCount)
{
lineTwo = this.ViewB.Lines?[line];
}
if (lineOne != null && lineTwo != null)
{
this.ViewLineDiff.SetData(lineOne, lineTwo);
}
}
}
private void View_PositionChanged(object? sender, EventArgs e)
{
DiffView view = this.ActiveView;
DiffViewPosition pos = view.Position;
this.lblPosition.Text = string.Format("Ln {0}, Col {1}", pos.Line + 1, pos.Column + 1);
this.UpdateButtons();
if (view != this.ViewLineDiff)
{
this.UpdateLineDiff();
}
}
private void ViewA_HScrollPosChanged(object? sender, EventArgs e)
{
this.ViewB.HScrollPos = this.ViewA.HScrollPos;
}
private void ViewA_VScrollPosChanged(object? sender, EventArgs e)
{
this.ViewB.VScrollPos = this.ViewA.VScrollPos;
}
private void ViewB_HScrollPosChanged(object? sender, EventArgs e)
{
this.ViewA.HScrollPos = this.ViewB.HScrollPos;
}
private void ViewB_VScrollPosChanged(object? sender, EventArgs e)
{
this.ViewA.VScrollPos = this.ViewB.VScrollPos;
}
private void ViewLineDiff_SizeChanged(object? sender, EventArgs e)
{
this.LineDiffSizeChanged?.Invoke(this, e);
}
#endregion
}
}