-
Notifications
You must be signed in to change notification settings - Fork 42
/
ReplaceDialog.cs
145 lines (114 loc) · 4.79 KB
/
ReplaceDialog.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyNotepad {
public partial class ReplaceDialog : Form {
private Main _Main;
public ReplaceDialog(Main pMain) {
InitializeComponent();
_Main = pMain;
}
private void controlFindWhatTextBox_Enter(object sender, EventArgs e) {
var Sender = (TextBox)sender;
Sender.SelectAll();
}
private void controlReplaceWithTextBox_Enter(object sender, EventArgs e) {
var Sender = (TextBox)sender;
Sender.SelectAll();
}
private void controlFindWhatTextBox_TextChanged(object sender, EventArgs e) {
UpdateButtons();
}
private void ReplaceDialog_Load(object sender, EventArgs e) {
UpdateButtons();
}
private void UpdateButtons() {
buttonFindNext.Enabled =
buttonReplace.Enabled =
buttonReplaceAll.Enabled = controlFindWhatTextBox.Text.Length > 0;
}
private void buttonCancel_Click(object sender, EventArgs e) {
Hide();
}
private void ReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = true;
Hide();
}
public void Triggered() {
controlFindWhatTextBox.Focus();
}
private void buttonFindNext_Click(object sender, EventArgs e) {
var SearchText = controlFindWhatTextBox.Text;
var MatchCase = controlMatchCaseCheckBox.Checked;
var bSearchDown = true;
if (!_Main.FindAndSelect(SearchText, MatchCase, bSearchDown)) {
MessageBox.Show(this, CONST.CannotFindMessage.FormatUsingObject(new { SearchText = SearchText }), "Notepad Clone");
}
}
private void buttonReplace_Click(object sender, EventArgs e) {
var SearchText = controlFindWhatTextBox.Text;
var ReplaceWithText = controlReplaceWithTextBox.Text;
var MatchCase = controlMatchCaseCheckBox.Checked;
var eStringComparison = MatchCase ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
var bSearchDown = true;
if (_Main.SelectedText.Equals(SearchText)) {
_Main.SelectedText = ReplaceWithText;
}
if (!_Main.FindAndSelect(SearchText, MatchCase, bSearchDown)) {
MessageBox.Show(this, CONST.CannotFindMessage.FormatUsingObject(new { SearchText = SearchText }), "Notepad Clone");
}
}
private void buttonReplaceAll_Click(object sender, EventArgs e) {
var Content = _Main.Content;
var SearchText = controlFindWhatTextBox.Text;
var MatchCase = controlMatchCaseCheckBox.Checked;
var Indexes = Helper.GetIndexes(Content, SearchText, MatchCase);
//if (!Indexes.Any()) { // TODO: Notepad doesn't do this
// MessageBox.Show(this, CONST.CannotFindMessage.FormatUsingObject(new { SearchText = SearchText }), "Notepad Clone");
// return;
//}
var Builder = new StringBuilder();
var ReplaceWith = controlReplaceWithTextBox.Text;
{ // scope
var LastIndex = -1;
foreach (var Index in Indexes) {
if (Index != 0) {
#region TakeStart
int TakeStart;
if (LastIndex == -1) {
TakeStart = 0;
} else {
TakeStart = LastIndex + SearchText.Length;
}
#endregion
var TakeEnd = Index - 1;
var Length = (TakeEnd - TakeStart) + 1;
var InBetween = Content.Substring(TakeStart, Length);
Builder.Append(InBetween);
}
Builder.Append(ReplaceWith);
LastIndex = Index;
}
{
#region TakeStart
int TakeStart;
if (LastIndex == -1) {
TakeStart = 0;
} else {
TakeStart = LastIndex + SearchText.Length;
}
#endregion
var TakeEnd = Content.Length - 1;
var Length = (TakeEnd - TakeStart) + 1;
Builder.Append(Content.Substring(TakeStart, Length));
}
}
_Main.Content = Builder.ToString();
}
}
}